示例#1
0
 /**
  * {@inheritdoc}
  */
 public function get($id)
 {
     if (!$this->has($id)) {
         throw new Exception\ServiceNotFound("Service `{$id}` not found in the container.");
     }
     try {
         return $this->di->getShared($id);
     } catch (DI\Exception $e) {
         throw new Exception\ContainerError("Cannot retrieve `{$id}` from the container.", 0, $e);
     }
 }
 /**
  * @param DI $di
  */
 public function registerServices($di)
 {
     //Registering a dispatcher
     $di->setShared('dispatcher', function () use($di) {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Admin");
         $eventsManager = $di->getShared('eventsManager');
         $eventsManager->attach('dispatch', new AdminSecurity($di));
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     $auto_admin = new \AutoAdmin\Module();
     $auto_admin->registerServices($di);
     $di->setShared('admin_views_dir', function () {
         return ADMINROOT . '/views/';
     });
     $di->setShared('session', function () {
         $session = new Session();
         $session->start();
         return $session;
     });
     $di->setShared('config', function () use($di) {
         $configFront = (require COREROOT . '/app/config/config.php');
         $configBack = (require ADMINROOT . '/config/config.php');
         $configFront->merge($configBack);
         return $configFront;
     });
 }
 /**
  * Registration services for specific module
  * @param \Phalcon\DI $di
  * @access public
  * @return mixed
  */
 public function registerServices($di)
 {
     // Dispatch register
     $di->set('dispatcher', function () use($di) {
         $eventsManager = $di->getShared('eventsManager');
         $eventsManager->attach('dispatch:beforeException', new \Plugins\Dispatcher\NotFoundPlugin());
         $dispatcher = new \Phalcon\Mvc\Dispatcher();
         $dispatcher->setEventsManager($eventsManager);
         $dispatcher->setDefaultNamespace('Modules\\' . self::MODULE . '\\Controllers');
         return $dispatcher;
     }, true);
     // Registration of component representations (Views)
     $di->set('view', function () {
         $view = new View();
         $view->setViewsDir($this->_config['application']['viewsBack'])->setMainView('auth-layout')->setPartialsDir('partials');
         return $view;
     });
     require_once APP_PATH . '/Modules/' . self::MODULE . '/config/services.php';
     // call profiler
     if ($this->_config->database->profiler === true) {
         new \Plugins\Debugger\Develop($di);
     }
     if (APPLICATION_ENV == 'development') {
         // share Fabfuel topbar
         $profiler = new \Fabfuel\Prophiler\Profiler();
         $di->setShared('profiler', $profiler);
         $pluginManager = new \Fabfuel\Prophiler\Plugin\Manager\Phalcon($profiler);
         $pluginManager->register();
         // add toolbar in your basic BaseController
     }
     return;
 }
 /**
  * Registration services for specific module
  * @param \Phalcon\DI $di
  * @access public
  * @return mixed
  */
 public function registerServices($di)
 {
     // Dispatch register
     $di->set('dispatcher', function () use($di) {
         $eventsManager = $di->getShared('eventsManager');
         $eventsManager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) {
             switch ($exception->getCode()) {
                 case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                 case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                     $dispatcher->forward(['module' => self::MODULE, 'namespace' => 'Modules\\' . self::MODULE . '\\Controllers\\', 'controller' => 'error', 'action' => 'notFound']);
                     return false;
                     break;
                 default:
                     $dispatcher->forward(['module' => self::MODULE, 'namespace' => 'Modules\\' . self::MODULE . '\\Controllers\\', 'controller' => 'error', 'action' => 'uncaughtException']);
                     return false;
                     break;
             }
         });
         $dispatcher = new \Phalcon\Mvc\Dispatcher();
         $dispatcher->setEventsManager($eventsManager);
         $dispatcher->setDefaultNamespace('Modules\\' . self::MODULE . '\\Controllers');
         return $dispatcher;
     }, true);
     // Registration of component representations (Views)
     $di->set('view', function () {
         $view = new View();
         $view->setViewsDir($this->_config['application']['viewsFront'])->setMainView('layout');
         return $view;
     });
     return require_once APP_PATH . '/Modules/' . self::MODULE . '/config/services.php';
 }
