Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed Aug 20, 2018
1 parent 9e2229c commit 77dbacd
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 112 deletions.
2 changes: 1 addition & 1 deletion build/travis/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ if [[ "$PHPUNIT" = true ]]; then
}
fi
done
fi
fi
7 changes: 3 additions & 4 deletions src/Automatic/AbstractConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
use Narrowspark\Automatic\Common\Contract\Resettable as ResettableContract;

abstract class AbstractConfigurator
{
Expand Down Expand Up @@ -130,11 +131,9 @@ public function getConfigurators(): array
}

/**
* Clear all configurators.
*
* @return void
* {@inheritdoc}
*/
public function clear(): void
public function reset(): void
{
$this->configurators = [];
}
Expand Down
97 changes: 31 additions & 66 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Composer\Script\ScriptEvents;
use FilesystemIterator;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Resettable as ResettableContract;
use Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException;
use Narrowspark\Automatic\Common\Contract\Exception\RuntimeException;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
Expand All @@ -52,7 +53,7 @@
use ReflectionClass;
use Symfony\Component\Console\Input\ArgvInput;

class Automatic implements PluginInterface, EventSubscriberInterface
class Automatic implements PluginInterface, EventSubscriberInterface, ResettableContract
{
use ExpandTargetDirTrait;
use GetGenericPropertyReaderTrait;
Expand Down Expand Up @@ -295,12 +296,12 @@ public function onPostUpdate(Event $event, array $operations = []): void
$this->operations = $operations;
}

/** @var \Narrowspark\Automatic\Lock $lock */
/** @var \Composer\IO\IOInterface $io */
$automaticOptions = $this->container->get('composer-extra')[Util::COMPOSER_EXTRA_KEY];
$allowInstall = $automaticOptions['allow-auto-install'] ?? false;
$packages = $this->container->get(OperationsResolver::class)->resolve($this->operations);
/** @var \Narrowspark\Automatic\Lock $lock */
$lock = $this->container->get(Lock::class);
/** @var \Composer\IO\IOInterface $io */
$io = $this->container->get(IOInterface::class);

$io->writeError(\sprintf(
Expand Down Expand Up @@ -524,6 +525,15 @@ public function onFileDownload(PreFileDownloadEvent $event): void
}
}

/**
* {@inheritdoc}
*/
public function reset(): void
{
$this->operations = [];
$this->container->get(Configurator::class)->reset();
}

/**
* Add found legacy tags to the tags manager.
*
Expand Down Expand Up @@ -661,7 +671,7 @@ private function doActionOnPackageOperation(PackageContract $package): void
$this->doUninstall($package, $packageConfigurator);
}

$packageConfigurator->clear();
$packageConfigurator->reset();
}

/**
Expand All @@ -679,7 +689,15 @@ private function doInstall(PackageContract $package, PackageConfigurator $packag
/** @var \Narrowspark\Automatic\Lock $lock */
$lock = $this->container->get(Lock::class);

$this->writeScriptExtenderToLock($package, $lock);
if ($package->hasConfig(ScriptExecutor::TYPE)) {
$extenders = [];

foreach ((array) $package->getConfig(ScriptExecutor::TYPE) as $extender) {
$extenders[$extender] = $package->getName() . \DIRECTORY_SEPARATOR . '';
}

$lock->addSub(ScriptExecutor::TYPE, $package->getName(), $extenders);
}

/** @var \Narrowspark\Automatic\Configurator $configurator */
$configurator = $this->container->get(Configurator::class);
Expand All @@ -697,13 +715,7 @@ private function doInstall(PackageContract $package, PackageConfigurator $packag
$this->postInstallOutput[] = '';
}

$lock->add(
self::LOCK_PACKAGES,
\array_merge(
(array) $lock->get(self::LOCK_PACKAGES),
[$package->getName() => $package->toArray()]
)
);
$lock->addSub(self::LOCK_PACKAGES, $package->getName(), $package->toArray());
}

/**
Expand All @@ -730,54 +742,10 @@ private function doUninstall(PackageContract $package, PackageConfigurator $pack
$lock = $this->container->get(Lock::class);

if ($package->hasConfig(ScriptExecutor::TYPE)) {
$extenders = (array) $lock->get(ScriptExecutor::TYPE);

/** @var \Narrowspark\Automatic\Common\Contract\ScriptExtender $extender */
foreach ((array) $package->getConfig(ScriptExecutor::TYPE) as $extender) {
$type = $extender::getType();

if (isset($extenders[$type])) {
unset($extenders[$type]);
}
}

$lock->add(ScriptExecutor::TYPE, $extenders);
$lock->remove(ScriptExecutor::TYPE, $package->getName());
}

$lock->remove($package->getName());
}

/**
* Looks if the package has a extra section for script extender.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
* @param \Narrowspark\Automatic\Lock $lock
*
* @throws \Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException
*
* @return void
*/
private function writeScriptExtenderToLock(PackageContract $package, Lock $lock): void
{
if ($package->hasConfig(ScriptExecutor::TYPE)) {
$extenders = (array) $lock->get(ScriptExecutor::TYPE);

foreach ((array) $package->getConfig(ScriptExecutor::TYPE) as $extender) {
/** @var \Narrowspark\Automatic\Common\Contract\ScriptExtender $extender */
if (isset($extenders[$extender::getType()])) {
throw new InvalidArgumentException(\sprintf('Script executor extender with the name [%s] already exists.', $extender::getType()));
}

if (! \is_subclass_of($extender, ScriptExtenderContract::class)) {
throw new InvalidArgumentException(\sprintf('The class [%s] must implement the interface [%s].', $extender, ScriptExtenderContract::class));
}

/** @var \Narrowspark\Automatic\Common\Contract\ScriptExtender $extender */
$extenders[$extender::getType()] = $extender;
}

$lock->add(ScriptExecutor::TYPE, $extenders);
}
$lock->remove(self::LOCK_PACKAGES, $package->getName());
}

/**
Expand All @@ -794,20 +762,17 @@ private function runSkeletonGenerator(): void

$lock->read();

if ($lock->has(SkeletonInstaller::LOCK_KEY) && $this->container->get(IOInterface::class)->isInteractive()) {
// Clear old operations if a skeleton is generated.
$this->operations = [];
$this->container->get(Configurator::class)->clear();
if ($lock->has(SkeletonInstaller::LOCK_KEY)) {
$this->reset();

/** @var \Narrowspark\Automatic\SkeletonGenerator $skeletonGenerator */
$skeletonGenerator = $this->container->get(SkeletonGenerator::class);

$skeletonGenerator->run();

$skeletonGenerator->remove();
$skeletonGenerator->run()
->selfRemove();
}

$lock->clear();
$lock->reset();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
declare(strict_types=1);
namespace Narrowspark\Automatic;

use Narrowspark\Automatic\Common\Contract\Resettable as ResettableContract;
use Symfony\Component\Finder\Finder;

final class PathClassLoader
final class ClassLoader implements ResettableContract
{
/**
* List of traits.
Expand Down Expand Up @@ -49,6 +50,10 @@ public function find($dirs): void
->in($dirs)
->name('*.php');

if ($this->filter !== null) {
$finder = $finder->filter($this->filter);
}

/** @var \SplFileInfo $file */
foreach ($finder as $file) {
$realPath = (string) $file->getRealPath();
Expand Down Expand Up @@ -133,6 +138,17 @@ public function getAll(): array
);
}

/**
* {@inheritdoc}
*/
public function reset(): void
{
$this->interfaces = [];
$this->traits = [];
$this->abstractClasses = [];
$this->classes = [];
}

