예제 #1
0
 /**
  * Create a view object for a determinated engine.
  *
  * @param  string $template view template filename
  * @param  array $data array containing view data
  * @param  string $engine view engine name
  * @throws \RuntimeException
  * @throws \Exception
  * @return \Alchemy\Mvc\ViewInterface $view created view object
  */
 protected function createView($template, array $data, $engine = '')
 {
     // creating config obj and setting it with all defaults configurations
     $conf = new \StdClass();
     $conf->template = $template;
     $conf->engine = empty($engine) ? $this->config->get('templating.default_engine') : $engine;
     $conf->templateDir = $this->config->get('app.view_templates_dir') . DS;
     $conf->layoutsDir = $this->config->get('app.view_layouts_dir') . DS;
     $conf->webDir = $this->config->get('app.web_dir');
     $conf->vendorDir = $this->config->get('app.vendor_dir');
     $conf->cacheDir = $this->config->get('templating.cache_dir') . DS;
     $conf->cacheEnabled = $this->config->get('templating.cache_enabled');
     $conf->extension = $this->config->get('templating.extension');
     $conf->charset = $this->config->get('templating.charset');
     $conf->debug = $this->config->get('templating.debug');
     $conf->assetsLocate = $this->config->getSection('assets_location');
     $conf->assetsPrecedence = explode(' ', $this->config->get('assets.precedence'));
     // File extension validation
     // A criteria can be if filename doesn't a period character (.)
     if (strpos($conf->template, '.') === false) {
         if ($this->config->isEmpty('templating.extension')) {
             switch ($conf->engine) {
                 case 'haanga':
                     $tplExtension = 'haanga';
                     break;
                 case 'twig':
                     $tplExtension = 'twig';
                     break;
                 case 'smarty':
                 default:
                     $tplExtension = 'tpl';
                     break;
             }
         } else {
             $tplExtension = $this->config->get('templating.extension');
         }
         $conf->template .= '.' . $tplExtension;
         // concatenate it with default extension from configuration
     }
     // read defaults bundles from configuration for desktop & mobile platform
     if ($this->request->isMobile()) {
         if ($this->config->exists('layout.mobile') && !$this->config->isEmpty('layout.mobile')) {
             if ($this->request->isIpad()) {
                 $layoutsDir = $conf->layoutsDir . $this->config->get('layout.mobile') . DS;
             } else {
                 $layoutsDir = $conf->layoutsDir . $this->config->get('layout.mobile') . DS;
             }
         } else {
             $layoutsDir = $conf->layoutsDir;
         }
     } else {
         if ($this->config->exists('layout.default') && !$this->config->isEmpty('layout.default')) {
             $layoutsDir = $conf->layoutsDir . $this->config->get('layout.default') . DS;
         } else {
             $layoutsDir = $conf->layoutsDir;
         }
     }
     // check if template file exists
     if (file_exists($conf->templateDir . $conf->template)) {
         // if relative path was given
     } elseif (file_exists($conf->layoutsDir . $conf->template)) {
         // if relative path was given
     } elseif (file_exists($layoutsDir . $conf->template)) {
         // if relative path was given
     } elseif (file_exists($conf->template)) {
         // if absolute path was given
     } else {
         // file doesn't exist, throw error
         throw new \RuntimeException("Error: File Not Found: template file doesn't exist: '{$conf->template}'", 404);
     }
     // composing the view class string
     $viewClass = '\\Alchemy\\Mvc\\Adapter\\' . ucfirst($conf->engine) . 'View';
     // check if view engine class exists, if does not exist throw an exception
     if (!class_exists($viewClass)) {
         throw new \RuntimeException(sprintf("Runtime Error: Template Engine is not available: '%s'", $conf->engine));
     }
     // resolving assets location
     $locateDirPrecedence = array();
     foreach ($conf->assetsPrecedence as $assetItem) {
         if (array_key_exists($assetItem, $conf->assetsLocate)) {
             $locateDirPrecedence[$assetItem] = $conf->assetsLocate[$assetItem];
         } else {
             $locateDirPrecedence[$assetItem] = $conf->webDir . '/assets/' . $assetItem;
         }
     }
     // create view object
     $view = new $viewClass($conf->template, $this->assetsHandler);
     // setup view object
     $view->enableDebug($conf->debug);
     $view->enableCache($conf->cacheEnabled);
     $view->setCacheDir($conf->cacheDir);
     $view->setCharset($conf->charset);
     // setting templates directories
     $view->setTemplateDir($conf->templateDir);
     $view->setTemplateDir($layoutsDir);
     $baseurl = '/' . $this->request->getBaseUrl();
     if (substr($baseurl, -4) == '.php') {
         $baseurl = substr($baseurl, 0, strrpos($baseurl, '/') + 1);
     } elseif (substr($baseurl, -1) !== '/') {
         $baseurl .= '/';
     }
     // setting data to be used by template
     $view->assign('baseurl', $baseurl);
     $view->assign($data);
     // setting & registering assets handler on view
     $view->assetsHandler->setBaseDir($conf->webDir);
     $view->assetsHandler->setLocateDir($locateDirPrecedence);
     $view->assetsHandler->setCacheDir($conf->cacheDir);
     $view->assetsHandler->setOutputDir('assets/compiled');
     $view->assetsHandler->setVendorDir($conf->vendorDir);
     return $view;
 }