示例#5
0
 /**
  * @depends testProfilerManagerAttachesDataCollectorsToVariousEvents
  */
 public function testProfilersMustBeExecutedBeforeSerialization()
 {
     $profiler = $this->di->get(ProfilerDataCollector::DI_NAME);
     $eventsManager = $this->di->getShared('eventsManager');
     $profiler->enable($this->getDataCollectors());
     try {
         json_encode($profiler);
         $this->fail();
     } catch (\Exception $e) {
         do {
             $e = $e->getPrevious();
         } while ($e && $e->getPrevious());
         $this->assertInstanceOf('\\Vegas\\Profiler\\Exception\\EventNotTriggeredException', $e);
     }
     $eventsManager->fire('db:beforeQuery', $this->di->getShared('db'));
     $this->di->getShared('db')->execute('SELECT 1');
     $eventsManager->fire('db:afterQuery', $this->di->getShared('db'));
     $eventsManager->fire('dispatch:beforeException', $this->di->getShared('dispatcher'), new \Vegas\Exception());
     $eventsManager->fire('dispatch:afterDispatchLoop', $this->di->getShared('dispatcher'));
     $eventsManager->fire('view:afterRender', $this->di->getShared('view'));
     $result = json_encode($profiler);
     $this->assertJson($result);
 }
示例#6
0
 /**
  * Return the config object in the services container
  *
  * @return \Phalcon\Mvc\Url
  */
 public static function getConnection()
 {
     return self::$di->getShared('db');
 }
 /**
  * @param $view
  *
  * @throws \DebugBar\DebugBarException
  */
 public function attachView($view)
 {
     if (!$this->shouldCollect('view', true) || isset($this->collectors['views'])) {
         return;
     }
     // You can add only One View instance
     if (is_string($view)) {
         $view = $this->di[$view];
     }
     $engins = $view->getRegisteredEngines();
     if (isset($engins['.volt'])) {
         $volt = $engins['.volt'];
         if (is_object($volt)) {
             if ($volt instanceof \Closure) {
                 $volt = $volt($view, $this->di);
             }
         } elseif (is_string($volt)) {
             if (class_exists($volt)) {
                 $volt = new Volt($view, $this->di);
             } elseif ($this->di->has($volt)) {
                 $volt = $this->di->getShared($volt, array($view, $this->di));
             }
         }
         $engins['.volt'] = $volt;
         $view->registerEngines($engins);
         $volt->getCompiler()->addExtension(new VoltFunctions($this->di));
     }
     $viewProfiler = new Registry();
     $viewProfiler->templates = array();
     $viewProfiler->engines = $view->getRegisteredEngines();
     $config = $this->config;
     $eventsManager = $view->getEventsManager();
     if (!is_object($eventsManager)) {
         $eventsManager = new Manager();
     }
     $eventsManager->attach('view:beforeRender', function ($event, $view) use($viewProfiler) {
         $viewProfiler->startRender = microtime(true);
     });
     $eventsManager->attach('view:afterRender', function ($event, $view) use($viewProfiler, $config) {
         $viewProfiler->stopRender = microtime(true);
         if ($config->options->views->get('data', false)) {
             $viewProfiler->params = $view->getParamsToView();
         } else {
             $viewProfiler->params = null;
         }
     });
     $eventsManager->attach('view:beforeRenderView', function ($event, $view) use($viewProfiler) {
         $viewFilePath = $view->getActiveRenderPath();
         if (Version::getId() >= 2000140) {
             if (!$view instanceof \Phalcon\Mvc\ViewInterface && $view instanceof \Phalcon\Mvc\ViewBaseInterface) {
                 $viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
             }
         } elseif ($view instanceof Simple) {
             $viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
         }
         $templates = $viewProfiler->templates;
         $templates[$viewFilePath]['startTime'] = microtime(true);
         $viewProfiler->templates = $templates;
     });
     $eventsManager->attach('view:afterRenderView', function ($event, $view) use($viewProfiler) {
         $viewFilePath = $view->getActiveRenderPath();
         if (Version::getId() >= 2000140) {
             if (!$view instanceof \Phalcon\Mvc\ViewInterface && $view instanceof \Phalcon\Mvc\ViewBaseInterface) {
                 $viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
             }
         } elseif ($view instanceof Simple) {
             $viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
         }
         $templates = $viewProfiler->templates;
         $templates[$viewFilePath]['stopTime'] = microtime(true);
         $viewProfiler->templates = $templates;
     });
     $view->setEventsManager($eventsManager);
     $collector = new ViewCollector($viewProfiler, $view);
     $this->addCollector($collector);
 }