From 777edafe8cad610217c08951a02a1fb46907f634 Mon Sep 17 00:00:00 2001 From: yosymfony Date: Mon, 23 Jun 2014 22:09:07 +0200 Subject: [PATCH 1/4] Support to load plugins without namespace --- ...nConfigData.php => PluginComposerData.php} | 14 +-- src/Yosymfony/Spress/Plugin/PluginItem.php | 5 ++ src/Yosymfony/Spress/Plugin/PluginManager.php | 87 +++++++++++++------ .../fixtures/project/_plugins/TestPlugin.php | 66 ++++++++++++++ 4 files changed, 134 insertions(+), 38 deletions(-) rename src/Yosymfony/Spress/Plugin/{PluginConfigData.php => PluginComposerData.php} (82%) create mode 100644 tests/fixtures/project/_plugins/TestPlugin.php diff --git a/src/Yosymfony/Spress/Plugin/PluginConfigData.php b/src/Yosymfony/Spress/Plugin/PluginComposerData.php similarity index 82% rename from src/Yosymfony/Spress/Plugin/PluginConfigData.php rename to src/Yosymfony/Spress/Plugin/PluginComposerData.php index 3bab249..fcccffb 100644 --- a/src/Yosymfony/Spress/Plugin/PluginConfigData.php +++ b/src/Yosymfony/Spress/Plugin/PluginComposerData.php @@ -12,11 +12,11 @@ namespace Yosymfony\Spress\Plugin; /** - * Configuration data of a plugin. + * Wrapper for Composer data of a plugin * * @author Victor Puertas */ -class PluginConfigData +class PluginComposerData { private $data; @@ -63,14 +63,4 @@ public function getSpressClass() return $result; } - - /** - * Is a valid plugin? - * - * @return boolean - */ - public function isValidPlugin() - { - return isset($this->data['extra']['spress_class']); - } } \ No newline at end of file diff --git a/src/Yosymfony/Spress/Plugin/PluginItem.php b/src/Yosymfony/Spress/Plugin/PluginItem.php index 43327ef..cd7baa6 100644 --- a/src/Yosymfony/Spress/Plugin/PluginItem.php +++ b/src/Yosymfony/Spress/Plugin/PluginItem.php @@ -63,4 +63,9 @@ public function getAuthor() { return $this->metas && isset($this->metas['author']) ? $this->metas['author'] : ''; } + + public function __toString() + { + return $this->getName(); + } } \ No newline at end of file diff --git a/src/Yosymfony/Spress/Plugin/PluginManager.php b/src/Yosymfony/Spress/Plugin/PluginManager.php index e7931b6..16030e6 100644 --- a/src/Yosymfony/Spress/Plugin/PluginManager.php +++ b/src/Yosymfony/Spress/Plugin/PluginManager.php @@ -20,7 +20,7 @@ use Yosymfony\Spress\ContentLocator\ContentLocator; /** - * Plugin manager + * Plugins manager * * @author Victor Puertas */ @@ -80,40 +80,23 @@ public function dispatchEvent($eventName, Event $event = null) } /** - * Get plugins available + * Get plugins availables * * @return array Array of PluginItem */ public function getPlugins() { - $result = []; + $plugins = []; + $composerPlugins = []; $dir = $this->contentLocator->getPluginDir(); if($dir) { - $finder = new Finder(); - $finder->files() - ->in($dir) - ->exclude(self::VENDORS_DIR) - ->name(self::COMPOSER_FILENAME); - - foreach($finder as $file) - { - $pluginConf = $this->getPluginConfigData($file); - - if($pluginConf->isValidPlugin()) - { - $className = $pluginConf->getSpressClass(); - - if($this->isValidClassName($className)) - { - $result[] = new PluginItem(new $className); - } - } - } + $plugins = $this->getNotNamespacePlugins($dir); + $composerPlugins = $this->getComposerPlugins($dir); } - return $result; + return array_merge($plugins, $composerPlugins); } /** @@ -136,6 +119,58 @@ public function getDispatcherShortcut() return $this->dispatcherShortcut; } + private function getNotNamespacePlugins($dir) + { + $plugins = []; + + $finder = new Finder(); + $finder->files() + ->in($dir) + ->exclude(self::VENDORS_DIR) + ->name('*.php'); + + foreach($finder as $file) + { + $className = $file->getBasename('.php'); + include_once($file->getRealPath()); + + if($this->isValidClassName($className)) + { + $plugins[$className] = new PluginItem(new $className); + } + } + + return $plugins; + } + + private function getComposerPlugins($dir) + { + $plugins = []; + + $finder = new Finder(); + $finder->files() + ->in($dir) + ->exclude(self::VENDORS_DIR) + ->name(self::COMPOSER_FILENAME); + + foreach($finder as $file) + { + $pluginConf = $this->getPluginComposerData($file); + + $className = $pluginConf->getSpressClass(); + + if($className) + { + if($this->isValidClassName($className)) + { + $plugins[$className] = new PluginItem(new $className); + } + } + } + + return $plugins; + } + private function updateClassLoader() { $baseDir = $this->contentLocator->getPluginDir(); @@ -201,11 +236,11 @@ private function isValidClassName($name) return $result; } - private function getPluginConfigData(SplFileInfo $item) + private function getPluginComposerData(SplFileInfo $item) { $json = $item->getContents(); $data = json_decode($json, true); - return new PluginConfigData($data); + return new PluginComposerData($data); } } \ No newline at end of file diff --git a/tests/fixtures/project/_plugins/TestPlugin.php b/tests/fixtures/project/_plugins/TestPlugin.php new file mode 100644 index 0000000..dc1cd35 --- /dev/null +++ b/tests/fixtures/project/_plugins/TestPlugin.php @@ -0,0 +1,66 @@ +addEventListener('spress.start', 'onStart'); + $subscriber->addEventListener('spress.before_convert', 'onBefore_convert'); + $subscriber->addEventListener('spress.after_convert', 'onAfter_convert'); + $subscriber->addEventListener('spress.after_convert_posts', 'onAfter_convert_posts'); + $subscriber->addEventListener('spress.before_render', 'onBefore_render'); + $subscriber->addEventListener('spress.after_render', 'onAfter_render'); + $subscriber->addEventListener('spress.before_render_pagination', 'onBefore_render_pagination'); + $subscriber->addEventListener('spress.after_render_pagination', 'onAfter_render_pagination'); + $subscriber->addEventListener('spress.finish', 'onFinish'); + } + + public function onStart(Event\EnvironmentEvent $event) + { + + } + + public function onBefore_convert(Event\ConvertEvent $event) + { + + } + + public function onAfter_convert(Event\ConvertEvent $event) + { + + } + + public function onAfter_convert_posts(Event\AfterConvertPostsEvent $event) + { + + } + + public function onBefore_render(Event\RenderEvent $event) + { + + } + + public function onAfter_render(Event\RenderEvent $event) + { + + } + + public function onBefore_render_pagination(Event\RenderEvent $event) + { + + } + + public function onAfter_render_pagination(Event\RenderEvent $event) + { + + } + + public function onFinish(Event\FinishEvent $event) + { + + } +} \ No newline at end of file From d5ede11aacd04415e6dcccbdb3ab5b725abc293b Mon Sep 17 00:00:00 2001 From: yosymfony Date: Wed, 25 Jun 2014 18:39:32 +0200 Subject: [PATCH 2/4] Options for PluginManager at the constructor --- src/Yosymfony/Spress/Application.php | 22 ++++++++++++------- src/Yosymfony/Spress/Plugin/PluginManager.php | 20 ++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Yosymfony/Spress/Application.php b/src/Yosymfony/Spress/Application.php index 69ca580..3e322c9 100644 --- a/src/Yosymfony/Spress/Application.php +++ b/src/Yosymfony/Spress/Application.php @@ -37,7 +37,7 @@ public function __construct(array $values = array()) $templatesPath = $this->getTemplatesPath($spressPath); // Paths and filenames standard - $this['spress.paths'] = array( + $this['spress.paths'] = [ 'root' => $spressPath, 'config' => $spressPath . '/app/config', 'config.file' => 'config.yml', @@ -45,7 +45,7 @@ public function __construct(array $values = array()) 'templates' => $templatesPath, 'web' => $spressPath . '/web', 'web.index' => $spressPath . '/web/index.php', - ); + ]; $this['spress.version'] = self::VERSION; if(false == isset($this['spress.io'])) @@ -78,12 +78,6 @@ public function __construct(array $values = array()) ]); }); - $this['spress.cms.plugin'] = $this->share(function($app){ - return new PluginManager( - $app['spress.content_locator'], - $app['spress.cms.plugin.classLoader']); - }); - $this['spress.cms.plugin.classLoader'] = function() { $autoloaders = spl_autoload_functions(); @@ -91,6 +85,18 @@ public function __construct(array $values = array()) return $autoloaders[0][0]; }; + $this['spress.cms.plugin.options'] = [ + 'vendors_dir' => 'vendors', + 'composer_filename' => 'composer.json', + ]; + + $this['spress.cms.plugin'] = $this->share(function($app){ + return new PluginManager( + $app['spress.content_locator'], + $app['spress.cms.plugin.classLoader'], + $app['spress.cms.plugin.options']); + }); + $this['spress.cms.renderizer'] = $this->share(function($app){ return new Renderizer( $app['spress.twig_factory'], diff --git a/src/Yosymfony/Spress/Plugin/PluginManager.php b/src/Yosymfony/Spress/Plugin/PluginManager.php index 16030e6..5bf3fbc 100644 --- a/src/Yosymfony/Spress/Plugin/PluginManager.php +++ b/src/Yosymfony/Spress/Plugin/PluginManager.php @@ -26,9 +26,7 @@ */ class PluginManager { - const VENDORS_DIR = "vendors"; - const COMPOSER_FILENAME = "composer.json"; - + private $options = []; private $contentLocator; private $classLoader; private $dispatcher; @@ -40,10 +38,12 @@ class PluginManager * Constructor * * @param ContentLocator $contentLocator - * @param $classLoader + * @param ClassLoader $classLoader + * @param array $options */ - public function __construct(ContentLocator $contentLocator, ClassLoader $classLoader) + public function __construct(ContentLocator $contentLocator, ClassLoader $classLoader, array $options) { + $this->options = $options; $this->contentLocator = $contentLocator; $this->classLoader = $classLoader; $this->dispatcher = new EventDispatcher(); @@ -126,7 +126,7 @@ private function getNotNamespacePlugins($dir) $finder = new Finder(); $finder->files() ->in($dir) - ->exclude(self::VENDORS_DIR) + ->exclude($this->options['vendors_dir']) ->name('*.php'); foreach($finder as $file) @@ -150,8 +150,8 @@ private function getComposerPlugins($dir) $finder = new Finder(); $finder->files() ->in($dir) - ->exclude(self::VENDORS_DIR) - ->name(self::COMPOSER_FILENAME); + ->exclude($this->options['vendors_dir']) + ->name($this->options['composer_filename']); foreach($finder as $file) { @@ -174,7 +174,7 @@ private function getComposerPlugins($dir) private function updateClassLoader() { $baseDir = $this->contentLocator->getPluginDir(); - $vendorDir = $baseDir . '/' . self::VENDORS_DIR; + $vendorDir = $baseDir . '/' . $this->options['vendors_dir']; if(false == file_exists($vendorDir)) { @@ -187,7 +187,7 @@ private function updateClassLoader() ); $embeddedComposer = $embeddedComposerBuilder - ->setComposerFilename(self::COMPOSER_FILENAME) + ->setComposerFilename($this->options['composer_filename']) ->setVendorDirectory($vendorDir) ->build(); From 030846fabf30ef1c212e2d78c801e8c2e689e38a Mon Sep 17 00:00:00 2001 From: yosymfony Date: Thu, 26 Jun 2014 20:13:09 +0200 Subject: [PATCH 3/4] Decoupled options for PluginManager --- src/Yosymfony/Spress/Plugin/PluginManager.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Yosymfony/Spress/Plugin/PluginManager.php b/src/Yosymfony/Spress/Plugin/PluginManager.php index 5bf3fbc..758bdaf 100644 --- a/src/Yosymfony/Spress/Plugin/PluginManager.php +++ b/src/Yosymfony/Spress/Plugin/PluginManager.php @@ -52,9 +52,13 @@ public function __construct(ContentLocator $contentLocator, ClassLoader $classLo /** * Start the life clycle + * + * @expectedException \RuntimeException If not there mandatory options */ public function initialize() { + $this->checkOptions($this->options); + $this->eventsDispatched = []; $this->updateClassLoader(); @@ -243,4 +247,17 @@ private function getPluginComposerData(SplFileInfo $item) return new PluginComposerData($data); } + + private function checkOptions(array $options) + { + if(false == isset($options['vendors_dir'])) + { + throw new \RuntimeException('vendors_dir option is necessary for Plugin Manager'); + } + + if(false == isset($options['composer_filename'])) + { + throw new \RuntimeException('composer_filename option is necessary for Plugin Manager'); + } + } } \ No newline at end of file From e17c8f2ac7f82102cfc61f1291cc1d36754d532c Mon Sep 17 00:00:00 2001 From: yosymfony Date: Thu, 26 Jun 2014 20:14:58 +0200 Subject: [PATCH 4/4] Fix documentation --- src/Yosymfony/Spress/Plugin/PluginComposerData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Yosymfony/Spress/Plugin/PluginComposerData.php b/src/Yosymfony/Spress/Plugin/PluginComposerData.php index fcccffb..0ff52e3 100644 --- a/src/Yosymfony/Spress/Plugin/PluginComposerData.php +++ b/src/Yosymfony/Spress/Plugin/PluginComposerData.php @@ -48,7 +48,7 @@ public function getSpressName() } /** - * Get the Spress plugin entry point + * Get the Spress plugin entry-point * * @return string */