当前位置:首页 > PHP教程 > php高级应用 > 列表

Pimple运行流程浅析(PHP容器)

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-26 10:48:24 浏览: 评论:0 

需要具备的知识点

闭包

闭包和匿名函数在PHP5.3.0中引入的。

闭包是指:

创建时封装周围状态的函数。即使闭包所处的环境不存在了,闭包中封装的状态依然存在。

理论上,闭包和匿名函数是不同的概念。但是PHP将其视作相同概念。

实际上,闭包和匿名函数是伪装成函数的对象。他们是Closure类的实例。

闭包和字符串、整数一样,是一等值类型。

创建闭包:

  1. <?php 
  2.  
  3. $closure = function ($name) { 
  4.  
  5.     return 'Hello ' . $name; 
  6.  
  7. }; 
  8.  
  9. echo $closure('nesfo');//Hello nesfo 
  10.  
  11. var_dump(method_exists($closure, '__invoke'));//true 

我们之所以能调用$closure变量,是因为这个变量的值是一个闭包,而且闭包对象实现了__invoke()魔术方法。只要变量名后有(),PHP就会查找并调用__invoke()方法。

通常会把PHP闭包当作函数的回调使用。

array_map(), preg_replace_callback()方法都会用到回调函数,这是使用闭包的最佳时机!

举个例子:

  1. <?php 
  2.  
  3. $numbersPlusOne = array_map(function ($number) { 
  4.  
  5.     return $number + 1; 
  6.  
  7. }, [1, 2, 3]); 
  8.  
  9. print_r($numbersPlusOne); 

得到结果:

[2, 3, 4]

在闭包出现之前,只能单独创建具名函数,然后使用名称引用那个函数,这么做,代码执行会稍微慢点,而且把回调的实现和使用场景隔离了。

  1. <?php 
  2.  
  3. function incrementNum ($number) { 
  4.  
  5.     return $number + 1; 
  6.  
  7. } 
  8.  
  9. $numbersPlusOne = array_map('incrementNum', [1, 2, 3]); 
  10.  
  11. print_r($numbersPlusOne); 

SPL

ArrayAccess

实现ArrayAccess接口,可以使得object像array那样操作,ArrayAccess接口包含四个必须实现的方法:

  1. interface ArrayAccess { 
  2.  
  3.     //检查一个偏移位置是否存在  
  4.  
  5.     public mixed offsetExists ( mixed $offset  ); 
  6.  
  7.  
  8.     //获取一个偏移位置的值  
  9.  
  10.     public mixed offsetGet( mixed $offset  ); 
  11.  
  12.  
  13.     //设置一个偏移位置的值  
  14.  
  15.     public mixed offsetSet ( mixed $offset  ); 
  16.  
  17.  
  18.     //复位一个偏移位置的值  
  19.  
  20.     public mixed offsetUnset  ( mixed $offset  ); 
  21.  
  22. } 

SplObjectStorage

SplObjectStorage类实现了以对象为键的映射(map)或对象的集合(如果忽略作为键的对象所对应的数据)这种数据结构。这个类的实例很像一个数组,但是它所存放的对象都是唯一。该类的另一个特点是,可以直接从中删除指定的对象,而不需要遍历或搜索整个集合。

::class语法

因为 ::class 表示是字符串,用 ::class 的好处在于 IDE 里面可以直接改名一个 class,然后 IDE 自动处理相关引用。

同时,PHP 执行相关代码时,是不会先加载相关 class 的。

同理,代码自动化检查 inspect 也可以正确识别 class。

Pimple容器流程浅析

Pimpl是php社区中比较流行的容器。代码不是很多,详见

https://github.com/silexphp/Pimple/blob/master/src/Pimple/Container.php 。

