2017-02-22 16:10:28 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Core\Manifest;
|
|
|
|
|
|
|
|
use LogicException;
|
2017-03-14 15:20:51 +13:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use SilverStripe\Core\Cache\CacheFactory;
|
2017-02-22 16:10:28 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility class which builds a manifest of configuration items
|
|
|
|
*/
|
|
|
|
class ModuleManifest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The base path used when building the manifest
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $base;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A string to prepend to all cache keys to ensure all keys are unique to just this $base
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-02-26 17:05:25 +13:00
|
|
|
protected $cacheKey;
|
2017-02-22 16:10:28 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether `test` directories should be searched when searching for configuration
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $includeTests;
|
|
|
|
|
|
|
|
/**
|
2017-03-14 15:20:51 +13:00
|
|
|
* @var CacheInterface
|
2017-02-22 16:10:28 +13:00
|
|
|
*/
|
|
|
|
protected $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of all modules.
|
|
|
|
*
|
|
|
|
* @var Module[]
|
|
|
|
*/
|
|
|
|
protected $modules = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a path as a module
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
public function addModule($path)
|
|
|
|
{
|
|
|
|
$module = new Module($path, $this->base);
|
|
|
|
$name = $module->getName();
|
|
|
|
|
|
|
|
// Save if not already added
|
|
|
|
if (empty($this->modules[$name])) {
|
|
|
|
$this->modules[$name] = $module;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate duplicate module
|
|
|
|
$path = $module->getPath();
|
|
|
|
$otherPath = $this->modules[$name]->getPath();
|
|
|
|
if ($otherPath !== $path) {
|
|
|
|
throw new LogicException(
|
|
|
|
"Module {$name} is in two places - {$path} and {$otherPath}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the passed module exists
|
|
|
|
*
|
|
|
|
* @param string $name Either full composer name or short name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function moduleExists($name)
|
|
|
|
{
|
|
|
|
$module = $this->getModule($name);
|
|
|
|
return !empty($module);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs and initialises a new configuration object, either loading
|
|
|
|
* from the cache or re-scanning for classes.
|
|
|
|
*
|
|
|
|
* @param string $base The project base path.
|
|
|
|
* @param bool $includeTests
|
|
|
|
* @param bool $forceRegen Force the manifest to be regenerated.
|
2017-03-14 15:20:51 +13:00
|
|
|
* @param CacheFactory $cacheFactory Cache factory to use
|
2017-02-22 16:10:28 +13:00
|
|
|
*/
|
2017-03-14 15:20:51 +13:00
|
|
|
public function __construct($base, $includeTests = false, $forceRegen = false, CacheFactory $cacheFactory = null)
|
2017-02-22 16:10:28 +13:00
|
|
|
{
|
|
|
|
$this->base = $base;
|
2017-02-26 17:05:25 +13:00
|
|
|
$this->cacheKey = sha1($base).'_modules';
|
2017-02-22 16:10:28 +13:00
|
|
|
$this->includeTests = $includeTests;
|
|
|
|
|
2017-03-14 15:20:51 +13:00
|
|
|
// build cache from factory
|
|
|
|
if ($cacheFactory) {
|
|
|
|
$this->cache = $cacheFactory->create(
|
|
|
|
CacheInterface::class.'.modulemanifest',
|
|
|
|
[ 'namespace' => 'modulemanifest' . ($includeTests ? '_tests' : '') ]
|
|
|
|
);
|
|
|
|
}
|
2017-02-22 16:10:28 +13:00
|
|
|
|
|
|
|
// Unless we're forcing regen, try loading from cache
|
2017-03-14 15:20:51 +13:00
|
|
|
if (!$forceRegen && $this->cache) {
|
|
|
|
$this->modules = $this->cache->get($this->cacheKey) ?: [];
|
2017-02-22 16:10:28 +13:00
|
|
|
}
|
|
|
|
if (empty($this->modules)) {
|
|
|
|
$this->regenerate($includeTests);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes all of the php _config.php files found by this manifest.
|
|
|
|
*/
|
|
|
|
public function activateConfig()
|
|
|
|
{
|
|
|
|
foreach ($this->getModules() as $module) {
|
|
|
|
$module->activate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Completely regenerates the manifest file. Scans through finding all php _config.php and yaml _config/*.ya?ml
|
|
|
|
* files,parses the yaml files into fragments, sorts them and figures out what values need to be checked to pick
|
|
|
|
* the correct variant.
|
|
|
|
*
|
|
|
|
* Does _not_ build the actual variant
|
|
|
|
*
|
|
|
|
* @param bool $includeTests
|
|
|
|
*/
|
2017-03-14 15:20:51 +13:00
|
|
|
public function regenerate($includeTests = false)
|
2017-02-22 16:10:28 +13:00
|
|
|
{
|
|
|
|
$this->modules = [];
|
|
|
|
|
|
|
|
$finder = new ManifestFileFinder();
|
|
|
|
$finder->setOptions(array(
|
|
|
|
'min_depth' => 0,
|
|
|
|
'name_regex' => '/(^|[\/\\\\])_config.php$/',
|
|
|
|
'ignore_tests' => !$includeTests,
|
|
|
|
'file_callback' => array($this, 'addSourceConfigFile'),
|
|
|
|
// Cannot be max_depth: 1 due to "/framework/admin/_config.php"
|
|
|
|
'max_depth' => 2
|
|
|
|
));
|
|
|
|
$finder->find($this->base);
|
|
|
|
|
|
|
|
$finder = new ManifestFileFinder();
|
|
|
|
$finder->setOptions(array(
|
|
|
|
'name_regex' => '/\.ya?ml$/',
|
|
|
|
'ignore_tests' => !$includeTests,
|
|
|
|
'file_callback' => array($this, 'addYAMLConfigFile'),
|
|
|
|
'max_depth' => 2
|
|
|
|
));
|
|
|
|
$finder->find($this->base);
|
|
|
|
|
2017-03-14 15:20:51 +13:00
|
|
|
if ($this->cache) {
|
|
|
|
$this->cache->set($this->cacheKey, $this->modules);
|
2017-02-22 16:10:28 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record finding of _config.php file
|
|
|
|
*
|
|
|
|
* @param string $basename
|
|
|
|
* @param string $pathname
|
|
|
|
*/
|
2017-03-14 15:20:51 +13:00
|
|
|
public function addSourceConfigFile($basename, $pathname)
|
2017-02-22 16:10:28 +13:00
|
|
|
{
|
|
|
|
$this->addModule(dirname($pathname));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle lookup of _config/*.yml file
|
|
|
|
*
|
|
|
|
* @param string $basename
|
|
|
|
* @param string $pathname
|
|
|
|
*/
|
2017-03-14 15:20:51 +13:00
|
|
|
public function addYAMLConfigFile($basename, $pathname)
|
2017-02-22 16:10:28 +13:00
|
|
|
{
|
|
|
|
if (preg_match('{/([^/]+)/_config/}', $pathname, $match)) {
|
|
|
|
$this->addModule(dirname(dirname($pathname)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get module by name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return Module
|
|
|
|
*/
|
|
|
|
public function getModule($name)
|
|
|
|
{
|
|
|
|
// Optimised find
|
|
|
|
if (isset($this->modules[$name])) {
|
|
|
|
return $this->modules[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to lookup by shortname
|
|
|
|
if (!strstr($name, '/')) {
|
|
|
|
foreach ($this->modules as $module) {
|
|
|
|
if (strcasecmp($module->getShortName(), $name) === 0) {
|
|
|
|
return $module;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get modules found
|
|
|
|
*
|
|
|
|
* @return Module[]
|
|
|
|
*/
|
|
|
|
public function getModules()
|
|
|
|
{
|
|
|
|
return $this->modules;
|
|
|
|
}
|
|
|
|
}
|