예제 #2
0
 /**
  * Construct application object
  * @param array $conf
  * @throws \Exception
  * @internal param \Alchemy\Contains $config all app configuration
  */
 public function init($conf = array())
 {
     defined('DS') || define('DS', DIRECTORY_SEPARATOR);
     $app = $this;
     $this['logger'] = null;
     $this['config'] = function () use($conf, $app) {
         $config = new Config();
         $config->load($conf);
         if (!$config->exists('phpalchemy.root_dir')) {
             $config->set('phpalchemy.root_dir', realpath(__DIR__ . '/../'));
         }
         if (!empty($app->appDir)) {
             $config->set('app.root_dir', $app->getAppDir());
         }
         return $config;
     };
     // load configuration ini files
     $this->loadAppConfigurationFiles();
     // apply configurated php settings
     $this->applyPhpSettings();
     $this['autoloader'] = function () {
         return new ClassLoader();
     };
     $this['yaml'] = function () {
         return new Yaml();
     };
     $this['annotation'] = function () use($app) {
         return new NotojReader();
         //return new Annotations($app['config']);
     };
     $this['mapper'] = function () use($app) {
         $config = $app['config'];
         $routesDir = $config->get('app.root_dir') . DS . 'config' . DS;
         if (file_exists($routesDir . 'routes.php')) {
             $mapper = (include $routesDir . 'routes.php');
         } elseif (file_exists($routesDir . 'routes.yaml')) {
             $mapper = new Mapper($app['yaml']);
             $mapper->setCacheDir($config->get('app.cache_dir'));
             $mapper->loadFrom($routesDir . 'routes.yaml');
         } else {
             throw new \Exception("Application Error: No routes found for this app.\n" . "You need create & configure 'config/routes.yaml'");
         }
         return $mapper;
     };
     //TODO $this['exception_handler'] = $this->share(function () {
     //     return new ExceptionHandler();
     // });
     $this['dispatcher'] = function () use($app) {
         $dispatcher = new EventDispatcher();
         $dispatcher->addSubscriber($app);
         // subscribing events
         $dispatcher->addSubscriber(new EventListener\ResponseListener($app['config']->get('templating.charset')));
         //TODO $dispatcher->addSubscriber(new LocaleListener($app['locale'], $urlMatcher));
         return $dispatcher;
     };
     $this['resolver'] = function () use($app) {
         return new ControllerResolver($app, $app['logger']);
     };
     $this['ui_reader_factory'] = function () use($app) {
         return new ReaderFactory();
     };
     $this['ui_parser'] = function () use($app) {
         return new Parser();
     };
     /** @var \Alchemy\Component\UI\Engine */
     $this['ui_engine'] = function () use($app) {
         return new Engine($app['ui_reader_factory'], $app['ui_parser']);
     };
     $this['assetsHandler'] = function () use($app) {
         return new WebAssets\Bundle();
     };
     /** @var \Alchemy\Kernel\Kernel */
     $this['kernel'] = function () use($app) {
         return new Kernel($app['dispatcher'], $app['mapper'], $app['resolver'], $app['config'], $app['annotation'], $app['ui_engine'], $app['assetsHandler']);
     };
     // registering the aplication namespace to SPL ClassLoader
     $this['autoloader']->register($this['config']->get('app.name'), $this['config']->get('app.app_root_dir'));
     // registering the aplication Extend folder to SPL ClassLoader (if folder was created)
     if (is_dir($this['config']->get('app.root_dir') . DIRECTORY_SEPARATOR . 'Extend')) {
         $this['autoloader']->register($this['config']->get('app.name') . '/Extend', $this['config']->get('app.root_dir') . DIRECTORY_SEPARATOR . 'Extend');
     }
     $this['exception_handler'] = function () use($app) {
         return new ExceptionHandler();
     };
 }