public function initRPC($processor) { $socket_tranport = new \Thrift\Server\TServerSocket(Di::get('config')['host'], Di::get('config')['port']); $out_factory = $in_factory = new \Thrift\Factory\TFramedTransportFactory(); $out_protocol = $in_protocol = new \Thrift\Factory\TBinaryProtocolFactory(); $server = new \Swoole\Thrift\Server($processor, $socket_tranport, $in_factory, $out_factory, $in_protocol, $out_protocol); Di::set('receive', [$server, 'onReceive']); }
public function initTwig() { $config = DI::get('Config'); // Add custom namespace path to Imagine lib $vendorDir = $config->get('site.path') . '/../vendor'; $autoload = (require $vendorDir . '/autoload.php'); $autoload->add('Twig_', __DIR__ . '/vendor/twig/lib'); $this->twig = new Twig($config); $this->twig->init(); Di::set('Twig', $this->twig); Hook::trigger(Hook::ACTION, 'twigInitialized', $this->twig->getEnvironment()); }
{ public $name; public $age; public function __construct($name = '') { $this->name = $name; } } include "Di.class.php"; $di = new Di(); //匿名函数方式注册一个a1服务 $di->setShared('a1', function ($name = '') { return new A($name); }); //直接以类名的方式注册 $di->set('a2', 'A'); //直接传入实例化的对象 $di->set('a3', new A('小超')); $a1 = $di->get('a1', array('小李')); echo $a1->name, "<br/>"; $a1_1 = $di->get('a1', array('小王')); echo $a1->name, "<br/>"; echo $a1_1->name, "<br/>"; $a2 = $di->get('a2', array("小张")); echo $a2->name . "<br/>"; //小张 $a2_1 = $di->get('a2', array("小徐")); echo $a2->name . "<br/>"; //小张 echo $a2_1->name . "<br/>"; //小徐