-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 42bdc7f
Showing
4 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor | ||
composer.lock | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |