From c0959a1057007136af51d849920056896f0c7c94 Mon Sep 17 00:00:00 2001 From: Laurent Laville Date: Wed, 26 Aug 2020 08:43:59 +0000 Subject: [PATCH] switch from internal api v3 to v5 with a lite analyser:run command with only source path (no more json config file required) --- bin/phpcompatinfo | 7 -- bin/phpcompatinfo.json.dist | 14 --- src/Bartlett/CompatInfo/Api/Analyser.php | 16 ++- src/Bartlett/CompatInfo/Api/V3/Analyser.php | 118 ------------------ src/Bartlett/CompatInfo/Api/V5/Analyser.php | 57 +++++++++ .../CompatInfo/Api/{V3 => V5}/Parser.php | 2 +- .../CompatInfo/Api/{V3 => V5}/Reference.php | 2 +- src/Bartlett/CompatInfo/Client.php | 14 +-- .../CompatInfo/Console/Application.php | 18 +-- 9 files changed, 78 insertions(+), 170 deletions(-) delete mode 100644 bin/phpcompatinfo.json.dist delete mode 100644 src/Bartlett/CompatInfo/Api/V3/Analyser.php create mode 100644 src/Bartlett/CompatInfo/Api/V5/Analyser.php rename src/Bartlett/CompatInfo/Api/{V3 => V5}/Parser.php (99%) rename src/Bartlett/CompatInfo/Api/{V3 => V5}/Reference.php (99%) diff --git a/bin/phpcompatinfo b/bin/phpcompatinfo index b7847999..4f575a7e 100755 --- a/bin/phpcompatinfo +++ b/bin/phpcompatinfo @@ -11,10 +11,6 @@ if (\Phar::running()) { if (!file_exists($vendorDir . '/autoload.php')) { $vendorDir = dirname(dirname($baseDir)); } - - if (!getenv("BARTLETTRC")) { - putenv("BARTLETTRC=" . $appName . '.json'); - } } $loader = require_once $vendorDir . '/autoload.php'; $loader->setUseIncludePath(true); @@ -23,10 +19,7 @@ if (PHP_SAPI !== 'cli') { return; } -use Bartlett\Reflect\Environment; use Bartlett\CompatInfo\Console\Application; -Environment::setScanDir(); - $application = new Application(); $application->run(); diff --git a/bin/phpcompatinfo.json.dist b/bin/phpcompatinfo.json.dist deleted file mode 100644 index 7d9f15b2..00000000 --- a/bin/phpcompatinfo.json.dist +++ /dev/null @@ -1,14 +0,0 @@ -{ - "source-providers": [ - { - "in": ". as current", - "name": "/\\.(php|inc|phtml)$/" - } - ], - "plugins": [ - ], - "analysers" : [ - ], - "services" : [ - ] -} diff --git a/src/Bartlett/CompatInfo/Api/Analyser.php b/src/Bartlett/CompatInfo/Api/Analyser.php index 3367f616..97b0dd49 100644 --- a/src/Bartlett/CompatInfo/Api/Analyser.php +++ b/src/Bartlett/CompatInfo/Api/Analyser.php @@ -13,6 +13,8 @@ namespace Bartlett\CompatInfo\Api; +use Bartlett\Reflect\Api\BaseApi; + /** * Collect and analyse metrics of parsing results. * @@ -22,6 +24,18 @@ * @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License * @since Class available since Release 5.4.0 */ -class Analyser extends \Bartlett\Reflect\Api\Analyser +class Analyser extends BaseApi { + /** + * Analyse a data source and display results. + * + * @param string $source Path to the data source + * + * @return array metrics + */ + public function run($source) + { + $source = trim($source); + return $this->request('analyser/run', 'POST', array($source)); + } } diff --git a/src/Bartlett/CompatInfo/Api/V3/Analyser.php b/src/Bartlett/CompatInfo/Api/V3/Analyser.php deleted file mode 100644 index 208c3857..00000000 --- a/src/Bartlett/CompatInfo/Api/V3/Analyser.php +++ /dev/null @@ -1,118 +0,0 @@ - - * @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License -*/ - -namespace Bartlett\CompatInfo\Api\V3; - -use Bartlett\CompatInfo\Analyser\CompatibilityAnalyser; -use Bartlett\Reflect; -use Bartlett\Reflect\Plugin\PluginManager; - -/** - * Collect and analyse metrics of parsing results. - * - * @category PHP - * @package PHP_CompatInfo - * @author Laurent Laville - * @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License - * @since Class available since Release 5.4.0 - */ -class Analyser extends Reflect\Api\V3\Common -{ - public function __call($name, $args) - { - if ('list' == $name) { - return $this->dir(); - } - } - - /** - * List all analysers available. - * - * @return array - */ - public function dir() - { - return ['compatibility' => CompatibilityAnalyser::class]; - } - - /** - * Analyse a data source and display results. - * - * @param string $source Path to the data source or its alias - * @param array $analysers One or more analyser to perform (case insensitive). - * @param mixed $alias If set, the source refers to its alias - * @param string $format If set, convert result to a specific format. - * @param mixed $filter (optional) Function used to filter results - * - * @return array metrics - * @throws \InvalidArgumentException if an analyser required is not installed - */ - public function run(string $source, array $analysers, $alias, string $format, $filter) - { - $finder = $this->findProvider($source, $alias); - - if ($finder === false) { - throw new \RuntimeException( - 'None data source matching' - ); - } - - if ($filter === false) { - // filter feature is not possible on reflection:* commands - $filter = function ($data) { - return $data; - }; - } - - $reflect = new Parser(); - $reflect->setEventDispatcher($this->eventDispatcher); - $reflect->setDataSourceId($this->dataSourceId); - - $analysersAvailable = $this->dir(); - - // attach valid analysers only - foreach ($analysers as $analyserName) { - if (!array_key_exists(strtolower($analyserName), $analysersAvailable)) { - throw new \InvalidArgumentException( - sprintf( - '"%s" Analyser is not installed or available in v5.4', - $analyserName - ) - ); - } - - $reflect->addAnalyser(new $analysersAvailable[$analyserName]); - } - - $pm = new PluginManager($this->eventDispatcher); - if ($this->registerPlugins) { - $pm->registerPlugins(); - } - - $response = $reflect->parse($finder); - - $response = $filter($response); - - if (!empty($format)) { - $transformMethod = sprintf('transformTo%s', ucfirst($format)); - if (!method_exists($this, $transformMethod)) { - throw new \InvalidArgumentException( - 'Could not render result in this format (not implemented).' - ); - } - $response = $this->$transformMethod($response); - } - - return $response; - } -} diff --git a/src/Bartlett/CompatInfo/Api/V5/Analyser.php b/src/Bartlett/CompatInfo/Api/V5/Analyser.php new file mode 100644 index 00000000..e596a305 --- /dev/null +++ b/src/Bartlett/CompatInfo/Api/V5/Analyser.php @@ -0,0 +1,57 @@ + + * @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License +*/ + +namespace Bartlett\CompatInfo\Api\V5; + +use Bartlett\CompatInfo\Analyser\CompatibilityAnalyser; +use Bartlett\Reflect; + +use RuntimeException; + +/** + * Collect and analyse metrics of parsing results. + * + * @category PHP + * @package PHP_CompatInfo + * @author Laurent Laville + * @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License + * @since Class available since Release 5.4.0 + */ +class Analyser extends Reflect\Api\V3\Common +{ + /** + * Analyse a data source and display results. + * + * @param string $source Path to the data source or its alias + * + * @return array metrics + */ + public function run(string $source) + { + $finder = $this->findProvider($source, false); + + if ($finder === false) { + throw new RuntimeException( + 'None data source matching' + ); + } + + $parser = new Parser(); + $parser->setEventDispatcher($this->eventDispatcher); + $parser->setDataSourceId($this->dataSourceId); + + $parser->addAnalyser(new CompatibilityAnalyser()); + + return $parser->parse($finder); + } +} diff --git a/src/Bartlett/CompatInfo/Api/V3/Parser.php b/src/Bartlett/CompatInfo/Api/V5/Parser.php similarity index 99% rename from src/Bartlett/CompatInfo/Api/V3/Parser.php rename to src/Bartlett/CompatInfo/Api/V5/Parser.php index 2092d964..ba7a61d8 100644 --- a/src/Bartlett/CompatInfo/Api/V3/Parser.php +++ b/src/Bartlett/CompatInfo/Api/V5/Parser.php @@ -1,6 +1,6 @@ client = new LocalClient('Bartlett\CompatInfo\Api\V3'); + $this->client = new LocalClient('Bartlett\CompatInfo\Api\V5'); } /** @@ -51,14 +51,6 @@ public function __construct() public function api($name): BaseApi { $classes = array( - 'Bartlett\Reflect\Api\\' => array( - 'Cache', - 'Config', - 'Diagnose', - 'Diagram', - 'Plugin', - 'Reflection', - ), 'Bartlett\CompatInfo\Api\\' => array( 'Analyser', 'Reference' @@ -80,7 +72,7 @@ public function api($name): BaseApi ); } - $this->client->setNamespace($ns . 'V3'); + $this->client->setNamespace($ns . 'V5'); return new $class($this->client, $this->token); } diff --git a/src/Bartlett/CompatInfo/Console/Application.php b/src/Bartlett/CompatInfo/Console/Application.php index e88e3d9c..775bf357 100644 --- a/src/Bartlett/CompatInfo/Console/Application.php +++ b/src/Bartlett/CompatInfo/Console/Application.php @@ -92,23 +92,7 @@ public function __construct(string $version = 'UNKNOWN') $jsonFile = './' . getenv("BARTLETTRC"); } - $exceptions = array( - 'analyser' => array( - 'run' => array( - 'analysers' => array( - 'default' => array('compatibility'), - ), - ), - ), - 'config' => array( - 'validate' => array( - 'file' => array( - 'default' => $jsonFile, - 'replaceTokens' => array('{json}' => getenv("BARTLETTRC")) - ), - ), - ), - ); + $exceptions = array(); $factory = new CommandFactory($this, $exceptions); $this->addCommands($factory->generateCommands());