public function run($viewContent, array $options = array()) { $extendsBlock = '{% extends "' . self::DEFAULT_LAYOUT . '" %}'; $contentStartBlock = '{% block content %}'; $contentEndBlock = '{% endblock %}'; $viewContent = $extendsBlock . $contentStartBlock . $viewContent . $contentEndBlock; if (array_key_exists('layout', $options)) { $layoutFileName = $options['layout']; } else { $layoutFileName = self::DEFAULT_LAYOUT; } $layoutFilePath = Application::getBasePath() . DIRECTORY_SEPARATOR . Application::getConfigItem('viewsFolder') . DIRECTORY_SEPARATOR . $layoutFileName; if (!file_exists($layoutFilePath)) { throw new \Exception('layout file could not be found'); } $layoutContent = file_get_contents($layoutFilePath); $this->twigLoaderArray = new \Twig_Loader_Array(['layout.html' => $layoutContent, 'view.html' => $viewContent]); $loader = new \Twig_Loader_Chain([$this->twigLoaderFileSystem, $this->twigLoaderArray]); $twig = new \Twig_Environment($loader); $twig->registerUndefinedFunctionCallback(function ($name) { if (function_exists($name)) { return new \Twig_SimpleFunction($name, function () use($name) { return call_user_func_array($name, func_get_args()); }); return false; } }); echo $twig->render('view.html', $options); return; }
public function __construct($langString = null) { $this->langString = isset($langString) ? $langString : Application::getConfigItem('defaultLanguage'); try { $this->loadLanguageFile(); } catch (\Exception $e) { echo 'Error loading language file: ', $e->getMessage(); } return; }
private static function setJsFolder($jsFolder = '') { if (empty($jsFolder)) { $jsFolder = self::getAssetsFolder() . DIRECTORY_SEPARATOR . Application::getConfigItem('jsFolder'); } else { $jsFolder = self::getAssetsFolder() . DIRECTORY_SEPARATOR . $jsFolder; } if (!is_dir($jsFolder)) { throw new \Exception('jsFolder is not a folder'); } self::$jsFolder = $jsFolder; }
protected function getView($view) { if (!is_string($view) || empty($view)) { throw new \Exception('view file name parameter could not be blank'); } $controller = get_class($this); $pattern = '#([\\w]+)' . Application::getConfigItem('controllerPostfix') . '$#'; preg_match($pattern, $controller, $matches); $controller = $matches[1]; $viewFilePath = Application::getBasePath() . DIRECTORY_SEPARATOR . Application::getConfigItem('viewsFolder') . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . $view; if (!file_exists($viewFilePath)) { } return file_get_contents($viewFilePath); }
public static function setAction($action = self::NOTFOUND_ACTION) { if (empty($action)) { $action = self::NOTFOUND_ACTION; } $action = Application::getConfigItem('actionPrefix') . ucfirst($action); $controller = self::getController(); if (empty($controller)) { throw new \Exception('controller should be set before action'); } $reflector = new \ReflectionClass($controller); if (!$reflector->hasMethod($action)) { throw new \Exception('controller action ' . $action . ' has been not defined'); } self::$action = $action; return; }