From 3fd1cb0c6d714b9c3245f9d0d49addd9cfa484bd Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 2 Nov 2013 15:34:07 -0400 Subject: [PATCH] reorganized extension directories. --- extensions/composer/Installer.php | 132 +++++++++++++++++++++++++ extensions/composer/Plugin.php | 34 +++++++ extensions/composer/composer.json | 1 + extensions/composer/yii/composer/Installer.php | 132 ------------------------- extensions/composer/yii/composer/Plugin.php | 34 ------- extensions/smarty/ViewRenderer.php | 98 ++++++++++++++++++ extensions/smarty/composer.json | 3 +- extensions/smarty/yii/smarty/ViewRenderer.php | 98 ------------------ extensions/twig/ViewRenderer.php | 73 ++++++++++++++ extensions/twig/composer.json | 3 +- extensions/twig/yii/twig/ViewRenderer.php | 73 -------------- 11 files changed, 342 insertions(+), 339 deletions(-) create mode 100644 extensions/composer/Installer.php create mode 100644 extensions/composer/Plugin.php delete mode 100644 extensions/composer/yii/composer/Installer.php delete mode 100644 extensions/composer/yii/composer/Plugin.php create mode 100644 extensions/smarty/ViewRenderer.php delete mode 100644 extensions/smarty/yii/smarty/ViewRenderer.php create mode 100644 extensions/twig/ViewRenderer.php delete mode 100644 extensions/twig/yii/twig/ViewRenderer.php diff --git a/extensions/composer/Installer.php b/extensions/composer/Installer.php new file mode 100644 index 0000000..0dda073 --- /dev/null +++ b/extensions/composer/Installer.php @@ -0,0 +1,132 @@ + + * @since 2.0 + */ +class Installer extends LibraryInstaller +{ + const EXTRA_BOOTSTRAP = 'bootstrap'; + const EXTRA_WRITABLE = 'writable'; + const EXTRA_EXECUTABLE = 'executable'; + + /** + * @inheritdoc + */ + public function supports($packageType) + { + return $packageType === 'yii2-extension'; + } + + /** + * @inheritdoc + */ + public function install(InstalledRepositoryInterface $repo, PackageInterface $package) + { + parent::install($repo, $package); + $this->addPackage($package); + } + + /** + * @inheritdoc + */ + public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) + { + parent::update($repo, $initial, $target); + $this->removePackage($initial); + $this->addPackage($target); + } + + /** + * @inheritdoc + */ + public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) + { + parent::uninstall($repo, $package); + $this->removePackage($package); + } + + protected function addPackage(PackageInterface $package) + { + $extension = [ + 'name' => $package->getName(), + 'version' => $package->getVersion(), + ]; + + $extra = $package->getExtra(); + + if (isset($extra[self::EXTRA_BOOTSTRAP]) && is_string($extra[self::EXTRA_BOOTSTRAP])) { + $extension['bootstrap'] = $extra[self::EXTRA_BOOTSTRAP]; + } + + $extensions = $this->loadExtensions(); + $extensions[$package->getName()] = $extension; + $this->saveExtensions($extensions); + } + + protected function removePackage(PackageInterface $package) + { + $packages = $this->loadExtensions(); + unset($packages[$package->getName()]); + $this->saveExtensions($packages); + } + + protected function loadExtensions() + { + $file = $this->vendorDir . '/yii-extensions.php'; + return is_file($file) ? require($file) : []; + } + + protected function saveExtensions(array $extensions) + { + $file = $this->vendorDir . '/yii-extensions.php'; + file_put_contents($file, " [], + self::EXTRA_EXECUTABLE => [], + ], $event->getComposer()->getPackage()->getExtra()); + + foreach ((array)$options[self::EXTRA_WRITABLE] as $path) { + echo "Setting writable: $path ..."; + if (is_dir($path)) { + chmod($path, 0777); + echo "done\n"; + } else { + echo "The directory was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path; + return; + } + } + + foreach ((array)$options[self::EXTRA_EXECUTABLE] as $path) { + echo "Setting executable: $path ..."; + if (is_file($path)) { + chmod($path, 0755); + echo "done\n"; + } else { + echo "\n\tThe file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path . "\n"; + return; + } + } + } +} diff --git a/extensions/composer/Plugin.php b/extensions/composer/Plugin.php new file mode 100644 index 0000000..fb480a3 --- /dev/null +++ b/extensions/composer/Plugin.php @@ -0,0 +1,34 @@ + + * @since 2.0 + */ +class Plugin implements PluginInterface +{ + /** + * @inheritdoc + */ + public function activate(Composer $composer, IOInterface $io) + { + $installer = new Installer($io, $composer); + $composer->getInstallationManager()->addInstaller($installer); + $file = rtrim($composer->getConfig()->get('vendor-dir'), '/') . '/yii-extensions.php'; + if (!is_file($file)) { + file_put_contents($file, " - * @since 2.0 - */ -class Installer extends LibraryInstaller -{ - const EXTRA_BOOTSTRAP = 'bootstrap'; - const EXTRA_WRITABLE = 'writable'; - const EXTRA_EXECUTABLE = 'executable'; - - /** - * @inheritdoc - */ - public function supports($packageType) - { - return $packageType === 'yii2-extension'; - } - - /** - * @inheritdoc - */ - public function install(InstalledRepositoryInterface $repo, PackageInterface $package) - { - parent::install($repo, $package); - $this->addPackage($package); - } - - /** - * @inheritdoc - */ - public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) - { - parent::update($repo, $initial, $target); - $this->removePackage($initial); - $this->addPackage($target); - } - - /** - * @inheritdoc - */ - public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) - { - parent::uninstall($repo, $package); - $this->removePackage($package); - } - - protected function addPackage(PackageInterface $package) - { - $extension = [ - 'name' => $package->getName(), - 'version' => $package->getVersion(), - ]; - - $extra = $package->getExtra(); - - if (isset($extra[self::EXTRA_BOOTSTRAP]) && is_string($extra[self::EXTRA_BOOTSTRAP])) { - $extension['bootstrap'] = $extra[self::EXTRA_BOOTSTRAP]; - } - - $extensions = $this->loadExtensions(); - $extensions[$package->getName()] = $extension; - $this->saveExtensions($extensions); - } - - protected function removePackage(PackageInterface $package) - { - $packages = $this->loadExtensions(); - unset($packages[$package->getName()]); - $this->saveExtensions($packages); - } - - protected function loadExtensions() - { - $file = $this->vendorDir . '/yii-extensions.php'; - return is_file($file) ? require($file) : []; - } - - protected function saveExtensions(array $extensions) - { - $file = $this->vendorDir . '/yii-extensions.php'; - file_put_contents($file, " [], - self::EXTRA_EXECUTABLE => [], - ], $event->getComposer()->getPackage()->getExtra()); - - foreach ((array)$options[self::EXTRA_WRITABLE] as $path) { - echo "Setting writable: $path ..."; - if (is_dir($path)) { - chmod($path, 0777); - echo "done\n"; - } else { - echo "The directory was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path; - return; - } - } - - foreach ((array)$options[self::EXTRA_EXECUTABLE] as $path) { - echo "Setting executable: $path ..."; - if (is_file($path)) { - chmod($path, 0755); - echo "done\n"; - } else { - echo "\n\tThe file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path . "\n"; - return; - } - } - } -} diff --git a/extensions/composer/yii/composer/Plugin.php b/extensions/composer/yii/composer/Plugin.php deleted file mode 100644 index fb480a3..0000000 --- a/extensions/composer/yii/composer/Plugin.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @since 2.0 - */ -class Plugin implements PluginInterface -{ - /** - * @inheritdoc - */ - public function activate(Composer $composer, IOInterface $io) - { - $installer = new Installer($io, $composer); - $composer->getInstallationManager()->addInstaller($installer); - $file = rtrim($composer->getConfig()->get('vendor-dir'), '/') . '/yii-extensions.php'; - if (!is_file($file)) { - file_put_contents($file, " + * @since 2.0 + */ +class ViewRenderer extends BaseViewRenderer +{ + /** + * @var string the directory or path alias pointing to where Smarty cache will be stored. + */ + public $cachePath = '@runtime/Smarty/cache'; + + /** + * @var string the directory or path alias pointing to where Smarty compiled templates will be stored. + */ + public $compilePath = '@runtime/Smarty/compile'; + + /** + * @var Smarty + */ + public $smarty; + + public function init() + { + $this->smarty = new Smarty(); + $this->smarty->setCompileDir(Yii::getAlias($this->compilePath)); + $this->smarty->setCacheDir(Yii::getAlias($this->cachePath)); + + $this->smarty->registerPlugin('function', 'path', [$this, 'smarty_function_path']); + } + + /** + * Smarty template function to get a path for using in links + * + * Usage is the following: + * + * {path route='blog/view' alias=$post.alias user=$user.id} + * + * where route is Yii route and the rest of parameters are passed as is. + * + * @param $params + * @param \Smarty_Internal_Template $template + * + * @return string + */ + public function smarty_function_path($params, \Smarty_Internal_Template $template) + { + if (!isset($params['route'])) { + trigger_error("path: missing 'route' parameter"); + } + + array_unshift($params, $params['route']) ; + unset($params['route']); + + return Html::url($params); + } + + /** + * 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) + { + /** @var \Smarty_Internal_Template $template */ + $template = $this->smarty->createTemplate($file, null, null, $params, true); + + $template->assign('app', \Yii::$app); + $template->assign('this', $view); + + return $template->fetch(); + } +} diff --git a/extensions/smarty/composer.json b/extensions/smarty/composer.json index 90a154f..566187a 100644 --- a/extensions/smarty/composer.json +++ b/extensions/smarty/composer.json @@ -24,5 +24,6 @@ }, "autoload": { "psr-0": { "yii\\smarty\\": "" } - } + }, + "target-dir": "yii/smarty" } diff --git a/extensions/smarty/yii/smarty/ViewRenderer.php b/extensions/smarty/yii/smarty/ViewRenderer.php deleted file mode 100644 index 2cfb216..0000000 --- a/extensions/smarty/yii/smarty/ViewRenderer.php +++ /dev/null @@ -1,98 +0,0 @@ - - * @since 2.0 - */ -class ViewRenderer extends BaseViewRenderer -{ - /** - * @var string the directory or path alias pointing to where Smarty cache will be stored. - */ - public $cachePath = '@runtime/Smarty/cache'; - - /** - * @var string the directory or path alias pointing to where Smarty compiled templates will be stored. - */ - public $compilePath = '@runtime/Smarty/compile'; - - /** - * @var Smarty - */ - public $smarty; - - public function init() - { - $this->smarty = new Smarty(); - $this->smarty->setCompileDir(Yii::getAlias($this->compilePath)); - $this->smarty->setCacheDir(Yii::getAlias($this->cachePath)); - - $this->smarty->registerPlugin('function', 'path', [$this, 'smarty_function_path']); - } - - /** - * Smarty template function to get a path for using in links - * - * Usage is the following: - * - * {path route='blog/view' alias=$post.alias user=$user.id} - * - * where route is Yii route and the rest of parameters are passed as is. - * - * @param $params - * @param \Smarty_Internal_Template $template - * - * @return string - */ - public function smarty_function_path($params, \Smarty_Internal_Template $template) - { - if (!isset($params['route'])) { - trigger_error("path: missing 'route' parameter"); - } - - array_unshift($params, $params['route']) ; - unset($params['route']); - - return Html::url($params); - } - - /** - * 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) - { - /** @var \Smarty_Internal_Template $template */ - $template = $this->smarty->createTemplate($file, null, null, $params, true); - - $template->assign('app', \Yii::$app); - $template->assign('this', $view); - - return $template->fetch(); - } -} diff --git a/extensions/twig/ViewRenderer.php b/extensions/twig/ViewRenderer.php new file mode 100644 index 0000000..b1598cf --- /dev/null +++ b/extensions/twig/ViewRenderer.php @@ -0,0 +1,73 @@ + + * @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 \Twig_Environment + */ + public $twig; + + public function init() + { + $loader = new \Twig_Loader_String(); + + $this->twig = new \Twig_Environment($loader, array_merge([ + 'cache' => Yii::getAlias($this->cachePath), + ], $this->options)); + + $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); + return $this->twig->render(file_get_contents($file), $params); + } +} diff --git a/extensions/twig/composer.json b/extensions/twig/composer.json index f67cbbd..c57c65d 100644 --- a/extensions/twig/composer.json +++ b/extensions/twig/composer.json @@ -24,5 +24,6 @@ }, "autoload": { "psr-0": { "yii\\twig\\": "" } - } + }, + "target-dir": "yii/twig" } diff --git a/extensions/twig/yii/twig/ViewRenderer.php b/extensions/twig/yii/twig/ViewRenderer.php deleted file mode 100644 index b1598cf..0000000 --- a/extensions/twig/yii/twig/ViewRenderer.php +++ /dev/null @@ -1,73 +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 \Twig_Environment - */ - public $twig; - - public function init() - { - $loader = new \Twig_Loader_String(); - - $this->twig = new \Twig_Environment($loader, array_merge([ - 'cache' => Yii::getAlias($this->cachePath), - ], $this->options)); - - $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); - return $this->twig->render(file_get_contents($file), $params); - } -}