我们的应用可以基于Pimple开发:

  1. namespace EasyWeChat\Foundation; 
  2.  
  3. use Pimple\Container; 
  4.  
  5. class Application extends Container 
  6.  
  7. { 
  8.  
  9.     /** 
  10.  
  11.      * Service Providers. 
  12.  
  13.      * 
  14.  
  15.      * @var array 
  16.  
  17.      */ 
  18.  
  19.     protected $providers = [ 
  20.  
  21.         ServiceProviders\ServerServiceProvider::class, 
  22.  
  23.         ServiceProviders\UserServiceProvider::class 
  24.  
  25.     ]; 
  26.  
  27.     /** 
  28.  
  29.      * Application constructor. 
  30.  
  31.      * 
  32.  
  33.      * @param array $config 
  34.  
  35.      */ 
  36.  
  37.     public function __construct($config) 
  38.  
  39.     { 
  40.  
  41.         parent::__construct(); 
  42.  
  43.         $this['config'] = function () use ($config) { 
  44.  
  45.             return new Config($config); 
  46.  
  47.         }; 
  48.  
  49.         if ($this['config']['debug']) { 
  50.  
  51.             error_reporting(E_ALL); 
  52.  
  53.         } 
  54.  
  55.         $this->registerProviders(); 
  56.  
  57.     } 
  58.  
  59.     /** 
  60.  
  61.      * Add a provider. 
  62.  
  63.      * 
  64.  
  65.      * @param string $provider 
  66.  
  67.      * 
  68.  
  69.      * @return Application 
  70.  
  71.      */ 
  72.  
  73.     public function addProvider($provider) 
  74.  
  75.     { 
  76.  
  77.         array_push($this->providers, $provider); 
  78.  
  79.         return $this; 
  80.  
  81.     } 
  82.  
  83.     /** 
  84.  
  85.      * Set providers. 
  86.  
  87.      * 
  88.  
  89.      * @param array $providers 
  90.  
  91.      */ 
  92.  
  93.     public function setProviders(array $providers) 
  94.  
  95.     { 
  96.  
  97.         $this->providers = []; 
  98.  
  99.         foreach ($providers as $provider) { 
  100.  
  101.             $this->addProvider($provider); 
  102.  
  103.         } 
  104.  
  105.     } 
  106.  
  107.     /** 
  108.  
  109.      * Return all providers. 
  110.  
  111.      * 
  112.  
  113.      * @return array 
  114.  
  115.      */ 
  116.  
  117.     public function getProviders() 
  118.  
  119.     { 
  120.  
  121.         return $this->providers; 
  122.  
  123.     } 
  124.  
  125.     /** 
  126.  
  127.      * Magic get access. 
  128.  
  129.      * 
  130.  
  131.      * @param string $id 
  132.  
  133.      * 
  134.  
  135.      * @return mixed 
  136.  
  137.      */ 
  138.  
  139.     public function __get($id) 
  140.  
  141.     { 
  142.  
  143.         return $this->offsetGet($id); 
  144.  
  145.     } 
  146.  
  147.     /** 
  148.  
  149.      * Magic set access. 
  150.  
  151.      * 
  152.  
  153.      * @param string $id 
  154.  
  155.      * @param mixed  $value 
  156.  
  157.      */ 
  158.  
  159.     public function __set($id, $value) 
  160.  
  161.     { 
  162.  
  163.         $this->offsetSet($id, $value); 
  164.  
  165.     } 
  166.  
  167. } 

如何使用我们的应用:

$app = new Application([]);

$user = $app->user;

之后我们就可以使用$user对象的方法了,我们发现其实并没有$this->user这个属性,但是可以直接使用。主要是这两个方法起的作用:

public function offsetSet($id, $value){}

public function offsetGet($id){}

下面我们将解释在执行这两句代码,Pimple做了什么。但在解释这个之前,我们先看看容器的一些核心概念。

服务提供者

服务提供者是连接容器与具体功能实现类的桥梁。服务提供者需要实现接口ServiceProviderInterface:

  1. namespace Pimple; 
  2.  
  3. /** 
  4.  
  5.  * Pimple service provider interface. 
  6.  
  7.  * 
  8.  
  9.  * @author  Fabien Potencier 
  10.  
  11.  * @author  Dominik Zogg 
  12.  
  13.  */ 
  14.  
  15. interface ServiceProviderInterface 
  16.  
  17. { 
  18.  
  19.     /** 
  20.  
  21.      * Registers services on the given container. 
  22.  
  23.      * 
  24.  
  25.      * This method should only be used to configure services and parameters. 
  26.  
  27.      * It should not get services. 
  28.  
  29.      * 
  30.  
  31.      * @param Container $pimple A container instance 
  32.  
  33.      */ 
  34.  
  35.     public function register(Container $pimple); 
  36.  
  37. } 

