Skip to content

Commit

Permalink
Merge pull request #7 from bluzphp/develop
Browse files Browse the repository at this point in the history
Updated hooks
  • Loading branch information
Anton authored Sep 21, 2017
2 parents 5cbe7c7 + aa457c0 commit bef1319
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
52 changes: 29 additions & 23 deletions src/Installers/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -82,30 +83,28 @@ public function activate(Composer $composer, IOInterface $io)
public static function getSubscribedEvents(): array
{
return [
// copy files to working directory
PackageEvents::POST_PACKAGE_INSTALL => 'copyFiles',
// removed unchanged files
PackageEvents::PRE_PACKAGE_UPDATE => 'removeFiles',
// copy new files
PackageEvents::POST_PACKAGE_UPDATE => 'copyFiles',
// removed all files
PackageEvents::PRE_PACKAGE_UNINSTALL => 'removeFiles',
// copy extra files from root composer.json
// do it only once after create project
ScriptEvents::POST_ROOT_PACKAGE_INSTALL => 'copyProjectExtraFiles',
// copy module's files to working directory
PackageEvents::POST_PACKAGE_INSTALL => 'copyModuleFiles',
PackageEvents::POST_PACKAGE_UPDATE => 'copyModuleFiles',
// removed unchanged module's files
PackageEvents::PRE_PACKAGE_UPDATE => 'removeModuleFiles',
PackageEvents::PRE_PACKAGE_UNINSTALL => 'removeModuleFiles',
];
}

/**
* Hook which is called after install package
* Copy extra files from compose.json of project
*
* It copies bluz module
* @param Event $event
*
* @return void
* @throws \InvalidArgumentException
*/
public function copyFiles(PackageEvent $event)
public function copyProjectExtraFiles(Event $event)
{
if (file_exists($this->installer->getVendorPath())) {
$this->copyModule();
}

$extras = $event->getComposer()->getPackage()->getExtra();
if (array_key_exists('copy-files', $extras)) {
$this->installer->getIo()->write(
Expand All @@ -117,19 +116,26 @@ public function copyFiles(PackageEvent $event)
}

/**
* Hook which is called before update package
* Hook which is called after install package
* It copies bluz module
*
* It checks bluz module
* @throws \InvalidArgumentException
*/
public function removeFiles(PackageEvent $event)
public function copyModuleFiles()
{
if (file_exists($this->installer->getVendorPath())) {
$this->removeModule();
$this->copyModule();
}
}

$extras = $event->getComposer()->getPackage()->getExtra();
if (array_key_exists('copy-files', $extras)) {
$this->removeExtras($extras['copy-files']);
/**
* Hook which is called before update package
* It checks bluz module
*/
public function removeModuleFiles()
{
if (file_exists($this->installer->getVendorPath())) {
$this->removeModule();
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Installers/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class PluginTest extends TestCase
{
public function testSubscribedEventIsPresent()
{
self::assertCount(4, Plugin::getSubscribedEvents());
self::assertCount(5, Plugin::getSubscribedEvents());
}
}

0 comments on commit bef1319

Please sign in to comment.