Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from reatang/master
Browse files Browse the repository at this point in the history
封装\Yaf\Register并添加启动过程
  • Loading branch information
overtrue authored Apr 12, 2018
2 parents 13f1943 + 009ece6 commit e4a2f3b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 34 deletions.
33 changes: 31 additions & 2 deletions app/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Yaf\Bootstrap_Abstract as YafBootstrap;
use Yaf\Dispatcher;
use Yaf\Registry as YafRegistry;
use \App\Services\Register;

/**
* Class Bootstrap.
Expand All @@ -20,6 +20,11 @@
*/
class Bootstrap extends YafBootstrap
{
/**
* @var Register
*/
protected $register;

/**
* 项目基本初始化操作.
*
Expand All @@ -43,6 +48,29 @@ public function _initLoader(Dispatcher $dispatcher)
$loader->import(ROOT_PATH.'/vendor/autoload.php');
}

/**
* init container
* @param Dispatcher $dispatcher
*/
public function _initContainer(Dispatcher $dispatcher)
{
// init Container
$this->register = $register = new Register();
$register->set(Register::class, $register);
$register->alias('services.register', $register);
}

/**
* init Facade
*
* @param Dispatcher $dispatcher
*/
public function _initFacade(Dispatcher $dispatcher)
{
// inject Register Container
Facade::init($this->register);
}

/**
* @param \Yaf\Dispatcher $dispatcher
*
Expand All @@ -65,7 +93,8 @@ public function _initLogger(Dispatcher $dispatcher)
// 请自己配置日志
//$log->setHandlers();

YafRegistry::set(\App\Services\Logger::class, $log);
$this->register->set(\App\Services\Logger::class, $log);
$this->register->alias('services.log', $log);
}

/**
Expand Down
20 changes: 18 additions & 2 deletions app/facades/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

use Mockery\MockInterface;
use \App\Services\Register;

/**
* 外观基类.
Expand All @@ -23,6 +24,21 @@ abstract class Facade
*/
protected static $resolvedInstance;

/**
* @var Register
*/
protected static $container;

/**
* start Facade
*
* @param Register $register
*/
public static function init(Register $register)
{
static::$container = $register;
}

/**
* Hotswap the underlying instance behind the facade.
*
Expand Down Expand Up @@ -135,8 +151,8 @@ protected static function resolveFacadeInstance($name)
return static::$resolvedInstance[$name];
}

if (\Yaf\Registry::has($name)) {
return \Yaf\Registry::get($name);
if (static::$container->has($name)) {
return static::$container->get($name);
}

return static::$resolvedInstance[$name] = new $name();
Expand Down
38 changes: 9 additions & 29 deletions app/facades/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,22 @@

/**
* class Registry.
*
* @method static get(string $name): mixed
* @method static set(string $name, mixed $instance): mixed
* @method static has(string $name): bool
*/
class Registry extends Facade
{
/**
* 允许的命名空间.
*/
const NAMESPACES = ['services', 'session', 'setting', 'routing', 'http', 'testing'];

/**
* 设定变量.
*
* @param string $key
* @param mixed $value
*
* @return mixed
*/
public static function set($key, $value)
{
$prefix = strstr($key, '.', true);

if (!in_array($prefix, self::NAMESPACES)) {
throw new InvalidArgumentException("不合法的命名空间:$prefix");
}

return YafRegistry::set($key, $value);
}

/**
* 读取.
* Get the registered name of the component.
*
* @param string $key
* @throws \RuntimeException
*
* @return mixed
* @return string
*/
public static function get($key)
protected static function getFacadeAccessor()
{
return YafRegistry::get($key);
return 'services.register';
}
}
75 changes: 75 additions & 0 deletions app/services/Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Created by PhpStorm.
* User: reatang
* Date: 18/4/10
* Time: 下午11:29
*/

namespace App\Services;


use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Yaf\Registry as YafRegister;

/**
* Register Container
*
* @package App\Services
*/
class Register implements ContainerInterface
{
/**
* set instance
*
* @param $name
* @param $instance
*
* @return $this
*/
public function set($name, $instance)
{
YafRegister::set($name, $instance);

return $this;
}

/**
* alias name
*
* @param $name
* @param $instance
*
* @return Register
*/
public function alias($name, $instance)
{
return $this->set($name, $instance);
}

/**
* get instance
*
* @param string $name
*
* @return mixed
*/
public function get($name)
{
return YafRegister::get($name);
}

/**
* instance isset
*
* @param string $name
*
* @return bool
*/
public function has($name)
{
return YafRegister::has($name);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"psr/http-message": "~1.0",
"symfony/console": "3.*",
"monolog/monolog": "~1.0",
"league/plates": "~3.0"
"league/plates": "~3.0",
"psr/container": "^1.0"
},
"require-dev": {
"mockery/mockery": "~1.0",
Expand Down

0 comments on commit e4a2f3b

Please sign in to comment.