This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
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.
Merge pull request #5 from silpion/feature/remove-commands
Added commands for removing src and dist from a composer.lock
- Loading branch information
Showing
8 changed files
with
307 additions
and
115 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
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,108 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: julius | ||
* Date: 01.10.14 | ||
* Time: 11:48 | ||
*/ | ||
|
||
namespace Silpion\ComposerChecker\Command; | ||
|
||
use Camspiers\JsonPretty\JsonPretty; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
abstract class BaseCommand extends Command | ||
{ | ||
/** | ||
* @var OutputInterface | ||
*/ | ||
protected $output; | ||
|
||
/** | ||
* @var InputInterface | ||
*/ | ||
protected $input; | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$this->input = $input; | ||
$this->output = $output; | ||
} | ||
|
||
/** | ||
* Verbose output helper. | ||
* | ||
* @param $message | ||
* @param $level | ||
*/ | ||
protected function verbose($message, $level) | ||
{ | ||
if ($this->output->getVerbosity() >= $level) { | ||
$this->output->write($message); | ||
} | ||
} | ||
|
||
protected function printTable($rows, $title = ' --- Invalid urls found --- ', array $headers = array('Package', 'Src-URL')) | ||
{ | ||
/** @var \Symfony\Component\Console\Helper\TableHelper $table */ | ||
$table = $this->getApplication()->getHelperSet()->get('table'); | ||
$table->setHeaders($headers)->setRows($rows); | ||
|
||
$this->output->writeln('<error>'.$title.'</error>'); | ||
$table->render($this->output); | ||
} | ||
|
||
/** | ||
* Will return true, if the given url matches at least one of the given patterns. | ||
* | ||
* @param $url | ||
* @param array $patterns | ||
* @return bool | ||
*/ | ||
protected function doesUrlMatchToAtLeastOnePattern($url, array $patterns) | ||
{ | ||
foreach ($patterns as $pattern) { | ||
$regex = '|' . $pattern . '|'; | ||
$this->verbose( | ||
"Checking url '" . $url . "' with regex '" . $regex . "' -> ", | ||
OutputInterface::VERBOSITY_VERBOSE | ||
); | ||
if (preg_match($regex, $url)) { | ||
$this->verbose("MATCHED\n", OutputInterface::VERBOSITY_VERBOSE); | ||
return true; | ||
} else { | ||
$this->verbose("NOT matched\n", OutputInterface::VERBOSITY_VERBOSE); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected function readJsonFromFile($path) | ||
{ | ||
$content = @file_get_contents($path); | ||
if (!$content) { | ||
throw new \Exception("File not found or empty."); | ||
} | ||
|
||
$json = @json_decode($content); | ||
if (!is_object($json) || json_last_error() != JSON_ERROR_NONE) { | ||
throw new \Exception("Invalid JSON in file."); | ||
} | ||
|
||
return $json; | ||
} | ||
|
||
protected function updateJsonContentInFile($path, \stdClass $content) | ||
{ | ||
array_unshift($content->_readme, '------------------------------------------------------------------'); | ||
array_unshift($content->_readme, 'ATTENTION: This file has been changed by silpion/composer-checker.'); | ||
array_unshift($content->_readme, '------------------------------------------------------------------'); | ||
|
||
// Write new content to same file | ||
$jsonPretty = new JsonPretty(); | ||
file_put_contents($path, $jsonPretty->prettify($content)); | ||
|
||
$this->output->writeln('<info>Updated file.</info>'); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
* | ||
* @author Julius Beckmann <[email protected]> | ||
*/ | ||
class CheckDistCommand extends Command | ||
class CheckDistCommand extends BaseCommand | ||
{ | ||
protected function configure() | ||
{ | ||
|
@@ -41,6 +41,8 @@ protected function configure() | |
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
parent::execute($input, $output); | ||
|
||
$patterns = $input->getOption('url-pattern'); | ||
if (!$patterns) { | ||
$output->writeln('<error>Need at least one url-pattern.</error>'); | ||
|
@@ -52,17 +54,10 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
$patterns[] = '^$'; | ||
} | ||
|
||
$content = @file_get_contents($input->getArgument('file')); | ||
if (!$content) { | ||
$output->writeln('<error>File not found.</error>'); | ||
|
||
return 1; | ||
} | ||
|
||
$json = @json_decode($content); | ||
if (!is_object($json) || json_last_error() != JSON_ERROR_NONE) { | ||
$output->writeln('<error>Invalid JSON in file.</error>'); | ||
|
||
try { | ||
$json = $this->readJsonFromFile($input->getArgument('file')); | ||
}catch(\Exception $e) { | ||
$output->writeln('<error>'.$e->getMessage().'</error>'); | ||
return 1; | ||
} | ||
|
||
|
@@ -73,12 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
$rows[] = array($package, $url); | ||
} | ||
|
||
/** @var \Symfony\Component\Console\Helper\TableHelper $table */ | ||
$table = $this->getApplication()->getHelperSet()->get('table'); | ||
$table->setHeaders(array('Package', 'Dist-URL'))->setRows($rows); | ||
|
||
$output->writeln('<error> --- Invalid urls found --- </error>'); | ||
$table->render($output); | ||
$this->printTable($rows); | ||
|
||
return 1; | ||
} | ||
|
@@ -87,63 +77,30 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
} | ||
|
||
/** | ||
* Will return a array of invalid packages and their dist-urls determined by the given patterns. | ||
* A dist-url is invalid if NONE of the given patterns has matched. | ||
* Will return a array of invalid packages and their urls determined by the given patterns. | ||
* A url is invalid if NONE of the given patterns has matched. | ||
* | ||
* @param \stdClass $json | ||
* @param array $patterns | ||
* @param \Symfony\Component\Console\Output\OutputInterface $output | ||
* @return array | ||
*/ | ||
protected function searchUrlPatterns(\stdClass $json, array $patterns, OutputInterface $output) | ||
protected function searchUrlPatterns(\stdClass $json, array $patterns) | ||
{ | ||
$errors = array(); | ||
foreach ($json->packages as $package) { | ||
if (!isset($package->dist)) { | ||
$this->verbose($output, 'Dist not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE); | ||
$this->verbose('Dist not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE); | ||
$url = ''; | ||
}else{ | ||
$url = $package->dist->url; | ||
} | ||
|
||
$matched = false; | ||
|
||
foreach ($patterns as $pattern) { | ||
$regex = '|' . $pattern . '|'; | ||
$this->verbose( | ||
$output, | ||
"Checking dist url '" . $url . "' with regex '" . $regex . "' -> ", | ||
OutputInterface::VERBOSITY_VERBOSE | ||
); | ||
if (preg_match($regex, $url)) { | ||
$this->verbose($output, "MATCHED\n", OutputInterface::VERBOSITY_VERBOSE); | ||
$matched = true; | ||
break; | ||
} else { | ||
$this->verbose($output, "NOT matched\n", OutputInterface::VERBOSITY_VERBOSE); | ||
} | ||
} | ||
|
||
if (!$matched) { | ||
if (!$this->doesUrlMatchToAtLeastOnePattern($url, $patterns)) { | ||
$errors[$package->name] = $url; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
/** | ||
* Verbose output helper. | ||
* | ||
* @param OutputInterface $output | ||
* @param $message | ||
* @param $level | ||
*/ | ||
private function verbose(OutputInterface $output, $message, $level) | ||
{ | ||
if ($output->getVerbosity() >= $level) { | ||
$output->write($message); | ||
} | ||
} | ||
} | ||
|
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 |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
* | ||
* @author Julius Beckmann <[email protected]> | ||
*/ | ||
class CheckSrcCommand extends Command | ||
class CheckSrcCommand extends BaseCommand | ||
{ | ||
protected function configure() | ||
{ | ||
|
@@ -41,6 +41,8 @@ protected function configure() | |
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
parent::execute($input, $output); | ||
|
||
$patterns = $input->getOption('url-pattern'); | ||
if (!$patterns) { | ||
$output->writeln('<error>Need at least one url-pattern.</error>'); | ||
|
@@ -52,17 +54,10 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
$patterns[] = '^$'; | ||
} | ||
|
||
$content = @file_get_contents($input->getArgument('file')); | ||
if (!$content) { | ||
$output->writeln('<error>File not found.</error>'); | ||
|
||
return 1; | ||
} | ||
|
||
$json = @json_decode($content); | ||
if (!is_object($json) || json_last_error() != JSON_ERROR_NONE) { | ||
$output->writeln('<error>Invalid JSON in file.</error>'); | ||
|
||
try { | ||
$json = $this->readJsonFromFile($input->getArgument('file')); | ||
}catch(\Exception $e) { | ||
$output->writeln('<error>'.$e->getMessage().'</error>'); | ||
return 1; | ||
} | ||
|
||
|
@@ -73,12 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
$rows[] = array($package, $url); | ||
} | ||
|
||
/** @var \Symfony\Component\Console\Helper\TableHelper $table */ | ||
$table = $this->getApplication()->getHelperSet()->get('table'); | ||
$table->setHeaders(array('Package', 'Src-URL'))->setRows($rows); | ||
|
||
$output->writeln('<error> --- Invalid urls found --- </error>'); | ||
$table->render($output); | ||
$this->printTable($rows); | ||
|
||
return 1; | ||
} | ||
|
@@ -92,58 +82,25 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
* | ||
* @param \stdClass $json | ||
* @param array $patterns | ||
* @param \Symfony\Component\Console\Output\OutputInterface $output | ||
* @return array | ||
*/ | ||
protected function searchUrlPatterns(\stdClass $json, array $patterns, OutputInterface $output) | ||
protected function searchUrlPatterns(\stdClass $json, array $patterns) | ||
{ | ||
$errors = array(); | ||
foreach ($json->packages as $package) { | ||
if (!isset($package->source)) { | ||
$this->verbose($output, 'Source not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE); | ||
$this->verbose('Source not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE); | ||
$url = ''; | ||
}else{ | ||
$url = $package->source->url; | ||
} | ||
|
||
$matched = false; | ||
|
||
foreach ($patterns as $pattern) { | ||
$regex = '|' . $pattern . '|'; | ||
$this->verbose( | ||
$output, | ||
"Checking src url '" . $url . "' with regex '" . $regex . "' -> ", | ||
OutputInterface::VERBOSITY_VERBOSE | ||
); | ||
if (preg_match($regex, $url)) { | ||
$this->verbose($output, "MATCHED\n", OutputInterface::VERBOSITY_VERBOSE); | ||
$matched = true; | ||
break; | ||
} else { | ||
$this->verbose($output, "NOT matched\n", OutputInterface::VERBOSITY_VERBOSE); | ||
} | ||
} | ||
|
||
if (!$matched) { | ||
if (!$this->doesUrlMatchToAtLeastOnePattern($url, $patterns)) { | ||
$errors[$package->name] = $url; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
/** | ||
* Verbose output helper. | ||
* | ||
* @param OutputInterface $output | ||
* @param $message | ||
* @param $level | ||
*/ | ||
private function verbose(OutputInterface $output, $message, $level) | ||
{ | ||
if ($output->getVerbosity() >= $level) { | ||
$output->write($message); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.