Beispiel #1
0
 function container($get = null)
 {
     if ($get === null) {
         return Proxy::container();
     } else {
         return call_user_func_array(array(Proxy::container(), 'get'), func_get_args());
     }
 }
Beispiel #2
0
 /**
  * Bootstrap Kernel
  * Set base definitions
  * Set base aliases
  * @return \YF\Core\Kernel
  */
 public function __construct($loader = null)
 {
     // set definitions
     $this->definitions = array('cache' => array(function ($container, $group = null) {
         if ($group === 'proxy') {
             $instance = new \YF\Core\Cache(null, null);
             $instance->container($container);
             return $instance;
         }
         $config = $container->get('config')->group('cache');
         if ($group === null) {
             $group = $config->key('groups', 0);
         }
         if ($instance = $container->get('cache.' . $group)) {
             return $instance;
         } else {
             $groupConfig = $config->get('groups.' . $group);
             $adapterConfig = $config->get('adapters.' . $groupConfig['adapter']);
             $adapter = new $adapterConfig['class']($adapterConfig['params']);
             $instance = new \YF\Core\Cache($group, $adapter, $groupConfig);
             $container->set('cache.' . $group, $instance);
             return $instance;
         }
     }, false), 'config' => array(function ($container) {
         $instance = new \YF\Core\Reader($container->get('loader'), $container->get('vars'));
         $instance->path($container->get('config.path'));
         return $instance;
     }, true), 'controller' => array(function ($container, $name) {
         if ($name === 'proxy') {
             $instance = new \YF\Core\Controller(null);
             $instance->container($container);
             return $instance;
         }
         if (strpos($name, 'Controller\\') === false) {
             $class = '\\Controller\\' . ucfirst($name);
         } else {
             $class = $name;
         }
         $instance = new $class($container->get('vars'));
         $instance->container($container);
         return $instance;
     }, false), 'cookie' => array(function ($container) {
         return new \YF\Core\Cookie($_COOKIE, $container->get('request'), $container->get('cookie.salt'));
     }, true), 'database' => array(function ($container, $connection = null) {
         $config = $container->get('config')->group('database');
         if ($connection === null || $connection === 'proxy') {
             $connection = $config->key(0);
         }
         if ($instance = $container->get('database.' . $connection)) {
             return $instance;
         } else {
             $config = $config->get($connection);
             $instance = new \YF\Core\Database(new \YF\Core\Database\Query(new $config['class']($config['params']), ''), new \YF\Core\Database\Raw(null));
             $instance->container($container);
             $container->set('database.' . $connection, $instance);
             return $instance;
         }
     }, false), 'encryption' => array(function ($container) {
         return new \YF\Core\Encryption($container->get('config')->group('encryption')->get());
     }, true), 'exception.handler' => array(function ($container) {
         return new \YF\Core\Exception\Handler();
     }, true), 'helper' => array(function ($container, $name) {
         //$class = '\\Helper\\'.ucfirst($name);
         //return new $class;
     }, false), 'loader' => array($loader, true), 'log' => array(function ($container, $adapter = null) {
         $config = $container->get('config')->group('log');
         if ($adapter === null || $adapter === 'proxy') {
             $adapter = $config->key(0);
         }
         if ($instance = $container->get('log.' . $adapter)) {
             return $instance;
         } else {
             $config = $config->get($adapter);
             $instance = new \YF\Core\Log(new $config['class']($config['params']));
             $instance->container($container);
             $container->set('log.' . $adapter, $instance);
             return $instance;
         }
     }, false), 'response' => array(function ($container) {
         return new \YF\Core\HTTP\Response();
     }, true), 'request' => array(function ($container) {
         return new \YF\Core\HTTP\Request($_SERVER, $_GET, $_POST);
     }, true), 'route' => array(function ($container, $pattern = null, $callback = null) {
         if ($pattern === null || $pattern === 'proxy') {
             return new \YF\Core\Route(null, null);
         }
         return new \YF\Core\Route($pattern, $callback);
     }, false), 'router' => array(function ($container) {
         $instance = new \YF\Core\Router();
         $instance->request($container->get('request'));
         return $instance;
     }, true), 'session' => array(function ($container, $driver = null) {
         $config = $container->get('config')->group('session');
         if ($driver === null || $driver === 'proxy') {
             $driver = $config->key(0);
         }
         if ($instance = $container->get('session.' . $driver)) {
             return $instance;
         } else {
             $config = $config->get($driver);
             switch ($driver) {
                 case 'database':
                     $instance = new \YF\Core\Session(new $config['class']($config['params'], $container->get('database', $config['database']), $container->get('cookie'), $container->get('encryption')));
                     break;
                 case 'native':
                     $instance = new \YF\Core\Session(new $config['class']($config['params'], $container->get('cookie'), $container->get('encryption')));
                     break;
             }
             $instance->container($container);
             $container->set('session.' . $driver, $instance);
             return $instance;
         }
     }, false), 'url' => array(function ($container) {
         $instance = new \YF\Core\URL();
         $request = $container->get('request');
         $instance->protocol($request->protocol());
         $instance->domain($request->domain());
         $instance->port($request->port());
         $instance->path($request->path());
         $instance->router($container->get('router'));
         return $instance;
     }, false), 'vars' => array(function ($container) {
         return new \YF\Core\Vars();
     }, false), 'view' => array(function ($container) {
         $instance = new \YF\Core\View('', array());
         $instance->path($this->get('view.path'));
         $instance->loader($this->get('loader'));
         return $instance;
     }, false));
     // set proxys
     $proxys = array(array('Cache', '\\YF\\Core\\Proxy\\Cache', 'cache'), array('Config', '\\YF\\Core\\Proxy\\Config', 'config'), array('Cookie', '\\YF\\Core\\Proxy\\Cookie', 'cookie'), array('Controller', '\\YF\\Core\\Proxy\\Controller', 'controller'), array('DB', '\\YF\\Core\\Proxy\\Database', 'database'), array('Entity', '\\YF\\Core\\Proxy\\Entity', 'entity'), array('Helper', '\\YF\\Core\\Proxy\\Helper', 'helper'), array('Log', '\\YF\\Core\\Proxy\\Log', 'log'), array('Mapper', '\\YF\\Core\\Proxy\\Mapper', 'mapper'), array('Router', '\\YF\\Core\\Proxy\\Router', 'router'), array('Route', '\\YF\\Core\\Proxy\\Route', 'route'), array('Session', '\\YF\\Core\\Proxy\\Session', 'session'), array('URL', '\\YF\\Core\\Proxy\\URL', 'url'), array('View', '\\YF\\Core\\Proxy\\View', 'view'));
     foreach ($proxys as $proxy) {
         $this->proxy($proxy[0], $proxy[1], $proxy[2]);
     }
     // set container in proxy class
     \YF\Core\Proxy::container($this);
     // inject router
     $this->router($this->get('router'));
     // inject response
     $this->response($this->get('response'));
     // additional definitions
     $this->register($this);
 }