我们的应用可以基于Pimple开发:
namespace EasyWeChatFoundation;
use PimpleContainer;
class Application extends Container
{
/**
* Service Providers.
*
* @var array
*/
protected $providers = [
ServiceProvidersServerServiceProvider::class,
ServiceProvidersUserServiceProvider::class
];
/**
* Application constructor.
*
* @param array $config
*/
public function __construct($config)
{
parent::__construct();
$this['config'] = function () use ($config) {
return new Config($config);
};
if ($this['config']['debug']) {
error_reporting(E_ALL);
}
$this->registerProviders();
}
/**
* Add a provider.
*
* @param string $provider
*
* @return Application
*/
public function addProvider($provider)
{
array_push($this->providers, $provider);
return $this;
}
/**
* Set providers.
*
* @param array $providers
*/
public function setProviders(array $providers)
{
$this->providers = [];
foreach ($providers as $provider) {
$this->addProvider($provider);
}
}
/**
* Return all providers.
*
* @return array
*/
public function getProviders()
{
return $this->providers;
}
/**
* Magic get access.
*
* @param string $id
*
* @return mixed
*/
public function __get($id)
{
return $this->offsetGet($id);
}
/**
* Magic set access.
*
* @param string $id
* @param mixed $value
*/
public function __set($id, $value)
{
$this->offsetSet($id, $value);
}
}如何使用我们的应用:
$app = new Application([]); $user = $app->user;
之后我们就可以使用$user对象的方法了。我们发现其实并没有$this->user这个属性,但是可以直接使用。主要是这两个方法起的作用:
public function offsetSet($id, $value){}
public function offsetGet($id){}下面我们将解释在执行这两句代码,Pimple做了什么。但在解释这个之前,我们先看看容器的一些核心概念。
服务提供者
服务提供者是连接容器与具体功能实现类的桥梁。服务提供者需要实现接口ServiceProviderInterface:
namespace Pimple;
/**
* Pimple service provider interface.
*
* @author Fabien Potencier
* @author Dominik Zogg
*/
interface ServiceProviderInterface
{
/**
* Registers services on the given container.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Container $pimple A container instance
*/
public function register(Container $pimple);
}所有服务提供者必须实现接口register方法。
我们的应用里默认有2个服务提供者:
protected $providers = [ ServiceProvidersServerServiceProvider::class, ServiceProvidersUserServiceProvider::class ];
以UserServiceProvider为例,我们看其代码实现:
namespace EasyWeChatFoundationServiceProviders;
use EasyWeChatUserUser;
use PimpleContainer;
use PimpleServiceProviderInterface;
/**
* Class UserServiceProvider.
*/
class UserServiceProvider implements ServiceProviderInterface
{
/**
* Registers services on the given container.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Container $pimple A container instance
*/
public function register(Container $pimple)
{
$pimple['user'] = function ($pimple) {
return new User($pimple['access_token']);
};
}
}我们看到,该服务提供者的注册方法会给容器增加属性user,但是返回的不是对象,而是一个闭包。这个后面我再做讲解。
服务注册
我们在Application里构造函数里使用$this->registerProviders();对所有服务提供者进行了注册:
private function registerProviders()
{
foreach ($this->providers as $provider) {
$this->register(new $provider());
}
}仔细看,我们发现这里实例化了服务提供者,并调用了容器Pimple的register方法:
public function register(ServiceProviderInterface $provider, array $values = array())
{
$provider->register($this);
foreach ($values as $key => $value) {
$this[$key] = $value;
}
return $this;
}而这里调用了服务提供者的register方法,也就是我们在上一节中提到的:注册方法给容器增加了属性user,但返回的不是对象,而是一个闭包。
当我们给容器Pimple添加属性user的同时,会调用offsetSet($id, $value)方法:给容器Pimple的属性values、keys分别赋值:
$this->values[$id] = $value; $this->keys[$id] = true;
到这里,我们还没有实例化真正提供实际功能的类EasyWeChatUserUsr。但已经完成了服务提供者的注册工作。
当我们运行到这里:
$user = $app->user;
会调用offsetGet($id)并进行实例化真正的类:
$raw = $this->values[$id]; $val = $this->values[$id] = $raw($this); $this->raw[$id] = $raw; $this->frozen[$id] = true; return $val;
$raw获取的是闭包:
$pimple['user'] = function ($pimple) {
return new User($pimple['access_token']);
};$raw($this)返回的是实例化的对象User。也就是说只有实际调用才会去实例化具体的类。后面我们就可以通过$this['user']或者$this->user调用User类里的方法了。
当然,Pimple里还有很多特性值得我们去深入研究,这里不做过多讲解。
更多相关php知识,请访问php教程!
下载本文