From 42bdc7f6db6413c806797ce3473a83a7040d85f7 Mon Sep 17 00:00:00 2001 From: Aaron Scherer Date: Sat, 19 Jul 2014 12:59:55 -0600 Subject: [PATCH] Initial Commit --- .gitignore | 3 + README.md | 4 + composer.json | 19 +++++ src/BldrPlugin.php | 207 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/BldrPlugin.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa36fe5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +vendor +composer.lock +.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..52d38a8 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Bldr Composer Plugin +==================== + +No reason for you to use this. Go away now. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f543370 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "bldr-io/composer-plugin", + "type": "composer-plugin", + "require": { + "composer-plugin-api": "1.0.0", + "symfony/yaml": "~2.5.0" + }, + "require-dev": { + "composer/composer": "dev-master" + }, + "autoload": { + "psr-4": { + "Bldr\\Composer\\BldrPlugin\\": "src/" + } + }, + "extra": { + "class": "Bldr\\Composer\\BldrPlugin\\BldrPlugin" + } +} diff --git a/src/BldrPlugin.php b/src/BldrPlugin.php new file mode 100644 index 0000000..9976322 --- /dev/null +++ b/src/BldrPlugin.php @@ -0,0 +1,207 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE + */ + +namespace Bldr; + +use Composer\Composer; +use Composer\EventDispatcher\EventSubscriberInterface; +use Composer\IO\IOInterface; +use Composer\Package\PackageInterface; +use Composer\Plugin\PluginInterface; +use Composer\Script\PackageEvent; +use Composer\Script\ScriptEvents; +use Composer\Util\Filesystem; +use Symfony\Component\Finder\Finder; +use Symfony\Component\Yaml\Yaml; + +/** + * @author Aaron Scherer + */ +class BldrPlugin implements PluginInterface, EventSubscriberInterface +{ + /** + * @var $composer + */ + private $composer; + + /** + * @var $io + */ + private $io; + + /** + * @var $blockLoader + */ + private $blockLoader; + + /** + * @var Filesystem $filesystem + */ + private $filesystem; + + /** + * Initializes the filesystem + */ + public function __construct() + { + $this->filesystem = new Filesystem(); + } + + /** + * @param Composer $composer + * @param IOInterface $io + */ + public function activate(Composer $composer, IOInterface $io) + { + $this->composer = $composer; + $this->io = $io; + } + + /** + * @return array + */ + public static function getSubscribedEvents() + { + return array( + ScriptEvents::PRE_PACKAGE_INSTALL => [ + 'onPrePackageInstall', + ], + ScriptEvents::PRE_PACKAGE_UPDATE => [ + 'onPrePackageUpdate', + ], + ScriptEvents::POST_PACKAGE_INSTALL => [ + 'onPostPackageInstall', + ], + ScriptEvents::POST_PACKAGE_UPDATE => [ + 'onPostPackageUpdate', + ], + ScriptEvents::POST_PACKAGE_UNINSTALL => [ + 'onPostPackageUninstall', + ], + ); + } + + /** + * @param PackageEvent $packageEvent + */ + public function onPrePackageInstall(PackageEvent $packageEvent) + { + $package = $packageEvent->getOperation()->getPackage(); + $this->removeBlockFromLoader($package); + } + + /** + * @param PackageEvent $packageEvent + */ + public function onPrePackageUpdate(PackageEvent $packageEvent) + { + $package = $packageEvent->getOperation()->getInitialPackage(); + $this->removeBlockFromLoader($package); + } + + /** + * @param PackageEvent $packageEvent + */ + public function onPostPackageInstall(PackageEvent $packageEvent) + { + $package = $packageEvent->getOperation()->getPackage(); + $this->addBlockToLoader($package); + } + + /** + * @param PackageEvent $packageEvent + */ + public function onPostPackageUpdate(PackageEvent $packageEvent) + { + $package = $packageEvent->getOperation()->getTargetPackage(); + $this->addBlockToLoader($package); + } + + /** + * @param PackageEvent $packageEvent + */ + public function onPostPackageUninstall(PackageEvent $packageEvent) + { + $package = $packageEvent->getOperation()->getPackage(); + $this->removeBlockFromLoader($package); + } + + /** + * @return string + */ + private function getBlockLoader() + { + if (null !== $this->blockLoader) { + return $this->blockLoader; + } + + $config = $this->composer->getConfig(); + + return $this->blockLoader = $config->has('block-loader') ? $config->get('block-loader') : '.bldr/blocks.yml'; + } + + /** + * @param PackageInterface $package + * + * @return string|null + */ + private function getBlockClass(PackageInterface $package) + { + $extra = $package->getExtra(); + + return isset($extra['block-class']) ? $extra['block-class'] : null; + } + + /** + * @param PackageInterface $package + * + * @return bool|int + */ + private function addBlockToLoader(PackageInterface $package) + { + $class = $this->getBlockClass($package); + if (null === $class) { + return false; + } + + $loader = Yaml::parse(file_get_contents($this->getBlockLoader())); + + if (in_array($class, $loader)) { + return false; + } + + $loader[] = $class; + + return file_put_contents($this->getBlockLoader(), Yaml::dump($loader, 4)); + } + + /** + * @param PackageInterface $package + * + * @return bool|int + */ + private function removeBlockFromLoader(PackageInterface $package) + { + $class = $this->getBlockClass($package); + if (null === $class) { + return false; + } + + $loader = Yaml::parse(file_get_contents($this->getBlockLoader())); + + if (!in_array($class, $loader)) { + return false; + } + unset($loader[array_search($class, $loader)]); + + return file_put_contents($this->getBlockLoader(), Yaml::dump($loader, 4)); + } +}