/**
* Find the namespace in the tokens starting at a given key.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class Configurator extends AbstractConfigurator
/**
* {@inheritdoc}
*/
public function clear(): void
public function reset(): void
{
$this->configurators = [];
$this->cache = [];
Expand Down
8 changes: 4 additions & 4 deletions src/Automatic/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get(IOInterface::class),
$container->get(Composer::class),
$container->get(Lock::class),
new PathClassLoader()
new ClassLoader()
);
},
SkeletonInstaller::class => static function (Container $container) {
return new SkeletonInstaller(
$container->get(IOInterface::class),
$container->get(Composer::class),
$container->get(Lock::class),
new PathClassLoader()
new ClassLoader()
);
},
Configurator::class => static function (Container $container) {
Expand Down Expand Up @@ -150,8 +150,8 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get('composer-extra')
);

$scriptExecutor->addExtender(ScriptExtender::class);
$scriptExecutor->addExtender(PhpScriptExtender::class);
$scriptExecutor->addExtender(ScriptExtender::getType(), ScriptExtender::class);
$scriptExecutor->addExtender(PhpScriptExtender::getType(),PhpScriptExtender::class);

return $scriptExecutor;
},
Expand Down
29 changes: 18 additions & 11 deletions src/Automatic/Installer/AbstractInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use Narrowspark\Automatic\Automatic;
use Narrowspark\Automatic\ClassLoader;
use Narrowspark\Automatic\Common\Contract\Exception\UnexpectedValueException;
use Narrowspark\Automatic\Lock;
use Narrowspark\Automatic\PathClassLoader;

abstract class AbstractInstaller extends LibraryInstaller
{
Expand All @@ -34,19 +34,19 @@ abstract class AbstractInstaller extends LibraryInstaller
/**
* A path class loader instance.
*
* @var \Narrowspark\Automatic\PathClassLoader
* @var \Narrowspark\Automatic\ClassLoader
*/
protected $loader;

/**
* Create a new Installer instance.
*
* @param \Composer\IO\IOInterface $io
* @param \Composer\Composer $composer
* @param \Narrowspark\Automatic\Lock $lock
* @param \Narrowspark\Automatic\PathClassLoader $loader
* @param \Composer\IO\IOInterface $io
* @param \Composer\Composer $composer
* @param \Narrowspark\Automatic\Lock $lock
* @param \Narrowspark\Automatic\ClassLoader $loader
*/
public function __construct(IOInterface $io, Composer $composer, Lock $lock, PathClassLoader $loader)
public function __construct(IOInterface $io, Composer $composer, Lock $lock, ClassLoader $loader)
{
parent::__construct($io, $composer, static::TYPE);

Expand Down Expand Up @@ -124,10 +124,17 @@ protected function findClasses(array $autoload, PackageInterface $package): ?arr
{
$name = $package->getPrettyName();
$classes = [];

$psr4 = \array_map(function ($path) use ($name) {
return \rtrim(\rtrim($this->vendorDir, '/') . \DIRECTORY_SEPARATOR . $name . \DIRECTORY_SEPARATOR . $path, '/');
}, (array) $autoload['psr-4']);
$psr4 = [];

foreach ((array) $autoload['psr-4'] as $path) {
if (\is_array($path)) {
foreach ($path as $p) {
$psr4[] = \rtrim($this->vendorDir . \DIRECTORY_SEPARATOR . $name . \DIRECTORY_SEPARATOR . $p, '/');
}
} else {
$psr4[] = \rtrim($this->vendorDir . \DIRECTORY_SEPARATOR . $name . \DIRECTORY_SEPARATOR . $path, '/');
}
}

$this->loader->find($psr4);

Expand Down
11 changes: 5 additions & 6 deletions src/Automatic/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Narrowspark\Automatic;

use Composer\Json\JsonFile;
use Narrowspark\Automatic\Common\Contract\Resettable as ResettableContract;

class Lock
class Lock implements ResettableContract
{
/**
* Instance of JsonFile.
Expand Down Expand Up @@ -141,7 +142,7 @@ public function write(): void

$this->json->write($this->lock);

$this->clear();
$this->reset();
}

/**
Expand All @@ -159,11 +160,9 @@ public function read(): array
}

/**
* Clear the lock.
*
* @return void
* {@inheritdoc}
*/
public function clear(): void
public function reset(): void
{
$this->lock = [];
}
Expand Down
Loading

0 comments on commit 77dbacd

Please sign in to comment.