Skip to content

Commit

Permalink
Merge pull request #5 from potherca-contrib/develop
Browse files Browse the repository at this point in the history
Adds static function to call the Plugin::onDependenciesChangedEvent() method
  • Loading branch information
frenck authored Feb 14, 2017
2 parents c4f5de0 + 37487d2 commit 305ff39
Showing 1 changed file with 70 additions and 10 deletions.
80 changes: 70 additions & 10 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Exception\LogicException;
Expand All @@ -30,7 +31,11 @@
*/
class Plugin implements PluginInterface, EventSubscriberInterface
{
const MESSAGE_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer';
const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update';
const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed';

const PACKAGE_NAME = 'squizlabs/php_codesniffer';
const PACKAGE_TYPE = 'phpcodesniffer-standard';

const PHPCS_CONFIG_KEY = 'installed_paths';
Expand All @@ -55,6 +60,31 @@ class Plugin implements PluginInterface, EventSubscriberInterface
*/
private $processBuilder;

/**
* Triggers the plugin's main functionality.
*
* Makes it possible to run the plugin as a custom command.
*
* @param Event $event
*
* @throws \InvalidArgumentException
* @throws LogicException
* @throws ProcessFailedException
* @throws RuntimeException
*/
public static function run(Event $event)
{
$io = $event->getIO();
$composer = $event->getComposer();

$instance = new static();

$instance->io = $io;
$instance->composer = $composer;
$instance->init();
$instance->onDependenciesChangedEvent();
}

/**
* {@inheritDoc}
*
Expand All @@ -67,10 +97,24 @@ public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;

$this->init();
}

/**
* Prepares the plugin so it's main functionality can be run.
*
* @throws \RuntimeException
* @throws LogicException
* @throws ProcessFailedException
* @throws RuntimeException
*/
private function init()
{
$this->installedPaths = [];

$this->processBuilder = new ProcessBuilder();
$this->processBuilder->setPrefix($composer->getConfig()->get('bin-dir') . DIRECTORY_SEPARATOR . 'phpcs');
$this->processBuilder->setPrefix($this->composer->getConfig()->get('bin-dir') . DIRECTORY_SEPARATOR . 'phpcs');

$this->loadInstalledPaths();
}
Expand All @@ -93,19 +137,31 @@ public static function getSubscribedEvents()
/**
* Entry point for post install and post update events.
*
* @throws \InvalidArgumentException
* @throws RuntimeException
* @throws LogicException
* @throws ProcessFailedException
*/
public function onDependenciesChangedEvent()
{
$io = $this->io;
$isVerbose = $io->isVerbose();

if ($isVerbose) {
$io->write(sprintf('<info>%s</info>', self::MESSAGE_RUNNING_INSTALLER));
}

if ($this->isPHPCodeSnifferInstalled() === true) {
$installPathCleaned = $this->cleanInstalledPaths();
$installPathUpdated = $this->updateInstalledPaths();

if ($installPathCleaned === true || $installPathUpdated === true) {
$this->saveInstalledPaths();
} elseif ($isVerbose) {
$io->write(sprintf('<info>%s</info>', self::MESSAGE_NOTHING_TO_INSTALL));
}
} elseif ($isVerbose) {
$io->write(sprintf('<info>%s</info>', self::MESSAGE_NOT_INSTALLED));
}
}

Expand Down Expand Up @@ -199,6 +255,8 @@ private function cleanInstalledPaths()
* PHP_CodeSniffer and add the missing ones.
*
* @return bool True if changes where made, false otherwise
*
* @throws \InvalidArgumentException
*/
private function updateInstalledPaths()
{
Expand Down Expand Up @@ -254,17 +312,19 @@ function (PackageInterface $package) {
/**
* Simple check if PHP_CodeSniffer is installed.
*
* @return bool PHP_CodeSniffer is installed
* @return bool Whether PHP_CodeSniffer is installed
*/
private function isPHPCodeSnifferInstalled()
{
// Check if PHP_CodeSniffer is actually installed
return (count(
$this
->composer
->getRepositoryManager()
->getLocalRepository()
->findPackages('squizlabs/php_codesniffer')
) !== 0);
$packages = $this
->composer
->getRepositoryManager()
->getLocalRepository()
->findPackages(self::PACKAGE_NAME)
;

$packageCount = count($packages);

return ($packageCount !== 0);
}
}

0 comments on commit 305ff39

Please sign in to comment.