Example #1
0
 /**
  * Register services
  *
  * @param DiInterface $di
  */
 public final function registerServices(DiInterface $di)
 {
     $module = $this->module;
     $baseControllers = $this->baseControllers;
     //Registering a dispatcher
     $di->set('dispatcher', function () use($di, &$module, &$baseControllers) {
         //Create new Dispatcher
         $dispatcher = new PDispatcher();
         //Set default namespace to this module
         $dispatcher->setModuleName($this->module);
         //Set default namespace
         $dispatcher->setDefaultNamespace($baseControllers);
         //Set default controller
         $dispatcher->setDefaultController($this->defaultController);
         /**
          * Get Event Manager
          *
          * @var \Phalcon\Events\Manager $eventsManager
          */
         $eventsManager = $di->getShared('eventsManager');
         //Attach acl in dispatcher
         $eventsManager->attach('dispatch', $di->get('acl'));
         //Set event manager
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     //Register the view component
     $di->set('view', function () use($di, &$module) {
         //Create Phalcon\Mvc\View
         $view = new ZView();
         $template = new ZTemplate($this->module);
         //Attach event
         $eventsManager = $di->getShared('eventsManager');
         if (method_exists($eventsManager, 'attach')) {
             $eventsManager->attach('view:beforeRender', $template);
             $eventsManager->attach('view:afterRender', $template);
             $eventsManager->attach('view:beforeRenderView', $template);
         } else {
             die(__FILE__ . ' Error: ZCMS cannot render template');
         }
         //Set view Event
         $view->setEventsManager($eventsManager);
         //Set view dir
         $view->setViewsDir(APP_DIR . '/backend/' . $module . '/views/');
         //Register engines
         $view->registerEngines(['.volt' => function ($view, $di) {
             $volt = new Volt($view, $di);
             $volt->setOptions(['compiledPath' => function ($templatePath) {
                 $templatePath = strstr($templatePath, '/app');
                 $dirName = dirname($templatePath);
                 if (!is_dir(ROOT_PATH . '/cache/volt' . $dirName)) {
                     mkdir(ROOT_PATH . '/cache/volt' . $dirName, 0755, TRUE);
                 }
                 return ROOT_PATH . '/cache/volt' . $dirName . '/' . basename($templatePath, '.volt') . '.php';
             }, 'compileAlways' => method_exists($di, 'get') ? (bool) $di->get('config')->backendTemplate->compileTemplate : false]);
             $compiler = $volt->getCompiler();
             $compiler->addFunction('get_sidebar', 'get_sidebar');
             $compiler->addFunction('__', '__');
             $compiler->addFunction('strtotime', 'strtotime');
             $compiler->addFunction('human_timing', 'human_timing');
             $compiler->addFunction('moneyFormat', 'moneyFormat');
             $compiler->addFunction('number_format', 'number_format');
             $compiler->addFunction('change_date_format', 'change_date_format');
             $compiler->addFunction('in_array', 'in_array');
             return $volt;
         }]);
         return $view;
     }, true);
 }
Example #2
0
 /**
  * Register services
  *
  * @param DiInterface $di
  */
 public function registerServices(DiInterface $di)
 {
     $module = $this->module;
     $baseControllers = $this->baseControllers;
     //Registering a dispatcher
     $di->set('dispatcher', function () use($di, &$module, &$baseControllers) {
         /**
          * @var \Phalcon\Events\ManagerInterface $eventsManager
          */
         $eventsManager = $di->getShared('eventsManager');
         $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
             /**
              * @var Dispatcher $dispatcher
              * @var \Phalcon\Mvc\Dispatcher\Exception $exception
              */
             switch ($exception->getCode()) {
                 case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                 case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                     $dispatcher->forward(['module' => 'index', 'controller' => 'error', 'action' => 'notFound404']);
                     return false;
             }
             return true;
         });
         $dispatcher = new Dispatcher();
         //Set default namespace to this module
         $dispatcher->setDefaultNamespace($baseControllers);
         $dispatcher->setEventsManager($eventsManager);
         $dispatcher->setModuleName($module);
         return $dispatcher;
     });
     //Registering the view component
     $di->set('view', function () use($di, &$module) {
         $view = new View();
         $view->setViewsDir(APP_DIR . '/frontend/' . $module . '/views/');
         /**
          * @var \Phalcon\Events\Manager $eventsManager
          */
         $eventsManager = $di->getShared('eventsManager');
         $eventsManager->attach('view:beforeRender', new ZFrontTemplate($this->module));
         $eventsManager->attach('view:afterRender', new ZFrontTemplate($this->module));
         //Set view Event
         $view->setEventsManager($eventsManager);
         $view->registerEngines(['.volt' => function ($view, $di) {
             $volt = new Volt($view, $di);
             $volt->setOptions(['compiledPath' => function ($templatePath) {
                 $templatePath = strstr($templatePath, '/app');
                 $dirName = dirname($templatePath);
                 if (!is_dir(ROOT_PATH . '/cache/volt' . $dirName)) {
                     mkdir(ROOT_PATH . '/cache/volt' . $dirName, 0755, true);
                 }
                 return ROOT_PATH . '/cache/volt' . $dirName . '/' . basename($templatePath, '.volt') . '.php';
             }, 'compileAlways' => method_exists($di, 'get') ? (bool) $di->get('config')->frontendTemplate->compileTemplate : false, 'stat' => false]);
             $compiler = $volt->getCompiler();
             $compiler->addFunction('get_sidebar', 'get_sidebar');
             $compiler->addFunction('__', '__');
             $compiler->addFilter('t', function ($resolvedArgs) {
                 return '__(' . $resolvedArgs . ')';
             });
             $compiler->addFunction('strtotime', 'strtotime');
             $compiler->addFunction('human_timing', 'human_timing');
             $compiler->addFunction('moneyFormat', 'moneyFormat');
             $compiler->addFunction('number_format', 'number_format');
             $compiler->addFunction('zcms_header', 'zcms_header');
             $compiler->addFunction('zcms_header_prefix', 'zcms_header_prefix');
             $compiler->addFunction('change_date_format', 'change_date_format');
             $compiler->addFunction('in_array', 'in_array');
             return $volt;
         }]);
         return $view;
     });
 }