Merge branch '4.6' into 4

This commit is contained in:
Steve Boyd 2020-09-09 13:58:35 +12:00
commit 4c3a5441b2
9 changed files with 102 additions and 29 deletions

View File

@ -66,6 +66,20 @@ class ModuleManifest
*/
private static $project = null;
/**
* 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 CacheFactory $cacheFactory Cache factory to use
*/
public function __construct($base, CacheFactory $cacheFactory = null)
{
$this->base = $base;
$this->cacheKey = sha1($base) . '_modules';
$this->cacheFactory = $cacheFactory;
}
/**
* Adds a path as a module
*
@ -104,20 +118,6 @@ class ModuleManifest
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 CacheFactory $cacheFactory Cache factory to use
*/
public function __construct($base, CacheFactory $cacheFactory = null)
{
$this->base = $base;
$this->cacheKey = sha1($base) . '_modules';
$this->cacheFactory = $cacheFactory;
}
/**
* @param bool $includeTests
* @param bool $forceRegen Force the manifest to be regenerated.
@ -175,7 +175,7 @@ class ModuleManifest
if ($finder->isDirectoryModule($basename, $pathname, $depth)) {
$this->addModule($pathname);
}
}
},
]);
$finder->find($this->base);
@ -226,20 +226,18 @@ class ModuleManifest
/**
* Sort modules sorted by priority
*
* @return void
*/
public function sort()
{
$order = static::config()->uninherited('module_priority');
$project = static::config()->get('project');
/* @var PrioritySorter $sorter */
/** @var PrioritySorter $sorter */
$sorter = Injector::inst()->createWithArgs(
PrioritySorter::class . '.modulesorter',
[
$this->modules,
$order ?: []
$order ?: [],
]
);
@ -269,13 +267,28 @@ class ModuleManifest
// Find based on loaded modules
$modules = ModuleLoader::inst()->getManifest()->getModules();
foreach ($modules as $module) {
// Check if path is in module
$realPath = realpath($module->getPath());
if ($realPath && stripos($path, $realPath) !== 0) {
continue;
$modulePath = realpath($module->getPath());
// if there is a real path
if ($modulePath) {
// we remove separator to ensure that we are comparing fairly
$modulePath = rtrim($modulePath, DIRECTORY_SEPARATOR);
$path = rtrim($path, DIRECTORY_SEPARATOR);
// if the paths are not the same
if ($modulePath !== $path) {
//add separator to avoid mixing up, for example:
//silverstripe/framework and silverstripe/framework-extension
$modulePath .= DIRECTORY_SEPARATOR;
$path .= DIRECTORY_SEPARATOR;
// if the module path is not the same as the start of the module path being tested
if (stripos($path, $modulePath) !== 0) {
// then we need to test the next module
continue;
}
}
}
// If this is the root module, keep looking in case there is a more specific module later
if (empty($module->getRelativePath())) {
$rootModule = $module;

View File

@ -1025,7 +1025,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*/
public function column($colName = "ID")
{
return $this->dataQuery->distinct(false)->column($colName);
$dataQuery = clone $this->dataQuery;
return $dataQuery->distinct(false)->column($colName);
}
/**

View File

@ -33,12 +33,18 @@ class ArrayData extends ViewableData
{
if (is_object($value)) {
$this->array = get_object_vars($value);
} elseif (ArrayLib::is_associative($value)) {
$this->array = $value;
} elseif (is_array($value) && count($value) === 0) {
$this->array = [];
} elseif (is_array($value)) {
if (ArrayLib::is_associative($value)) {
$this->array = $value;
} elseif (count($value) === 0) {
$this->array = [];
} else {
$message = 'ArrayData constructor expects an object or associative array,
enumerated array passed instead. Did you mean to use ArrayList?';
throw new InvalidArgumentException($message);
}
} else {
$message = 'Parameter to ArrayData constructor needs to be an object or associative array';
$message = 'ArrayData constructor expects an object or associative array';
throw new InvalidArgumentException($message);
}
parent::__construct();

View File

@ -76,6 +76,7 @@ class ClassManifestTest extends SapphireTest
'classd' => "{$this->base}/module/classes/ClassD.php",
'classe' => "{$this->base}/module/classes/ClassE.php",
'vendorclassa' => "{$this->base}/vendor/silverstripe/modulec/code/VendorClassA.php",
'vendorclassx' => "{$this->base}/vendor/silverstripe/modulecbetter/code/VendorClassX.php",
];
$this->assertEquals($expect, $this->manifest->getClasses());
}
@ -90,6 +91,7 @@ class ClassManifestTest extends SapphireTest
'classd' => 'ClassD',
'classe' => 'ClassE',
'vendorclassa' => 'VendorClassA',
'vendorclassx' => 'VendorClassX',
],
$this->manifest->getClassNames()
);

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Core\Tests\Manifest;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Core\Manifest\ModuleManifest;
use SilverStripe\Dev\SapphireTest;
@ -34,6 +35,7 @@ class ModuleManifestTest extends SapphireTest
'module',
'silverstripe/awesome-module',
'silverstripe/modulec',
'silverstripe/modulecbetter',
'silverstripe/root-module',
],
array_keys($modules)
@ -105,4 +107,37 @@ class ModuleManifestTest extends SapphireTest
$module->getResource('composer.json')->getRelativePath()
);
}
/**
* @return array
*/
public function providerTestGetModuleByPath()
{
return [
['vendor/silverstripe/modulec/code/VendorClassA.php', 'silverstripe/modulec'],
['vendor/silverstripe/modulecbetter/code/VendorClassX.php', 'silverstripe/modulecbetter'],
];
}
/**
* @dataProvider providerTestGetModuleByPath
* @param string $path
* @param string $expectedModuleName
*/
public function testGetModuleByPath($path, $expectedModuleName)
{
// important - load the manifest that we are working with to the ModuleLoader
ModuleLoader::inst()->pushManifest($this->manifest);
// work out variables
$path = $this->base . '/' . $path;
$module = $this->manifest->getModuleByPath($path);
$actualModuleName = $module->getName();
// it is testing time!
$this->assertEquals($expectedModuleName, $actualModuleName);
// bye bye bogus manifest
ModuleLoader::inst()->popManifest();
}
}

View File

@ -0,0 +1,4 @@
---
Name: blankconfigmodulecbetter
---
{}

View File

@ -0,0 +1 @@
/* script.js */

View File

@ -0,0 +1,6 @@
<?php
class VendorClassX
{
}

View File

@ -0,0 +1,5 @@
{
"name": "silverstripe/modulecbetter",
"description": "dummy test module",
"require": {}
}