Skip to content

Commit

Permalink
Merge pull request #14 from bluzphp/develop
Browse files Browse the repository at this point in the history
Fixed `remove` module
  • Loading branch information
Anton authored Oct 25, 2017
2 parents 2e6970f + 683575f commit cfac028
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions src/Installers/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
namespace Bluz\Composer\Installers;

use Composer\Composer;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
Expand Down Expand Up @@ -49,6 +51,11 @@ class Plugin implements PluginInterface, EventSubscriberInterface
*/
protected $vendorPath;

/**
* @var string
*/
protected $packagePath;

/**
* @var string
*/
Expand Down Expand Up @@ -102,6 +109,21 @@ public static function getSubscribedEvents(): array
];
}

/**
* extractPackage
*
* @param PackageEvent $event
*
* @return PackageInterface
*/
protected function extractPackage(PackageEvent $event)
{
if ($event->getOperation() instanceof UpdateOperation) {
return $event->getOperation()->getTargetPackage();
}
return $event->getOperation()->getPackage();
}

/**
* Copy extra files from compose.json of project
*
Expand Down Expand Up @@ -132,8 +154,12 @@ public function copyProjectExtraFiles(Event $event)
*/
public function copyModuleFiles(PackageEvent $event)
{
if (file_exists($this->installer->getVendorPath())
&& $event->getOperation()->getPackage()->getType() === 'bluz-module') {
$package = $this->extractPackage($event);
$this->packagePath = $this->vendorPath .DS. $package->getName();
if ($package->getType() === 'bluz-module' && file_exists($this->packagePath)) {
if ($package->getExtra() && isset($package->getExtra()['copy-files'])) {
$this->copyExtras($package->getExtra()['copy-files']);
}
$this->copyModule();
}
}
Expand All @@ -146,8 +172,12 @@ public function copyModuleFiles(PackageEvent $event)
*/
public function removeModuleFiles(PackageEvent $event)
{
if (file_exists($this->installer->getVendorPath())
&& $event->getOperation()->getPackage()->getType() === 'bluz-module') {
$package = $this->extractPackage($event);
$this->packagePath = $this->vendorPath .DS. $package->getName();
if ($package->getType() === 'bluz-module' && file_exists($this->packagePath)) {
if ($package->getExtra() && isset($package->getExtra()['copy-files'])) {
$this->removeExtras($package->getExtra()['copy-files']);
}
$this->removeModule();
}
}
Expand All @@ -165,21 +195,6 @@ protected function getFilesystem()
return $this->filesystem;
}

/**
* getExtra
*
* @return array
*/
protected function getExtraFiles() : array
{
$moduleJson = json_decode(file_get_contents($this->installer->getVendorPath() . DS . 'composer.json'), true);

if (isset($moduleJson, $moduleJson['extra'], $moduleJson['extra']['copy-files'])) {
return $moduleJson['extra']['copy-files'];
}
return [];
}

/**
* Copy Module files
*
Expand All @@ -188,19 +203,17 @@ protected function getExtraFiles() : array
*/
protected function copyModule()
{
$this->copyExtras($this->getExtraFiles());

foreach (self::DIRECTORIES as $directory) {
$this->copy(
$this->installer->getVendorPath() . DS . $directory . DS,
$this->packagePath . DS . $directory . DS,
PATH_ROOT . DS . $directory . DS
);
}

$this->installer->getIo()->write(
sprintf(
' - Copied <comment>%s</comment> module to application',
basename($this->installer->getVendorPath())
basename($this->packagePath)
),
true
);
Expand All @@ -227,8 +240,8 @@ protected function copyExtras($files)
/**
* It recursively copies the files and directories
*
* @param $source
* @param $target
* @param string $source
* @param string $target
*
* @return void
* @throws \InvalidArgumentException
Expand Down Expand Up @@ -312,16 +325,14 @@ protected function copy($source, $target)
*/
protected function removeModule()
{
$this->removeExtras($this->getExtraFiles());

foreach (self::DIRECTORIES as $directory) {
$this->remove($directory);
}

$this->installer->getIo()->write(
sprintf(
' - Removed <comment>%s</comment> module from application',
basename($this->installer->getVendorPath())
basename($this->packagePath)
),
true
);
Expand Down Expand Up @@ -351,7 +362,7 @@ protected function removeExtras($files)
*/
protected function remove($directory)
{
$sourcePath = $this->installer->getVendorPath() . DS . $directory;
$sourcePath = $this->packagePath . DS . $directory;

if (!is_dir($sourcePath)) {
return;
Expand Down

0 comments on commit cfac028

Please sign in to comment.