php-censor/src/PHPCensor/Plugin/Util/ComposerPluginInformation.php

107 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin\Util;
2016-07-19 20:28:11 +02:00
use PHPCensor\Plugin;
2015-02-12 15:11:58 +01:00
/**
* Class ComposerPluginInformation
*
* @package PHPCensor\Plugin\Util
*/
class ComposerPluginInformation implements InstalledPluginInformation
{
/**
* @var array
*/
protected $composerPackages;
/**
* @var array
*/
protected $pluginInfo = null;
/**
* @param string $filePath The path of installed.json created by composer.
* @return ComposerPluginInformation
*/
public static function buildFromYaml($filePath)
{
if (file_exists($filePath)) {
$installed = json_decode(file_get_contents($filePath));
2014-02-27 15:02:21 +01:00
} else {
2016-04-21 06:58:09 +02:00
$installed = [];
}
return new self($installed);
}
/**
* Returns an array of objects. Each one represents an available plugin
* and will have the following properties:
* name - The friendly name of the plugin (may be an empty string)
* class - The class of the plugin (will include namespace)
* @return \stdClass[]
*/
public function getInstalledPlugins()
{
return $this->pluginInfo;
}
/**
* Returns an array of all the class names of plugins that have been
* loaded.
*
* @return string[]
*/
public function getPluginClasses()
{
2014-02-27 15:02:21 +01:00
return array_map(
2015-02-12 15:11:58 +01:00
function (Plugin $plugin) {
2014-02-27 15:02:21 +01:00
return $plugin->class;
},
$this->getInstalledPlugins()
);
}
/**
* @param \stdClass[] $plugins
* @param string $sourcePackageName
* @param string $rootNamespace
*/
protected function addPlugins(
array $plugins,
$sourcePackageName,
2014-02-27 15:02:21 +01:00
$rootNamespace = ""
) {
foreach ($plugins as $plugin) {
if (!isset($plugin->class)) {
continue;
}
$this->addPlugin($plugin, $sourcePackageName, $rootNamespace);
}
}
/**
* @param \stdClass $plugin
* @param string $sourcePackageName
* @param string $rootNamespace
*/
protected function addPlugin(
$plugin,
$sourcePackageName,
2014-02-27 15:02:21 +01:00
$rootNamespace = ""
) {
$newPlugin = clone $plugin;
$newPlugin->class = $rootNamespace . $newPlugin->class;
if (!isset($newPlugin->name)) {
$newPlugin->name = "";
}
$newPlugin->source = $sourcePackageName;
$this->pluginInfo[] = $newPlugin;
}
2014-02-27 15:02:21 +01:00
}