From d9802aacce5c4331696dc059f04c0478af876491 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 10 Jan 2014 20:51:49 -0500 Subject: [PATCH] psr-4 change. --- composer.json | 4 +- extensions/twig/CHANGELOG.md | 12 ++ extensions/twig/LICENSE.md | 32 +++ extensions/twig/README.md | 44 ++++ extensions/twig/TwigSimpleFileLoader.php | 74 +++++++ extensions/twig/ViewRenderer.php | 232 +++++++++++++++++++++ extensions/twig/ViewRendererStaticClassProxy.php | 43 ++++ extensions/twig/composer.json | 27 +++ extensions/yii/twig/CHANGELOG.md | 12 -- extensions/yii/twig/LICENSE.md | 32 --- extensions/yii/twig/README.md | 44 ---- extensions/yii/twig/TwigSimpleFileLoader.php | 74 ------- extensions/yii/twig/ViewRenderer.php | 232 --------------------- .../yii/twig/ViewRendererStaticClassProxy.php | 43 ---- extensions/yii/twig/composer.json | 28 --- 15 files changed, 466 insertions(+), 467 deletions(-) create mode 100644 extensions/twig/CHANGELOG.md create mode 100644 extensions/twig/LICENSE.md create mode 100644 extensions/twig/README.md create mode 100644 extensions/twig/TwigSimpleFileLoader.php create mode 100644 extensions/twig/ViewRenderer.php create mode 100644 extensions/twig/ViewRendererStaticClassProxy.php create mode 100644 extensions/twig/composer.json delete mode 100644 extensions/yii/twig/CHANGELOG.md delete mode 100644 extensions/yii/twig/LICENSE.md delete mode 100644 extensions/yii/twig/README.md delete mode 100644 extensions/yii/twig/TwigSimpleFileLoader.php delete mode 100644 extensions/yii/twig/ViewRenderer.php delete mode 100644 extensions/yii/twig/ViewRendererStaticClassProxy.php delete mode 100644 extensions/yii/twig/composer.json diff --git a/composer.json b/composer.json index be0b7de..368c817 100644 --- a/composer.json +++ b/composer.json @@ -97,7 +97,8 @@ }, "autoload": { "psr-4": { - "yii\\faker\\": "extensions/faker/" + "yii\\faker\\": "extensions/faker/", + "yii\\twig\\": "extensions/twig/" }, "psr-0": { "yii\\apidoc\\": "extensions/", @@ -114,7 +115,6 @@ "yii\\smarty\\": "extensions/", "yii\\swiftmailer\\": "extensions/", "yii\\sphinx\\": "extensions/", - "yii\\twig\\": "extensions/", "yii\\": "framework/" } } diff --git a/extensions/twig/CHANGELOG.md b/extensions/twig/CHANGELOG.md new file mode 100644 index 0000000..7c9a377 --- /dev/null +++ b/extensions/twig/CHANGELOG.md @@ -0,0 +1,12 @@ +Yii Framework 2 twig extension Change Log +========================================= + +2.0.0 beta under development +---------------------------- + +- Add File based Twig loader for better caching and usability of twig's file based function (dev-mraj, samdark) + +2.0.0 alpha, December 1, 2013 +----------------------------- + +- Initial release. diff --git a/extensions/twig/LICENSE.md b/extensions/twig/LICENSE.md new file mode 100644 index 0000000..e98f03d --- /dev/null +++ b/extensions/twig/LICENSE.md @@ -0,0 +1,32 @@ +The Yii framework is free software. It is released under the terms of +the following BSD License. + +Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Yii Software LLC nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/extensions/twig/README.md b/extensions/twig/README.md new file mode 100644 index 0000000..e7afabe --- /dev/null +++ b/extensions/twig/README.md @@ -0,0 +1,44 @@ +Twig Extension for Yii 2 +======================== + +This extension provides a `ViewRender` that would allow you to use Twig view template engine. + +To use this extension, simply add the following code in your application configuration: + +```php +return [ + //.... + 'components' => [ + 'view' => [ + 'renderers' => [ + 'twig' => [ + 'class' => 'yii\twig\ViewRenderer', + //'cachePath' => '@runtime/Twig/cache', + //'options' => [], /* Array of twig options */ + // ... see ViewRenderer for more options + ], + ], + ], + ], +]; +``` + + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require --prefer-dist yiisoft/yii2-twig "*" +``` + +or add + +``` +"yiisoft/yii2-twig": "*" +``` + +to the require section of your composer.json. diff --git a/extensions/twig/TwigSimpleFileLoader.php b/extensions/twig/TwigSimpleFileLoader.php new file mode 100644 index 0000000..281d465 --- /dev/null +++ b/extensions/twig/TwigSimpleFileLoader.php @@ -0,0 +1,74 @@ + + */ +class TwigSimpleFileLoader implements \Twig_LoaderInterface +{ + /** + * @var string Path to directory + */ + private $_dir; + + /** + * @param string $dir path to directory + */ + public function __construct($dir) + { + $this->_dir = $dir; + } + + /** + * Compare a file's freshness with previously stored timestamp + * + * @param $name string file name to check + * @param $time int timestamp to compare with + * @return boolean true if file is still fresh and not changes, false otherwise + */ + public function isFresh($name, $time) + { + return filemtime($this->getFilePath($name)) <= $time; + } + + /** + * Get the source of given file name + * + * @param string $name file name + * @return string contents of given file name + */ + public function getSource($name) + { + return file_get_contents($this->getFilePath($name)); + } + + /** + * Get unique key that can represent this file uniquely among other files. + * @param string $name + * @return string + */ + public function getCacheKey($name) + { + return $this->getFilePath($name); + } + + /** + * internally used to get absolute path of given file name + * @param string $name file name + * @return string absolute path of file + */ + protected function getFilePath($name){ + return $this->_dir . '/' . $name; + } +} \ No newline at end of file diff --git a/extensions/twig/ViewRenderer.php b/extensions/twig/ViewRenderer.php new file mode 100644 index 0000000..7edfccb --- /dev/null +++ b/extensions/twig/ViewRenderer.php @@ -0,0 +1,232 @@ + + * @since 2.0 + */ +class ViewRenderer extends BaseViewRenderer +{ + /** + * @var string the directory or path alias pointing to where Twig cache will be stored. + */ + public $cachePath = '@runtime/Twig/cache'; + /** + * @var array Twig options. + * @see http://twig.sensiolabs.org/doc/api.html#environment-options + */ + public $options = []; + /** + * @var array Objects or static classes. + * Keys of the array are names to call in template, values are objects or names of static classes. + * Example: `['html' => '\yii\helpers\Html']`. + * In the template you can use it like this: `{{ html.a('Login', 'site/login') | raw }}`. + */ + public $globals = []; + /** + * @var array Custom functions. + * Keys of the array are names to call in template, values are names of functions or static methods of some class. + * Example: `['rot13' => 'str_rot13', 'a' => '\yii\helpers\Html::a']`. + * In the template you can use it like this: `{{ rot13('test') }}` or `{{ a('Login', 'site/login') | raw }}`. + */ + public $functions = []; + /** + * @var array Custom filters. + * Keys of the array are names to call in template, values are names of functions or static methods of some class. + * Example: `['rot13' => 'str_rot13', 'jsonEncode' => '\yii\helpers\Json::encode']`. + * In the template you can use it like this: `{{ 'test'|rot13 }}` or `{{ model|jsonEncode }}`. + */ + public $filters = []; + /** + * @var array Custom extensions. + * Example: `['Twig_Extension_Sandbox', 'Twig_Extension_Text']` + */ + public $extensions = []; + /** + * @var array Twig lexer options. + * Example: Smarty-like syntax: + * ```php + * [ + * 'tag_comment' => ['{*', '*}'], + * 'tag_block' => ['{', '}'], + * 'tag_variable' => ['{$', '}'] + * ] + * ``` + * @see http://twig.sensiolabs.org/doc/recipes.html#customizing-the-syntax + */ + public $lexerOptions = []; + /** + * @var \Twig_Environment twig environment object that do all rendering twig templates + */ + public $twig; + + + public function init() + { + $this->twig = new \Twig_Environment(null, array_merge([ + 'cache' => Yii::getAlias($this->cachePath), + 'charset' => Yii::$app->charset, + ], $this->options)); + + // Adding custom extensions + if (!empty($this->extensions)) { + foreach ($this->extensions as $extension) { + $this->twig->addExtension(new $extension()); + } + } + + // Adding custom globals (objects or static classes) + if (!empty($this->globals)) { + $this->addGlobals($this->globals); + } + + // Adding custom functions + if (!empty($this->functions)) { + $this->addFunctions($this->functions); + } + + // Adding custom filters + if (!empty($this->filters)) { + $this->addFilters($this->filters); + } + + // Adding custom extensions + if (!empty($this->extensions)) { + $this->addExtensions($this->extensions); + } + + // Change lexer syntax + if (!empty($this->lexerOptions)) { + $this->setLexerOptions($this->lexerOptions); + } + + // Adding global 'void' function (usage: {{void(App.clientScript.registerScriptFile(...))}}) + $this->twig->addFunction('void', new \Twig_Function_Function(function($argument){ + })); + + $this->twig->addFunction('path', new \Twig_Function_Function(function ($path, $args = []) { + return Html::url(array_merge([$path], $args)); + })); + + $this->twig->addGlobal('app', \Yii::$app); + } + + /** + * Renders a view file. + * + * This method is invoked by [[View]] whenever it tries to render a view. + * Child classes must implement this method to render the given view file. + * + * @param View $view the view object used for rendering the file. + * @param string $file the view file. + * @param array $params the parameters to be passed to the view file. + * + * @return string the rendering result + */ + public function render($view, $file, $params) + { + $this->twig->addGlobal('this', $view); + $this->twig->setLoader(new TwigSimpleFileLoader(dirname($file))); + return $this->twig->render(pathinfo($file, PATHINFO_BASENAME), $params); + } + + /** + * Adds global objects or static classes + * @param array $globals @see self::$globals + */ + public function addGlobals($globals) + { + foreach ($globals as $name => $value) { + if (!is_object($value)) { + $value = new ViewRendererStaticClassProxy($value); + } + $this->twig->addGlobal($name, $value); + } + } + + /** + * Adds custom functions + * @param array $functions @see self::$functions + */ + public function addFunctions($functions) + { + $this->_addCustom('Function', $functions); + } + + /** + * Adds custom filters + * @param array $filters @see self::$filters + */ + public function addFilters($filters) + { + $this->_addCustom('Filter', $filters); + } + + /** + * Adds custom extensions + * @param array $extensions @see self::$extensions + */ + public function addExtensions($extensions) + { + foreach ($extensions as $extName) { + $this->twig->addExtension(new $extName()); + } + } + + /** + * Sets Twig lexer options to change templates syntax + * @param array $options @see self::$lexerOptions + */ + public function setLexerOptions($options) + { + $lexer = new \Twig_Lexer($this->twig, $options); + $this->twig->setLexer($lexer); + } + + /** + * Adds custom function or filter + * @param string $classType 'Function' or 'Filter' + * @param array $elements Parameters of elements to add + * @throws \Exception + */ + private function _addCustom($classType, $elements) + { + $classFunction = 'Twig_' . $classType . '_Function'; + + foreach ($elements as $name => $func) { + $twigElement = null; + + switch ($func) { + // Just a name of function + case is_string($func): + $twigElement = new $classFunction($func); + break; + // Name of function + options array + case is_array($func) && is_string($func[0]) && isset($func[1]) && is_array($func[1]): + $twigElement = new $classFunction($func[0], $func[1]); + break; + } + + if ($twigElement !== null) { + $this->twig->{'add'.$classType}($name, $twigElement); + } else { + throw new \Exception("Incorrect options for \"$classType\" $name."); + } + } + } +} diff --git a/extensions/twig/ViewRendererStaticClassProxy.php b/extensions/twig/ViewRendererStaticClassProxy.php new file mode 100644 index 0000000..3823ce0 --- /dev/null +++ b/extensions/twig/ViewRendererStaticClassProxy.php @@ -0,0 +1,43 @@ + + */ +class ViewRendererStaticClassProxy +{ + private $_staticClassName; + + public function __construct($staticClassName) { + $this->_staticClassName = $staticClassName; + } + + public function __get($property) + { + $class = new \ReflectionClass($this->_staticClassName); + return $class->getStaticPropertyValue($property); + } + + public function __set($property, $value) + { + $class = new \ReflectionClass($this->_staticClassName); + $class->setStaticPropertyValue($property, $value); + return $value; + } + + public function __call($method, $arguments) + { + return call_user_func_array(array($this->_staticClassName, $method), $arguments); + } +} \ No newline at end of file diff --git a/extensions/twig/composer.json b/extensions/twig/composer.json new file mode 100644 index 0000000..1638058 --- /dev/null +++ b/extensions/twig/composer.json @@ -0,0 +1,27 @@ +{ + "name": "yiisoft/yii2-twig", + "description": "The Twig integration for the Yii framework", + "keywords": ["yii", "twig", "renderer"], + "type": "yii2-extension", + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yiisoft/yii2/issues?labels=ext%3Atwig", + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "Alexander Makarov", + "email": "sam@rmcreative.ru" + } + ], + "require": { + "yiisoft/yii2": "*", + "twig/twig": "*" + }, + "autoload": { + "psr-4": { "yii\\twig\\": "" } + } +} diff --git a/extensions/yii/twig/CHANGELOG.md b/extensions/yii/twig/CHANGELOG.md deleted file mode 100644 index 7c9a377..0000000 --- a/extensions/yii/twig/CHANGELOG.md +++ /dev/null @@ -1,12 +0,0 @@ -Yii Framework 2 twig extension Change Log -========================================= - -2.0.0 beta under development ----------------------------- - -- Add File based Twig loader for better caching and usability of twig's file based function (dev-mraj, samdark) - -2.0.0 alpha, December 1, 2013 ------------------------------ - -- Initial release. diff --git a/extensions/yii/twig/LICENSE.md b/extensions/yii/twig/LICENSE.md deleted file mode 100644 index e98f03d..0000000 --- a/extensions/yii/twig/LICENSE.md +++ /dev/null @@ -1,32 +0,0 @@ -The Yii framework is free software. It is released under the terms of -the following BSD License. - -Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com) -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - * Neither the name of Yii Software LLC nor the names of its - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. diff --git a/extensions/yii/twig/README.md b/extensions/yii/twig/README.md deleted file mode 100644 index e7afabe..0000000 --- a/extensions/yii/twig/README.md +++ /dev/null @@ -1,44 +0,0 @@ -Twig Extension for Yii 2 -======================== - -This extension provides a `ViewRender` that would allow you to use Twig view template engine. - -To use this extension, simply add the following code in your application configuration: - -```php -return [ - //.... - 'components' => [ - 'view' => [ - 'renderers' => [ - 'twig' => [ - 'class' => 'yii\twig\ViewRenderer', - //'cachePath' => '@runtime/Twig/cache', - //'options' => [], /* Array of twig options */ - // ... see ViewRenderer for more options - ], - ], - ], - ], -]; -``` - - -Installation ------------- - -The preferred way to install this extension is through [composer](http://getcomposer.org/download/). - -Either run - -``` -php composer.phar require --prefer-dist yiisoft/yii2-twig "*" -``` - -or add - -``` -"yiisoft/yii2-twig": "*" -``` - -to the require section of your composer.json. diff --git a/extensions/yii/twig/TwigSimpleFileLoader.php b/extensions/yii/twig/TwigSimpleFileLoader.php deleted file mode 100644 index 281d465..0000000 --- a/extensions/yii/twig/TwigSimpleFileLoader.php +++ /dev/null @@ -1,74 +0,0 @@ - - */ -class TwigSimpleFileLoader implements \Twig_LoaderInterface -{ - /** - * @var string Path to directory - */ - private $_dir; - - /** - * @param string $dir path to directory - */ - public function __construct($dir) - { - $this->_dir = $dir; - } - - /** - * Compare a file's freshness with previously stored timestamp - * - * @param $name string file name to check - * @param $time int timestamp to compare with - * @return boolean true if file is still fresh and not changes, false otherwise - */ - public function isFresh($name, $time) - { - return filemtime($this->getFilePath($name)) <= $time; - } - - /** - * Get the source of given file name - * - * @param string $name file name - * @return string contents of given file name - */ - public function getSource($name) - { - return file_get_contents($this->getFilePath($name)); - } - - /** - * Get unique key that can represent this file uniquely among other files. - * @param string $name - * @return string - */ - public function getCacheKey($name) - { - return $this->getFilePath($name); - } - - /** - * internally used to get absolute path of given file name - * @param string $name file name - * @return string absolute path of file - */ - protected function getFilePath($name){ - return $this->_dir . '/' . $name; - } -} \ No newline at end of file diff --git a/extensions/yii/twig/ViewRenderer.php b/extensions/yii/twig/ViewRenderer.php deleted file mode 100644 index 7edfccb..0000000 --- a/extensions/yii/twig/ViewRenderer.php +++ /dev/null @@ -1,232 +0,0 @@ - - * @since 2.0 - */ -class ViewRenderer extends BaseViewRenderer -{ - /** - * @var string the directory or path alias pointing to where Twig cache will be stored. - */ - public $cachePath = '@runtime/Twig/cache'; - /** - * @var array Twig options. - * @see http://twig.sensiolabs.org/doc/api.html#environment-options - */ - public $options = []; - /** - * @var array Objects or static classes. - * Keys of the array are names to call in template, values are objects or names of static classes. - * Example: `['html' => '\yii\helpers\Html']`. - * In the template you can use it like this: `{{ html.a('Login', 'site/login') | raw }}`. - */ - public $globals = []; - /** - * @var array Custom functions. - * Keys of the array are names to call in template, values are names of functions or static methods of some class. - * Example: `['rot13' => 'str_rot13', 'a' => '\yii\helpers\Html::a']`. - * In the template you can use it like this: `{{ rot13('test') }}` or `{{ a('Login', 'site/login') | raw }}`. - */ - public $functions = []; - /** - * @var array Custom filters. - * Keys of the array are names to call in template, values are names of functions or static methods of some class. - * Example: `['rot13' => 'str_rot13', 'jsonEncode' => '\yii\helpers\Json::encode']`. - * In the template you can use it like this: `{{ 'test'|rot13 }}` or `{{ model|jsonEncode }}`. - */ - public $filters = []; - /** - * @var array Custom extensions. - * Example: `['Twig_Extension_Sandbox', 'Twig_Extension_Text']` - */ - public $extensions = []; - /** - * @var array Twig lexer options. - * Example: Smarty-like syntax: - * ```php - * [ - * 'tag_comment' => ['{*', '*}'], - * 'tag_block' => ['{', '}'], - * 'tag_variable' => ['{$', '}'] - * ] - * ``` - * @see http://twig.sensiolabs.org/doc/recipes.html#customizing-the-syntax - */ - public $lexerOptions = []; - /** - * @var \Twig_Environment twig environment object that do all rendering twig templates - */ - public $twig; - - - public function init() - { - $this->twig = new \Twig_Environment(null, array_merge([ - 'cache' => Yii::getAlias($this->cachePath), - 'charset' => Yii::$app->charset, - ], $this->options)); - - // Adding custom extensions - if (!empty($this->extensions)) { - foreach ($this->extensions as $extension) { - $this->twig->addExtension(new $extension()); - } - } - - // Adding custom globals (objects or static classes) - if (!empty($this->globals)) { - $this->addGlobals($this->globals); - } - - // Adding custom functions - if (!empty($this->functions)) { - $this->addFunctions($this->functions); - } - - // Adding custom filters - if (!empty($this->filters)) { - $this->addFilters($this->filters); - } - - // Adding custom extensions - if (!empty($this->extensions)) { - $this->addExtensions($this->extensions); - } - - // Change lexer syntax - if (!empty($this->lexerOptions)) { - $this->setLexerOptions($this->lexerOptions); - } - - // Adding global 'void' function (usage: {{void(App.clientScript.registerScriptFile(...))}}) - $this->twig->addFunction('void', new \Twig_Function_Function(function($argument){ - })); - - $this->twig->addFunction('path', new \Twig_Function_Function(function ($path, $args = []) { - return Html::url(array_merge([$path], $args)); - })); - - $this->twig->addGlobal('app', \Yii::$app); - } - - /** - * Renders a view file. - * - * This method is invoked by [[View]] whenever it tries to render a view. - * Child classes must implement this method to render the given view file. - * - * @param View $view the view object used for rendering the file. - * @param string $file the view file. - * @param array $params the parameters to be passed to the view file. - * - * @return string the rendering result - */ - public function render($view, $file, $params) - { - $this->twig->addGlobal('this', $view); - $this->twig->setLoader(new TwigSimpleFileLoader(dirname($file))); - return $this->twig->render(pathinfo($file, PATHINFO_BASENAME), $params); - } - - /** - * Adds global objects or static classes - * @param array $globals @see self::$globals - */ - public function addGlobals($globals) - { - foreach ($globals as $name => $value) { - if (!is_object($value)) { - $value = new ViewRendererStaticClassProxy($value); - } - $this->twig->addGlobal($name, $value); - } - } - - /** - * Adds custom functions - * @param array $functions @see self::$functions - */ - public function addFunctions($functions) - { - $this->_addCustom('Function', $functions); - } - - /** - * Adds custom filters - * @param array $filters @see self::$filters - */ - public function addFilters($filters) - { - $this->_addCustom('Filter', $filters); - } - - /** - * Adds custom extensions - * @param array $extensions @see self::$extensions - */ - public function addExtensions($extensions) - { - foreach ($extensions as $extName) { - $this->twig->addExtension(new $extName()); - } - } - - /** - * Sets Twig lexer options to change templates syntax - * @param array $options @see self::$lexerOptions - */ - public function setLexerOptions($options) - { - $lexer = new \Twig_Lexer($this->twig, $options); - $this->twig->setLexer($lexer); - } - - /** - * Adds custom function or filter - * @param string $classType 'Function' or 'Filter' - * @param array $elements Parameters of elements to add - * @throws \Exception - */ - private function _addCustom($classType, $elements) - { - $classFunction = 'Twig_' . $classType . '_Function'; - - foreach ($elements as $name => $func) { - $twigElement = null; - - switch ($func) { - // Just a name of function - case is_string($func): - $twigElement = new $classFunction($func); - break; - // Name of function + options array - case is_array($func) && is_string($func[0]) && isset($func[1]) && is_array($func[1]): - $twigElement = new $classFunction($func[0], $func[1]); - break; - } - - if ($twigElement !== null) { - $this->twig->{'add'.$classType}($name, $twigElement); - } else { - throw new \Exception("Incorrect options for \"$classType\" $name."); - } - } - } -} diff --git a/extensions/yii/twig/ViewRendererStaticClassProxy.php b/extensions/yii/twig/ViewRendererStaticClassProxy.php deleted file mode 100644 index 3823ce0..0000000 --- a/extensions/yii/twig/ViewRendererStaticClassProxy.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ -class ViewRendererStaticClassProxy -{ - private $_staticClassName; - - public function __construct($staticClassName) { - $this->_staticClassName = $staticClassName; - } - - public function __get($property) - { - $class = new \ReflectionClass($this->_staticClassName); - return $class->getStaticPropertyValue($property); - } - - public function __set($property, $value) - { - $class = new \ReflectionClass($this->_staticClassName); - $class->setStaticPropertyValue($property, $value); - return $value; - } - - public function __call($method, $arguments) - { - return call_user_func_array(array($this->_staticClassName, $method), $arguments); - } -} \ No newline at end of file diff --git a/extensions/yii/twig/composer.json b/extensions/yii/twig/composer.json deleted file mode 100644 index 1e7f49e..0000000 --- a/extensions/yii/twig/composer.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "yiisoft/yii2-twig", - "description": "The Twig integration for the Yii framework", - "keywords": ["yii", "twig", "renderer"], - "type": "yii2-extension", - "license": "BSD-3-Clause", - "support": { - "issues": "https://github.com/yiisoft/yii2/issues?labels=ext%3Atwig", - "forum": "http://www.yiiframework.com/forum/", - "wiki": "http://www.yiiframework.com/wiki/", - "irc": "irc://irc.freenode.net/yii", - "source": "https://github.com/yiisoft/yii2" - }, - "authors": [ - { - "name": "Alexander Makarov", - "email": "sam@rmcreative.ru" - } - ], - "require": { - "yiisoft/yii2": "*", - "twig/twig": "*" - }, - "autoload": { - "psr-0": { "yii\\twig\\": "" } - }, - "target-dir": "yii/twig" -}