guanguans

guanguans 查看完整档案

杭州编辑  |  填写毕业院校  |  填写所在公司/组织 www.guanguans.cn 编辑
编辑

No practice, no gain in one's wit.

个人动态

guanguans 发布了文章 · 2月23日

用 symfony/options-resolver 优雅的校验类初始化选项

示例类

use Symfony\Component\OptionsResolver\OptionsResolver;

class Email
{
    private $options;

    /**
     * Email constructor.
     *
     * @param  array  $options
     */
    public function __construct(array $options = [])
    {
        $this->setOptions($options);
    }

    /**
     * Configuration options.
     *
     * @param \Closure $closure
     * @param null     $object
     *
     * @return array
     */
    protected function configureOptions(array $options, Closure $closure, $object = null)
    {
        $resolver = new OptionsResolver();

        $closure($resolver);

        if ($object instanceof Closure) {
            return $object()->options = $resolver->resolve($options);
        }

        if (is_object($object)) {
            return $object->options = $resolver->resolve($options);
        }

        if (is_string($object) && class_exists($object)) {
            $object = new $object();

            return $object->options = $resolver->resolve($options);
        }

        return $resolver->resolve($options);
    }

    /**
     * @return mixed
     */
    public function getOptions()
    {
        return $this->options;
    }

    /**
     * @param  array  $options
     */
    public function setOptions(array $options): void
    {
        $this->options = $this->configureOptions($options, function (OptionsResolver $resolver) {
            $resolver->setDefaults([
                'host' => 'smtp.example.org',
                'username' => 'user',
                'password' => 'password',
                'port' => 25,
            ]);
            $resolver->setRequired(['host', 'username', 'password', 'port']);
            $resolver->setAllowedTypes('host', 'string');
            $resolver->setAllowedTypes('username', 'string');
            $resolver->setAllowedTypes('password', 'string');
            $resolver->setAllowedTypes('port', 'int');
        });
    }
}

选项通过校验

$email = new Email([
    'host'     => 'smtp.example.org',
    'username' => 'user',
    'password' => 'password',
    'port'     => 25,
]);

var_export($email);
Email::__set_state(array(
    'options' =>
        array (
            'host' => 'smtp.example.org',
            'username' => 'user',
            'password' => 'password',
            'port' => 25,
        ),
))

选项未通过校验

$email = new Email([
    'host'     => 'smtp.example.org',
    'username' => 'user',
    'password' => 'password',
    'port'     => '25', // 注意这里
]);

var_export($email);
PHP Fatal error:  Uncaught Symfony\Component\OptionsResolver\Exception\InvalidOptionsException: The option "port" with value "25" is expected to be of type "int", but is of type "string".

总结

个人觉得可使用该 symfony/options-resolver 组件较好的对开发第三方 sdk 的客户端类初始化参数进行校验,例如个人适配的 Yii 极光推送扩展包 - yii-jpush 中的应用。

查看原文

赞 1 收藏 1 评论 0

guanguans 关注了标签 · 1月27日

关注 975

guanguans 关注了标签 · 1月27日

nginx

nginx(发音同engine x)是一款由俄罗斯程序员Igor Sysoev所开发轻量级的网页服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。

关注 6535

guanguans 关注了标签 · 1月27日

golang

Go语言是谷歌2009发布的第二款开源编程语言。Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全、支持并行进程。
Go语言是谷歌推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性。谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发Go,是因为过去10多年间软件开发的难度令人沮丧。Go是谷歌2009发布的第二款编程语言。

七牛云存储CEO许式伟出版《Go语言编程
go语言翻译项目 http://code.google.com/p/gola...
《go编程导读》 http://code.google.com/p/ac-m...
golang的官方文档 http://golang.org/doc/docs.html
golang windows上安装 http://code.google.com/p/gomi...

关注 26183

guanguans 关注了标签 · 1月27日

关注 509

guanguans 发布了文章 · 1月26日

身份证号验证库

id-validator - Go 版中国身份证号验证库。

功能

  • 中国身份证号验证
  • 获取身份证号信息
  • 升级 15 位身份证号为 18 位
  • 伪造符合校验的身份证号

安装

$ go get -u github.com/guanguans/id-validator

使用

这只是一个快速介绍, 请查看 GoDoc 获得详细信息。

package main

import (
    idvalidator "github.com/guanguans/id-validator"
    "gopkg.in/ffmt.v1"
)

func main() {
    // 验证身份证号合法性
    ffmt.P(idvalidator.IsValid("440308199901101512"))  // 大陆居民身份证18位
    ffmt.P(idvalidator.IsValid("610104620927690"))     // 大陆居民身份证15位
    ffmt.P(idvalidator.IsValid("810000199408230021"))  // 港澳居民居住证18位
    ffmt.P(idvalidator.IsValid("830000199201300022"))  // 台湾居民居住证18位

    // 获取身份证号信息
    ffmt.P(idvalidator.GetInfo("440308199901101512"))
    // []interface {}[
    //     github.com/guanguans/id-validator.IdInfo{          // 身份证号信息
    //         AddressCode: int(440308)                           // 地址码
    //         Abandoned:   int(0)                                // 地址码是否废弃:1为废弃的,0为正在使用的
    //         Address:     string("广东省深圳市盐田区")             // 地址
    //         AddressTree: []string[                             // 省市区三级列表
    //            string("广东省")                                    // 省
    //            string("深圳市")                                    // 市
    //            string("盐田区")                                    // 区
    //        ]
    //         Birthday:      <1999-01-10 00:00:00 +0000 UTC>     // 出生日期
    //         Constellation: string("摩羯座")                     // 星座
    //         ChineseZodiac: string("卯兔")                       // 生肖
    //         Sex:           int(1)                              // 性别:1为男性,0为女性
    //         Length:        int(18)                             // 号码长度
    //         CheckBit:      string("2")                         // 校验码
    //     }
    //     <nil>                                              // 错误信息
    // ]

    // 生成可通过校验的假身份证号
    ffmt.P(idvalidator.FakeId()) // 随机生成
    ffmt.P(idvalidator.FakeRequireId(true, "江苏省", "200001", 1)) // 生成出生于2000年1月江苏省的男性居民身份证

    // 15位号码升级为18位
    ffmt.P(idvalidator.UpgradeId("610104620927690"))
    // []interface {}[
    //     string("610104196209276908") // 升级后号码
    //     <nil>                        // 错误信息
    // ]
}

源码链接

相关项目

参考资料

查看原文

赞 2 收藏 1 评论 0

guanguans 发布了文章 · 2020-12-22

用 Composer 组建了个 PHP 框架

image

大约在两三年前自己就有过写个 PHP web 框架的想法,期间也尝试过去实践开发,但总是由于各种各样的原因并没有完成。

这次终于恒心下来,忙里偷闲的组建了一个自己的 PHP 微框架。前前后后零零碎碎的开发了快一个月的时间,直到今天完成文档的编写,才向大家呈现。

除了框架本身的(Coole)核心代码,还提供一个框架应用(coolephp/skeleton)模板,另外编写了一份简陋的文档, 望能给 也想写框架的人带来一点参考(大神请忽略)。

生命周期

image

  1. 客户端向入口脚本 index.php 发起请求。
  2. 入口脚本中创建应用,向应用中注册配置服务、核心服务、第三方服务。
  3. 定义路由,载入到应用中。
  4. 启动运行应用。
  5. 创建请求对象。
  6. 根据请求对象解析路由,创建控制器实例。
  7. 动作调用模型数据,渲染到视图,生成响应对象。
  8. 管道过滤响应。
  9. 返回响应给客户端。
  10. 终止生命周期。

命令行界面

image

安装框架

$ composer require guanguans/coole -vvv

快速开始

<?php

use Guanguans\Coole\App;
use Guanguans\Coole\Facade\Router;
use Symfony\Component\HttpFoundation\Request;

require __DIR__.'/vendor/autoload.php';

// 1. 创建应用
$app = new App();
$app['debug'] = true;

// 2. 定义一个带中间件的路由
Router::get('/', function (){
    return 'This is the Coole framework.';
})->setMiddleware(function (Request $request, Closure $next){
    printf('Before request.<br>');
    $response = $next($request);
    printf('<br>After request.');

    return $response;
});

// 3. 监听运行
$app->run();

框架中每项功能职责所用到的组件

参考的文章及借鉴的框架

官方文档

源码链接

查看原文

赞 9 收藏 5 评论 0

guanguans 发布了文章 · 2020-12-08

Yii 中优雅的使用事件

image

Yii 中使用一个事件大概是这个样子的

// 绑定事件
$component->on($event::EVENT_NAME, [$object, 'methodNameA']);
$component->on($event::EVENT_NAME, [$object, 'methodNameB']);
// 触发事件
$component->trigger($event::EVENT_NAME, $event); 
从上面代码中可以看出如果要触发一个 5 个监听监听的事件,是要入侵 6 行代码的,这显然是不够优雅的,所以写了个组件包将事件与监听绑定写在组件配置文件,调用的时候只需一行代码去触发事件。

源码

环境要求

  • Yii > 2.0

安装

$ composer require guanguans/yii-event -vvv

配置

...
'components' => [
    ...
    'event' => [
        'class' => \Guanguans\YiiEvent\Event::className(),
        'listen' => [
            // 事件类名
            \app\events\ExampleEvent::className() => [      
                // 监听该事件监听的类名
                \app\listeners\ExampleListener::class,
            ],
        ],
    ],
    ...
],
...

使用示例

创建事件 app\events\ExampleEvent.php

namespace app\events;

use yii\base\Event;

class ExampleEvent extends Event
{
    public $name = 'example';
}

创建监听 app\listeners\ExampleListener.php

namespace app\listeners;

use Guanguans\YiiEvent\ListenerInterface;
use yii\base\Event;

class ExampleListener implements ListenerInterface
{
    public static function handle(Event $event)
    {
        // to do something.
        var_export($event->name);
    }
}

触发事件

Yii::$app->event->dispatch(new ExampleEvent());
// or
event(new ExampleEvent());

验证结果

'example'
查看原文

赞 4 收藏 2 评论 0

guanguans 发布了文章 · 2020-10-29

中文注释版 Laravel 容器类(Container)

中文注释版 Laravel 容器类(Container)

将 Laravel 的 illuminate/container 移除掉 illuminate/contracts 依赖,便于灵活的在非 Laravel 框架中使用,顺便机翻了一下 illuminate/container 的注释,便于理解 Laravel 的容器。

相关链接

代码

<?php

/*
 * This file is part of the guanguans/di.
 *
 * (c) guanguans <ityaozm@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled.
 */

namespace Guanguans\Di;

use ArrayAccess;
use Closure;
use LogicException;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionParameter;

/**
 * This file is modified from `illuminate/container`.
 *
 * @see https://github.com/illuminate/container
 */
class Container implements ArrayAccess, ContainerInterface
{
    /**
     * The current globally available container (if any).
     * 当前的全局可用容器(如果有)。
     *
     * @var static
     */
    protected static $instance;

    /**
     * An array of the types that have been resolved.
     * 已解析类型的数组。
     *
     * @var array
     */
    protected $resolved = [];

    /**
     * The container's bindings.
     * 容器的绑定。
     *
     * @var array
     */
    protected $bindings = [];

    /**
     * The container's method bindings.
     * 容器的方法绑定。
     *
     * @var array
     */
    protected $methodBindings = [];

    /**
     * The container's shared instances.
     * 容器的共享实例。
     *
     * @var array
     */
    protected $instances = [];

    /**
     * The registered type aliases.
     * 注册的类型别名。
     *
     * @var array
     */
    protected $aliases = [];

    /**
     * The registered aliases keyed by the abstract name.
     * 以抽象名称为关键字的注册别名。
     *
     * @var array
     */
    protected $abstractAliases = [];

    /**
     * The extension closures for services.
     * 服务的扩展名闭包。
     *
     * @var array
     */
    protected $extenders = [];

    /**
     * All of the registered tags.
     * 所有注册的标签。
     *
     * @var array
     */
    protected $tags = [];

    /**
     * The stack of concretions currently being built.
     * 目前正在构建的堆栈。
     *
     * @var array
     */
    protected $buildStack = [];

    /**
     * The parameter override stack.
     * 参数覆盖堆栈。
     *
     * @var array
     */
    protected $with = [];

    /**
     * The contextual binding map.
     * 上下文绑定图。
     *
     * @var array
     */
    public $contextual = [];

    /**
     * All of the registered rebound callbacks.
     * 所有已注册的反弹回调。
     *
     * @var array
     */
    protected $reboundCallbacks = [];

    /**
     * All of the global resolving callbacks.
     * 所有全局解析回调。
     *
     * @var array
     */
    protected $globalResolvingCallbacks = [];

    /**
     * All of the global after resolving callbacks.
     * 解决回调后的所有全局变量。
     *
     * @var array
     */
    protected $globalAfterResolvingCallbacks = [];

    /**
     * All of the resolving callbacks by class type.
     * 按类类型的所有解析回调。
     *
     * @var array
     */
    protected $resolvingCallbacks = [];

    /**
     * All of the after resolving callbacks by class type.
     * 所有按类类型解析后的回调。
     *
     * @var array
     */
    protected $afterResolvingCallbacks = [];

    /**
     * Define a contextual binding.
     * 定义上下文绑定。
     *
     * @param  string  $concrete
     * @return \Guanguans\Di\ContextualBindingBuilder
     */
    public function when($concrete)
    {
        return new ContextualBindingBuilder($this, $this->getAlias($concrete));
    }

    /**
     * Determine if the given abstract type has been bound.
     * 确定给定的抽象类型是否已绑定。
     *
     * @param  string  $abstract
     * @return bool
     */
    public function bound($abstract)
    {
        return isset($this->bindings[$abstract]) ||
               isset($this->instances[$abstract]) ||
               $this->isAlias($abstract);
    }

    /**
     * Determine if the given abstract type has been resolved.
     * 确定给定的抽象类型是否已解析。
     *
     * @param  string  $abstract
     * @return bool
     */
    public function resolved($abstract)
    {
        if ($this->isAlias($abstract)) {
            $abstract = $this->getAlias($abstract);
        }

        return isset($this->resolved[$abstract]) ||
               isset($this->instances[$abstract]);
    }

    /**
     * Determine if a given type is shared.
     * 确定给定类型是否共享。
     *
     * @param  string  $abstract
     * @return bool
     */
    public function isShared($abstract)
    {
        return isset($this->instances[$abstract]) ||
              (isset($this->bindings[$abstract]['shared']) &&
               $this->bindings[$abstract]['shared'] === true);
    }

    /**
     * Determine if a given string is an alias.
     * 确定给定的字符串是否为别名
     *
     * @param  string  $name
     * @return bool
     */
    public function isAlias($name)
    {
        return isset($this->aliases[$name]);
    }

    /**
     * Register a binding with the container.
     * 在容器中注册绑定
     *
     * @param  string|array  $abstract
     * @param  \Closure|string|null  $concrete
     * @param  bool  $shared
     * @return void
     */
    public function bind($abstract, $concrete = null, $shared = false)
    {
        // If no concrete type was given, we will simply set the concrete type to the
        // abstract type. After that, the concrete type to be registered as shared
        // without being forced to state their classes in both of the parameters.
        // 如果没有给出具体类型,我们将简单地将具体类型设置为抽象类型。
        // 之后,要注册为共享的具体类型,而不必强制在两个参数中声明其类。
        $this->dropStaleInstances($abstract);

        if (is_null($concrete)) {
            $concrete = $abstract;
        }

        // If the factory is not a Closure, it means it is just a class name which is
        // bound into this container to the abstract type and we will just wrap it
        // up inside its own Closure to give us more convenience when extending.
        // 如果工厂不是 Closure,则意味着它只是一个类名称,
        // 该类名称绑定到此容器中,与抽象类型绑定,
        // 我们将其包装在自己的 Closure 中,以便在扩展时为我们提供更多便利。
        if (! $concrete instanceof Closure) {
            $concrete = $this->getClosure($abstract, $concrete);
        }

        $this->bindings[$abstract] = compact('concrete', 'shared');

        // If the abstract type was already resolved in this container we'll fire the
        // rebound listener so that any objects which have already gotten resolved
        // can have their copy of the object updated via the listener callbacks.
        // 如果抽象类型已在此容器中解析,我们将触发回弹侦听器,
        // 以便所有已解析的对象都可以通过侦听器回调更新其对象副本。
        if ($this->resolved($abstract)) {
            $this->rebound($abstract);
        }
    }

    /**
     * Get the Closure to be used when building a type.
     * 获取要在构建类型时使用的闭包。
     *
     * @param  string  $abstract
     * @param  string  $concrete
     * @return \Closure
     */
    protected function getClosure($abstract, $concrete)
    {
        return function ($container, $parameters = []) use ($abstract, $concrete) {
            if ($abstract == $concrete) {
                return $container->build($concrete);
            }

            return $container->makeWith($concrete, $parameters);
        };
    }

    /**
     * Determine if the container has a method binding.
     * 确定容器是否具有方法绑定。
     *
     * @param  string  $method
     * @return bool
     */
    public function hasMethodBinding($method)
    {
        return isset($this->methodBindings[$method]);
    }

    /**
     * Bind a callback to resolve with Container::call.
     * 绑定回调以使用 Container::call 进行解析。
     *
     * @param  string  $method
     * @param  \Closure  $callback
     * @return void
     */
    public function bindMethod($method, $callback)
    {
        $this->methodBindings[$method] = $callback;
    }

    /**
     * Get the method binding for the given method.
     * 获取给定方法的方法绑定。
     *
     * @param  string  $method
     * @param  mixed  $instance
     * @return mixed
     */
    public function callMethodBinding($method, $instance)
    {
        return call_user_func($this->methodBindings[$method], $instance, $this);
    }

    /**
     * Add a contextual binding to the container.
     * 将上下文绑定添加到容器。
     *
     * @param  string  $concrete
     * @param  string  $abstract
     * @param  \Closure|string  $implementation
     * @return void
     */
    public function addContextualBinding($concrete, $abstract, $implementation)
    {
        $this->contextual[$concrete][$this->getAlias($abstract)] = $implementation;
    }

    /**
     * Register a binding if it hasn't already been registered.
     * 注册绑定(如果尚未注册)。
     *
     * @param  string  $abstract
     * @param  \Closure|string|null  $concrete
     * @param  bool  $shared
     * @return void
     */
    public function bindIf($abstract, $concrete = null, $shared = false)
    {
        if (! $this->bound($abstract)) {
            $this->bind($abstract, $concrete, $shared);
        }
    }

    /**
     * Register a shared binding in the container.
     * 在容器中注册共享绑定。
     *
     * @param  string|array  $abstract
     * @param  \Closure|string|null  $concrete
     * @return void
     */
    public function singleton($abstract, $concrete = null)
    {
        $this->bind($abstract, $concrete, true);
    }

    /**
     * "Extend" an abstract type in the container.
     * “扩展”容器中的抽象类型。
     *
     * @param  string    $abstract
     * @param  \Closure  $closure
     * @return void
     *
     * @throws \InvalidArgumentException
     */
    public function extend($abstract, Closure $closure)
    {
        $abstract = $this->getAlias($abstract);

        if (isset($this->instances[$abstract])) {
            $this->instances[$abstract] = $closure($this->instances[$abstract], $this);

            $this->rebound($abstract);
        } else {
            $this->extenders[$abstract][] = $closure;

            if ($this->resolved($abstract)) {
                $this->rebound($abstract);
            }
        }
    }

    /**
     * Register an existing instance as shared in the container.
     * 将现有实例注册为在容器中共享的实例。
     *
     * @param  string  $abstract
     * @param  mixed   $instance
     * @return void
     */
    public function instance($abstract, $instance)
    {
        $this->removeAbstractAlias($abstract);

        $isBound = $this->bound($abstract);

        unset($this->aliases[$abstract]);

        // We'll check to determine if this type has been bound before, and if it has
        // we will fire the rebound callbacks registered with the container and it
        // can be updated with consuming classes that have gotten resolved here.
        // 我们将检查以确定此类型是否以前已绑定,
        // 如果已经绑定,将触发在容器中注册的反弹回调,
        // 并且可以使用在这里已解决的消耗类进行更新。
        $this->instances[$abstract] = $instance;

        if ($isBound) {
            $this->rebound($abstract);
        }
    }

    /**
     * Remove an alias from the contextual binding alias cache.
     * 从上下文绑定别名缓存中删除别名。
     *
     * @param  string  $searched
     * @return void
     */
    protected function removeAbstractAlias($searched)
    {
        if (! isset($this->aliases[$searched])) {
            return;
        }

        foreach ($this->abstractAliases as $abstract => $aliases) {
            foreach ($aliases as $index => $alias) {
                if ($alias == $searched) {
                    unset($this->abstractAliases[$abstract][$index]);
                }
            }
        }
    }

    /**
     * Assign a set of tags to a given binding.
     * 将一组标签分配给给定的绑定。
     *
     * @param  array|string  $abstracts
     * @param  array|mixed   ...$tags
     * @return void
     */
    public function tag($abstracts, $tags)
    {
        $tags = is_array($tags) ? $tags : array_slice(func_get_args(), 1);

        foreach ($tags as $tag) {
            if (! isset($this->tags[$tag])) {
                $this->tags[$tag] = [];
            }

            foreach ((array) $abstracts as $abstract) {
                $this->tags[$tag][] = $abstract;
            }
        }
    }

    /**
     * Resolve all of the bindings for a given tag.
     * 解决给定标签的所有绑定。
     *
     * @param  string  $tag
     * @return array
     */
    public function tagged($tag)
    {
        $results = [];

        if (isset($this->tags[$tag])) {
            foreach ($this->tags[$tag] as $abstract) {
                $results[] = $this->make($abstract);
            }
        }

        return $results;
    }

    /**
     * Alias a type to a different name.
     * 将别名别名为另一个名称。
     *
     * @param  string  $abstract
     * @param  string  $alias
     * @return void
     */
    public function alias($abstract, $alias)
    {
        $this->aliases[$alias] = $abstract;

        $this->abstractAliases[$abstract][] = $alias;
    }

    /**
     * Bind a new callback to an abstract's rebind event.
     * 绑定一个新的回调到抽象的重新绑定事件。
     *
     * @param  string    $abstract
     * @param  \Closure  $callback
     * @return mixed
     */
    public function rebinding($abstract, Closure $callback)
    {
        $this->reboundCallbacks[$abstract = $this->getAlias($abstract)][] = $callback;

        if ($this->bound($abstract)) {
            return $this->make($abstract);
        }
    }

    /**
     * Refresh an instance on the given target and method.
     * 在给定的目标和方法上刷新实例。
     *
     * @param  string  $abstract
     * @param  mixed   $target
     * @param  string  $method
     * @return mixed
     */
    public function refresh($abstract, $target, $method)
    {
        return $this->rebinding($abstract, function ($app, $instance) use ($target, $method) {
            $target->{$method}($instance);
        });
    }

    /**
     * Fire the "rebound" callbacks for the given abstract type.
     * 为给定的抽象类型触发“反弹”回调。
     *
     * @param  string  $abstract
     * @return void
     */
    protected function rebound($abstract)
    {
        $instance = $this->make($abstract);

        foreach ($this->getReboundCallbacks($abstract) as $callback) {
            call_user_func($callback, $this, $instance);
        }
    }

    /**
     * Get the rebound callbacks for a given type.
     * 获取给定类型的反弹回调
     *
     * @param  string  $abstract
     * @return array
     */
    protected function getReboundCallbacks($abstract)
    {
        if (isset($this->reboundCallbacks[$abstract])) {
            return $this->reboundCallbacks[$abstract];
        }

        return [];
    }

    /**
     * Wrap the given closure such that its dependencies will be injected when executed.
     * 包装给定的闭包,以便在执行时注入其依赖项。
     *
     * @param  \Closure  $callback
     * @param  array  $parameters
     * @return \Closure
     */
    public function wrap(Closure $callback, array $parameters = [])
    {
        return function () use ($callback, $parameters) {
            return $this->call($callback, $parameters);
        };
    }

    /**
     * Call the given Closure / class@method and inject its dependencies.
     * 调用给定的 Closure / class@method 并注入其依赖项。
     *
     * @param  callable|string  $callback
     * @param  array  $parameters
     * @param  string|null  $defaultMethod
     * @return mixed
     */
    public function call($callback, array $parameters = [], $defaultMethod = null)
    {
        return BoundMethod::call($this, $callback, $parameters, $defaultMethod);
    }

    /**
     * Get a closure to resolve the given type from the container.
     * 获取一个容器来解析给定类型的闭包。
     *
     * @param  string  $abstract
     * @return \Closure
     */
    public function factory($abstract)
    {
        return function () use ($abstract) {
            return $this->make($abstract);
        };
    }

    /**
     * Resolve the given type with the given parameter overrides.
     * 使用给定的参数覆盖解析给定的类型。
     *
     * @param  string  $abstract
     * @param  array  $parameters
     * @return mixed
     */
    public function makeWith($abstract, array $parameters)
    {
        return $this->resolve($abstract, $parameters);
    }

    /**
     * Resolve the given type from the container.
     * 从容器解析给定类型。
     *
     * @param  string  $abstract
     * @return mixed
     */
    public function make($abstract)
    {
        return $this->resolve($abstract);
    }

    /**
     * Resolve the given type from the container.
     * 从容器解析给定类型。
     *
     * @param  string  $abstract
     * @param  array  $parameters
     * @return mixed
     */
    protected function resolve($abstract, $parameters = [])
    {
        $abstract = $this->getAlias($abstract);

        $needsContextualBuild = ! empty($parameters) || ! is_null(
            $this->getContextualConcrete($abstract)
        );

        // If an instance of the type is currently being managed as a singleton we'll
        // just return an existing instance instead of instantiating new instances
        // so the developer can keep using the same objects instance every time.
        // 如果当前正在以单例方式管理该类型的实例,
        // 我们将只返回一个现有实例,而不是实例化新实例,
        // 以便开发人员每次都可以继续使用相同的对象实例。
        if (isset($this->instances[$abstract]) && ! $needsContextualBuild) {
            return $this->instances[$abstract];
        }

        $this->with[] = $parameters;

        $concrete = $this->getConcrete($abstract);

        // We're ready to instantiate an instance of the concrete type registered for
        // the binding. This will instantiate the types, as well as resolve any of
        // its "nested" dependencies recursively until all have gotten resolved.
        // 我们已经准备好实例化为绑定注册的具体类型的实例。
        // 这将实例化类型,并以递归方式解析其“嵌套”依赖项中的任何一个,直到全部解析完毕。
        if ($this->isBuildable($concrete, $abstract)) {
            $object = $this->build($concrete);
        } else {
            $object = $this->make($concrete);
        }

        // If we defined any extenders for this type, we'll need to spin through them
        // and apply them to the object being built. This allows for the extension
        // of services, such as changing configuration or decorating the object.
        // 如果我们为此类型定义了任何扩展程序,则需要遍历它们并将其应用于正在构建的对象。
        // 这允许扩展服务,例如更改配置或装饰对象。
        foreach ($this->getExtenders($abstract) as $extender) {
            $object = $extender($object, $this);
        }

        // If the requested type is registered as a singleton we'll want to cache off
        // the instances in "memory" so we can return it later without creating an
        // entirely new instance of an object on each subsequent request for it.
        // 如果将请求的类型注册为单例,我们将将实例缓存在“内存”中,
        // 这样​​我们以后就可以返回它,而无需在随后的每次请求上都创建对象的全新实例。
        if ($this->isShared($abstract) && ! $needsContextualBuild) {
            $this->instances[$abstract] = $object;
        }

        $this->fireResolvingCallbacks($abstract, $object);

        // Before returning, we will also set the resolved flag to "true" and pop off
        // the parameter overrides for this build. After those two things are done
        // we will be ready to return back the fully constructed class instance.
        // 在返回之前,我们还将解析标记设置为 “true”,并弹出此版本的参数替代。
        // 完成这两件事之后,我们将准备好返回完整构造的类实例。
        $this->resolved[$abstract] = true;

        array_pop($this->with);

        return $object;
    }

    /**
     * Get the concrete type for a given abstract.
     * 获取给定摘要的具体类型。
     *
     * @param  string  $abstract
     * @return mixed   $concrete
     */
    protected function getConcrete($abstract)
    {
        if (! is_null($concrete = $this->getContextualConcrete($abstract))) {
            return $concrete;
        }

        // If we don't have a registered resolver or concrete for the type, we'll just
        // assume each type is a concrete name and will attempt to resolve it as is
        // since the container should be able to resolve concretes automatically.
        // 如果我们没有该类型的注册解析器或具体对象,我们将假设每种类型都是具体名称,
        // 并尝试按原样解析它,因为容器应该能够自动解析具体对象。
        if (isset($this->bindings[$abstract])) {
            return $this->bindings[$abstract]['concrete'];
        }

        return $abstract;
    }

    /**
     * Get the contextual concrete binding for the given abstract.
     * 获取给定摘要的上下文具体绑定。
     *
     * @param  string  $abstract
     * @return string|null
     */
    protected function getContextualConcrete($abstract)
    {
        if (! is_null($binding = $this->findInContextualBindings($abstract))) {
            return $binding;
        }

        // Next we need to see if a contextual binding might be bound under an alias of the
        // given abstract type. So, we will need to check if any aliases exist with this
        // type and then spin through them and check for contextual bindings on these.
        // 接下来,我们需要查看是否可以在给定抽象类型的别名下绑定上下文绑定。
        // 因此,我们将需要检查此类型是否存在别名,然后旋转它们并检查这些别名的上下文绑定。
        if (empty($this->abstractAliases[$abstract])) {
            return;
        }

        foreach ($this->abstractAliases[$abstract] as $alias) {
            if (! is_null($binding = $this->findInContextualBindings($alias))) {
                return $binding;
            }
        }
    }

    /**
     * Find the concrete binding for the given abstract in the contextual binding array.
     * 在上下文绑定数组中找到给定摘要的具体绑定
     *
     * @param  string  $abstract
     * @return string|null
     */
    protected function findInContextualBindings($abstract)
    {
        if (isset($this->contextual[end($this->buildStack)][$abstract])) {
            return $this->contextual[end($this->buildStack)][$abstract];
        }
    }

    /**
     * Determine if the given concrete is buildable.
     * 确定给定的具体是否可构建。
     *
     * @param  mixed   $concrete
     * @param  string  $abstract
     * @return bool
     */
    protected function isBuildable($concrete, $abstract)
    {
        return $concrete === $abstract || $concrete instanceof Closure;
    }

    /**
     * Instantiate a concrete instance of the given type.
     * 实例化给定类型的具体实例。
     *
     * @param  string  $concrete
     * @return mixed
     *
     * @throws \Guanguans\Di\BindingResolutionException
     */
    public function build($concrete)
    {
        // If the concrete type is actually a Closure, we will just execute it and
        // hand back the results of the functions, which allows functions to be
        // used as resolvers for more fine-tuned resolution of these objects.
        // 如果具体类型实际上是 Closure,我们将直接执行它并返回函数的结果,
        // 这允许将函数用作解析器,以便对这些对象进行更精细的解析。
        if ($concrete instanceof Closure) {
            return $concrete($this, $this->getLastParameterOverride());
        }

        $reflector = new ReflectionClass($concrete);

        // If the type is not instantiable, the developer is attempting to resolve
        // an abstract type such as an Interface of Abstract Class and there is
        // no binding registered for the abstractions so we need to bail out.
        // 如果类型不是可实例化的,则开发人员正尝试解析抽象类型,
        // 例如 Abstract Class 的接口,并且没有为抽象注册任何绑定,因此我们需要纾困。
        if (! $reflector->isInstantiable()) {
            return $this->notInstantiable($concrete);
        }

        $this->buildStack[] = $concrete;

        $constructor = $reflector->getConstructor();

        // If there are no constructors, that means there are no dependencies then
        // we can just resolve the instances of the objects right away, without
        // resolving any other types or dependencies out of these containers.
        // 如果没有构造函数,则意味着没有依赖项,我们可以立即解析对象的实例,
        // 而无需从这些容器中解析任何其他类型或依赖项。
        if (is_null($constructor)) {
            array_pop($this->buildStack);

            return new $concrete;
        }

        $dependencies = $constructor->getParameters();

        // Once we have all the constructor's parameters we can create each of the
        // dependency instances and then use the reflection instances to make a
        // new instance of this class, injecting the created dependencies in.
        // 一旦有了所有构造函数的参数,就可以创建每个依赖实例,
        // 然后使用反射实例创建此类的新实例,将创建的依赖注入其中。
        $instances = $this->resolveDependencies(
            $dependencies
        );

        array_pop($this->buildStack);

        return $reflector->newInstanceArgs($instances);
    }

    /**
     * Resolve all of the dependencies from the ReflectionParameters.
     * 从 ReflectionParameters 解析所有依赖项。
     *
     * @param  array  $dependencies
     * @return array
     */
    protected function resolveDependencies(array $dependencies)
    {
        $results = [];

        foreach ($dependencies as $dependency) {
            // If this dependency has a override for this particular build we will use
            // that instead as the value. Otherwise, we will continue with this run
            // of resolutions and let reflection attempt to determine the result.
            // 如果此依赖项对此特定构建具有替代,我们将使用作为值。
            // 否则,我们将继续执行的分辨率,并让反射尝试确定结果。
            if ($this->hasParameterOverride($dependency)) {
                $results[] = $this->getParameterOverride($dependency);

                continue;
            }

            // If the class is null, it means the dependency is a string or some other
            // primitive type which we can not resolve since it is not a class and
            // we will just bomb out with an error since we have no-where to go.
            // 如果该类为 null,则表示依赖项是字符串或其他类型的原始类型,
            // 由于它不是类,因此我们无法解析,并且由于没有位置,我们将抛出错误去。
            $results[] = is_null($class = $dependency->getClass())
                            ? $this->resolvePrimitive($dependency)
                            : $this->resolveClass($dependency);
        }

        return $results;
    }

    /**
     * Determine if the given dependency has a parameter override from makeWith.
     * 确定给定的依赖项是否具有来自 makeWith 的参数重写。
     *
     * @param  \ReflectionParameter  $dependency
     * @return bool
     */
    protected function hasParameterOverride($dependency)
    {
        return array_key_exists(
            $dependency->name,
            $this->getLastParameterOverride()
        );
    }

    /**
     * Get a parameter override for a dependency.
     * 获取依赖项的参数覆盖。
     *
     * @param  \ReflectionParameter  $dependency
     * @return mixed
     */
    protected function getParameterOverride($dependency)
    {
        return $this->getLastParameterOverride()[$dependency->name];
    }

    /**
     * Get the last parameter override.
     * 获取最后一个参数覆盖。
     *
     * @return array
     */
    protected function getLastParameterOverride()
    {
        return count($this->with) ? end($this->with) : [];
    }

    /**
     * Resolve a non-class hinted primitive dependency.
     * 解析非类提示的原始依赖项。
     *
     * @param  \ReflectionParameter  $parameter
     * @return mixed
     *
     * @throws \Guanguans\Di\BindingResolutionException
     */
    protected function resolvePrimitive(ReflectionParameter $parameter)
    {
        if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
            return $concrete instanceof Closure ? $concrete($this) : $concrete;
        }

        if ($parameter->isDefaultValueAvailable()) {
            return $parameter->getDefaultValue();
        }

        $this->unresolvablePrimitive($parameter);
    }

    /**
     * Resolve a class based dependency from the container.
     * 解析容器中基于类的依赖关系。
     *
     * @param  \ReflectionParameter  $parameter
     * @return mixed
     *
     * @throws \Guanguans\Di\BindingResolutionException
     */
    protected function resolveClass(ReflectionParameter $parameter)
    {
        try {
            return $this->make($parameter->getClass()->name);
        }

        // If we can not resolve the class instance, we will check to see if the value
        // is optional, and if it is we will return the optional parameter value as
        // the value of the dependency, similarly to how we do this with scalars.
        // 如果无法解析类实例,则将检查该值是否为可选,如果为可选,
        // 们将返回可选参数 value 作为依赖项的值,类似于执行此操作的方式标量。
        catch (BindingResolutionException $e) {
            if ($parameter->isOptional()) {
                return $parameter->getDefaultValue();
            }

            throw $e;
        }
    }

    /**
     * Throw an exception that the concrete is not instantiable.
     * 抛出一个具体无法实例化的异常。
     *
     * @param  string  $concrete
     * @return void
     *
     * @throws \Guanguans\Di\BindingResolutionException
     */
    protected function notInstantiable($concrete)
    {
        if (! empty($this->buildStack)) {
            $previous = implode(', ', $this->buildStack);

            $message = "Target [$concrete] is not instantiable while building [$previous].";
        } else {
            $message = "Target [$concrete] is not instantiable.";
        }

        throw new BindingResolutionException($message);
    }

    /**
     * Throw an exception for an unresolvable primitive.
     * 为无法解析的原语抛出异常。
     *
     * @param  \ReflectionParameter  $parameter
     * @return void
     *
     * @throws \Guanguans\Di\BindingResolutionException
     */
    protected function unresolvablePrimitive(ReflectionParameter $parameter)
    {
        $message = "Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}";

        throw new BindingResolutionException($message);
    }

    /**
     * Register a new resolving callback.
     * 注册一个新的解析回调。
     *
     * @param  string    $abstract
     * @param  \Closure|null  $callback
     * @return void
     */
    public function resolving($abstract, Closure $callback = null)
    {
        if (is_string($abstract)) {
            $abstract = $this->getAlias($abstract);
        }

        if (is_null($callback) && $abstract instanceof Closure) {
            $this->globalResolvingCallbacks[] = $abstract;
        } else {
            $this->resolvingCallbacks[$abstract][] = $callback;
        }
    }

    /**
     * Register a new after resolving callback for all types.
     * 在解析所有类型的回调之后注册一个新的。
     *
     * @param  string   $abstract
     * @param  \Closure|null $callback
     * @return void
     */
    public function afterResolving($abstract, Closure $callback = null)
    {
        if (is_string($abstract)) {
            $abstract = $this->getAlias($abstract);
        }

        if ($abstract instanceof Closure && is_null($callback)) {
            $this->globalAfterResolvingCallbacks[] = $abstract;
        } else {
            $this->afterResolvingCallbacks[$abstract][] = $callback;
        }
    }

    /**
     * Fire all of the resolving callbacks.
     * 触发所有解析的回调。
     *
     * @param  string  $abstract
     * @param  mixed   $object
     * @return void
     */
    protected function fireResolvingCallbacks($abstract, $object)
    {
        $this->fireCallbackArray($object, $this->globalResolvingCallbacks);

        $this->fireCallbackArray(
            $object,
            $this->getCallbacksForType($abstract, $object, $this->resolvingCallbacks)
        );

        $this->fireAfterResolvingCallbacks($abstract, $object);
    }

    /**
     * Fire all of the after resolving callbacks.
     * 解决回调后,请执行所有操作。
     *
     * @param  string  $abstract
     * @param  mixed   $object
     * @return void
     */
    protected function fireAfterResolvingCallbacks($abstract, $object)
    {
        $this->fireCallbackArray($object, $this->globalAfterResolvingCallbacks);

        $this->fireCallbackArray(
            $object,
            $this->getCallbacksForType($abstract, $object, $this->afterResolvingCallbacks)
        );
    }

    /**
     * Get all callbacks for a given type.
     * 获取给定类型的所有回调。
     *
     * @param  string  $abstract
     * @param  object  $object
     * @param  array   $callbacksPerType
     *
     * @return array
     */
    protected function getCallbacksForType($abstract, $object, array $callbacksPerType)
    {
        $results = [];

        foreach ($callbacksPerType as $type => $callbacks) {
            if ($type === $abstract || $object instanceof $type) {
                $results = array_merge($results, $callbacks);
            }
        }

        return $results;
    }

    /**
     * Fire an array of callbacks with an object.
     * 触发带有对象的回调数组。
     *
     * @param  mixed  $object
     * @param  array  $callbacks
     * @return void
     */
    protected function fireCallbackArray($object, array $callbacks)
    {
        foreach ($callbacks as $callback) {
            $callback($object, $this);
        }
    }

    /**
     * Get the container's bindings.
     * 获取容器的绑定。
     *
     * @return array
     */
    public function getBindings()
    {
        return $this->bindings;
    }

    /**
     * Get the alias for an abstract if available.
     * 获取摘要的别名(如果有)。
     *
     * @param  string  $abstract
     * @return string
     *
     * @throws \LogicException
     */
    public function getAlias($abstract)
    {
        if (! isset($this->aliases[$abstract])) {
            return $abstract;
        }

        if ($this->aliases[$abstract] === $abstract) {
            throw new LogicException("[{$abstract}] is aliased to itself.");
        }

        return $this->getAlias($this->aliases[$abstract]);
    }

    /**
     * Get the extender callbacks for a given type.
     * 获取给定类型的扩展程序回调。
     *
     * @param  string  $abstract
     * @return array
     */
    protected function getExtenders($abstract)
    {
        $abstract = $this->getAlias($abstract);

        if (isset($this->extenders[$abstract])) {
            return $this->extenders[$abstract];
        }

        return [];
    }

    /**
     * Remove all of the extender callbacks for a given type.
     * 删除给定类型的所有扩展器回调。
     *
     * @param  string  $abstract
     * @return void
     */
    public function forgetExtenders($abstract)
    {
        unset($this->extenders[$this->getAlias($abstract)]);
    }

    /**
     * Drop all of the stale instances and aliases.
     * 删除所有过时的实例和别名。
     *
     * @param  string  $abstract
     * @return void
     */
    protected function dropStaleInstances($abstract)
    {
        unset($this->instances[$abstract], $this->aliases[$abstract]);
    }

    /**
     * Remove a resolved instance from the instance cache.
     * 从实例缓存中删除已解析的实例。
     *
     * @param  string  $abstract
     * @return void
     */
    public function forgetInstance($abstract)
    {
        unset($this->instances[$abstract]);
    }

    /**
     * Clear all of the instances from the container.
     * 从容器中清除所有实例。
     *
     * @return void
     */
    public function forgetInstances()
    {
        $this->instances = [];
    }

    /**
     * Flush the container of all bindings and resolved instances.
     * 刷新容器中的所有绑定和已解决的实例。
     *
     * @return void
     */
    public function flush()
    {
        $this->aliases = [];
        $this->resolved = [];
        $this->bindings = [];
        $this->instances = [];
        $this->abstractAliases = [];
    }

    /**
     * Set the globally available instance of the container.
     * 设置容器的全局可用实例。
     *
     * @return static
     */
    public static function getInstance()
    {
        if (is_null(static::$instance)) {
            static::$instance = new static;
        }

        return static::$instance;
    }

    /**
     * Set the shared instance of the container.
     * 设置容器的共享实例。
     *
     * @param  \Guanguans\Di\Container|null  $container
     * @return static
     */
    public static function setInstance(Container $container = null)
    {
        return static::$instance = $container;
    }

    /**
     * Determine if a given offset exists.
     * 确定是否存在给定的 key 。
     *
     * @param  string  $key
     * @return bool
     */
    public function offsetExists($key)
    {
        return $this->bound($key);
    }

    /**
     * Get the value at a given offset.
     * 获取给定 key 的值。
     *
     * @param  string  $key
     * @return mixed
     */
    public function offsetGet($key)
    {
        return $this->make($key);
    }

    /**
     * Set the value at a given offset.
     * 将值设置为给定的 key 。
     *
     * @param  string  $key
     * @param  mixed   $value
     * @return void
     */
    public function offsetSet($key, $value)
    {
        $this->bind($key, $value instanceof Closure ? $value : function () use ($value) {
            return $value;
        });
    }

    /**
     * Unset the value at a given offset.
     * 以给定 key 取消设置值。
     *
     * @param  string  $key
     * @return void
     */
    public function offsetUnset($key)
    {
        unset($this->bindings[$key], $this->instances[$key], $this->resolved[$key]);
    }

    /**
     * Dynamically access container services.
     * 动态访问容器服务。
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this[$key];
    }

    /**
     * Dynamically set container services.
     * 动态设置容器服务。
     *
     * @param  string  $key
     * @param  mixed   $value
     * @return void
     */
    public function __set($key, $value)
    {
        $this[$key] = $value;
    }

    /**
     * 通过其标识符查找容器的条目并返回它。
     *
     * @inheritDoc
     */
    public function has($id)
    {
        return $this->offsetExists($id);
    }

    /**
     * 如果容器可以返回给定标识符的条目,则返回 true 。 否则返回 false 。
     *
     * @inheritDoc
     */
    public function get($id)
    {
        return $this[$id];
    }
}
查看原文

赞 3 收藏 2 评论 0

guanguans 赞了文章 · 2020-10-19

PhpStorm在使用Yii框架开发的时候程序崩溃假死的解决办法

当你的 PhpStorm 在开发Yii框架相关程序的时候,我们有的人会启用一个插件 Github:Yii2support,但是当Phpstorm开启这个插件的时候,每当我们的代码写道 ::class的时候,Phpstorm都会假死。

其实这个插件的作者已经解决了这个办法,但是这个版本的插件还是测试版,大家可以去这里下载:https://github.com/nvlad/yii2...,或者直接下载 yii2support-0.10.57.24.jar.zip , 当前2020-10-15日的最新版本还是 0.10.57.23,作者说了这个是测试版。

怎么从本地安装插件,自己百度或者摸索去,很简单,我就不多说了。使用后,假死的情况的确没有了。终于解决了一年多了的问题,都差点儿有心理阴影了。

查看原文

赞 2 收藏 1 评论 0

认证与成就

  • 获得 751 次点赞
  • 获得 5 枚徽章 获得 0 枚金徽章, 获得 0 枚银徽章, 获得 5 枚铜徽章

擅长技能
编辑

开源项目 & 著作
编辑

(゚∀゚ )
暂时没有

注册于 2016-10-20
个人主页被 3.7k 人浏览