Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptiklemur committed Jul 19, 2014
0 parents commit 42bdc7f
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
composer.lock
.idea
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bldr Composer Plugin
====================

No reason for you to use this. Go away now.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
207 changes: 207 additions & 0 deletions src/BldrPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php

/**
* This file is part of Bldr.io
*
* (c) Aaron Scherer <[email protected]>
*
* 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 <[email protected]>
*/
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));
}
}

0 comments on commit 42bdc7f

Please sign in to comment.