所有服务提供者必须实现接口register方法。

我们的应用里默认有2个服务提供者:

  1. protected $providers = [ 
  2.  
  3.     ServiceProviders\ServerServiceProvider::class, 
  4.  
  5.     ServiceProviders\UserServiceProvider::class 
  6.  
  7. ]; 

以UserServiceProvider为例,我们看其代码实现:

  1. namespace EasyWeChat\Foundation\ServiceProviders; 
  2.  
  3. use EasyWeChat\User\User; 
  4.  
  5. use Pimple\Container; 
  6.  
  7. use Pimple\ServiceProviderInterface; 
  8.  
  9. /** 
  10.  
  11.  * Class UserServiceProvider. 
  12.  
  13.  */ 
  14.  
  15. class UserServiceProvider implements ServiceProviderInterface 
  16.  
  17. { 
  18.  
  19.     /** 
  20.  
  21.      * Registers services on the given container. 
  22.  
  23.      * 
  24.  
  25.      * This method should only be used to configure services and parameters. 
  26.  
  27.      * It should not get services. 
  28.  
  29.      * 
  30.  
  31.      * @param Container $pimple A container instance 
  32.  
  33.      */ 
  34.  
  35.     public function register(Container $pimple) 
  36.  
  37.     { 
  38.  
  39.         $pimple['user'] = function ($pimple) { 
  40.  
  41.             return new User($pimple['access_token']); 
  42.  
  43.         }; 
  44.  
  45.     } 
  46.  
  47. } 

我们看到,该服务提供者的注册方法会给容器增加属性user,但是返回的不是对象,而是一个闭包。这个后面我再做讲解。

服务注册

我们在Application里构造函数里使用$this->registerProviders();对所有服务提供者进行了注册:

  1. private function registerProviders() 
  2.  
  3. { 
  4.  
  5.     foreach ($this->providers as $provider) { 
  6.  
  7.         $this->register(new $provider()); 
  8.  
  9.     } 
  10.  
  11. } 

仔细看,我们发现这里实例化了服务提供者,并调用了容器Pimple的register方法:

  1. public function register(ServiceProviderInterface $provider, array $values = array()) 
  2.  
  3. { 
  4.  
  5.     $provider->register($this); 
  6.  
  7.     foreach ($values as $key => $value) { 
  8.  
  9.         $this[$key] = $value; 
  10.  
  11.     } 
  12.  
  13.     return $this; 
  14.  
  15. } 

而这里调用了服务提供者的register方法,也就是我们在上一节中提到的:注册方法给容器增加了属性user,但返回的不是对象,而是一个闭包。

当我们给容器Pimple添加属性user的同时,会调用offsetSet($id, $value)方法:给容器Pimple的属性values、keys分别赋值:

$this->values[$id] = $value;

$this->keys[$id] = true;

到这里,我们还没有实例化真正提供实际功能的类EasyWeChat\User\Usr,但已经完成了服务提供者的注册工作。

当我们运行到这里:

$user = $app->user;

会调用offsetGet($id)并进行实例化真正的类:

  1. $raw = $this->values[$id]; 
  2.  
  3. $val = $this->values[$id] = $raw($this); 
  4.  
  5. $this->raw[$id] = $raw; 
  6.  
  7. $this->frozen[$id] = true; 
  8.  
  9. return $val; 

$raw获取的是闭包:

  1. $pimple['user'] = function ($pimple) { 
  2.  
  3.     return new User($pimple['access_token']); 
  4.  
  5. }; 

$raw($this)返回的是实例化的对象User,也就是说只有实际调用才会去实例化具体的类。后面我们就可以通过$this['user']或者$this->user调用User类里的方法了。

当然,Pimple里还有很多特性值得我们去深入研究,这里不做过多讲解。

Tags: Pimple运行流程 PHP容器

分享到: