承接 azjezz/typed 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

azjezz/typed

Composer 安装命令:

composer require azjezz/typed

包简介

Typed variables for PHP 7.4+

README 文档

README

This library allows you to create typed variables in PHP 7.4 +

Composer installation :

$ composer require azjezz/typed

Usage Example :

<?php declare(strict_types=1);

use Typed as t;

$name = &t\string('saif');
$age = &t\int(19);

try {

  $name = 'azjezz'; // works
  $name = 15; // fails
  $age = null; // fails

} catch(TypeError $error) {
   
} finally {
    t\clean();
}

callable => func, array => { arr, keyset, vector, set }

Since callable and array cant be used for function names, instead i have used func for callable and arr for array. keyset, vector and set are just helper to help you create an array of keys ( keyset = arr(array_keys($value)) ), an array of values ( vector = arr(array_values($value)) ) and an array of unique values ( set = arr(array_unique($value)) ).

typed

typed() is a function to help you create a typed variables based on the type of the passed variable, currently it supports string, int, float, bool, object and iterable. it was suggested by @mikesherov on twitter.

purge, clean, delete and c

As mentioned here and here, memory leak is a huge issue here, if a specific function created a typed variable, the variable will still exist inside the repository even after execution, for this i made some helper functions that allow you to remove variables from the repository.

  • Typed\purge() will delete every reference registered in the repository, you can call this function at the end of every request/response cycle in a web application per example.
  • Typed\clean(string $namespace = 'default') this function will delete every reference to a typed variable in a specific namespace as shown in the example above, its recommended to use a unique namespace every time you use typed variables and call clean() to ensure there's no memory leak.
  • Typed\filter(mixed $value, string $namespace = 'default') this function will delete the every created reference to a typed variable with the given value in a specific namespace.
  • Typed\delete(mixed $value, string $namespace = 'default') similar to what Typed\filter does, this function allows you to delete the last created reference to a typed variable with the given value in a specific namespace. example :
<?php declare(strict_types=1);

use Typed as t;
    
$name = &t\string('azjezz', 'data');
$age = &t\int(19, 'data');

t\delete($name, 'data');

/**
 * the reference to the `$name` variable has been deleted.
 * therefor we can assign any type to the variable `$name` now. 
 */
$name = []; // works
  • Typed\c this function accepted a callable that take no arguments, the callable will be called inside c ( container ) and all the assigned typed variables in the time of the call would be deleted afterward, this makes it easier to ensure that function calls don't cause any memory leak. example :
<?php declare(strict_types=1);

use Typed as t;
use function Typed\c;

/**
 * all assigned variables inside the callable will be destroyed after execution. 
 */
c(function(): void {
    $name = &t\string('saif eddin');
    $age = &t\int(5);
    $arr = &t\arr([
        'age' => $age,
        'name' => $name    
    ]);
});

More :

A cool PSR-15 Middleware to delete all typed variables created inside the next middleware/handler

<?php declare(strict_types=1);

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use function Typed\c;

class TypedMiddleware implements MiddlewareInterface 
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = null;
        c(function() use($request, &$response, $handler) {
            /**
             * if any typed variable is created in the handler, it would be deleted after execution.
             */
            $response = $handler->handle($request);
        });
        return $response;
    }
}

TODO :

  • add nullable* functions.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 69
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 69
  • Watchers: 8
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固