2017-02-22 16:10:28 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Core\Manifest;
|
|
|
|
|
|
|
|
use Exception;
|
2018-01-12 16:25:02 +13:00
|
|
|
use InvalidArgumentException;
|
2017-02-22 16:10:28 +13:00
|
|
|
use Serializable;
|
2018-01-12 16:25:02 +13:00
|
|
|
use SilverStripe\Core\Path;
|
2017-09-21 17:54:06 +12:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2017-02-22 16:10:28 +13:00
|
|
|
|
|
|
|
class Module implements Serializable
|
|
|
|
{
|
2018-01-12 16:25:02 +13:00
|
|
|
/**
|
|
|
|
* @deprecated 4.1..5.0 Use Path::normalise() instead
|
|
|
|
*/
|
|
|
|
const TRIM_CHARS = ' /\\';
|
2017-09-21 17:54:06 +12:00
|
|
|
|
2017-02-22 16:10:28 +13:00
|
|
|
/**
|
2017-09-21 17:54:06 +12:00
|
|
|
* Full directory path to this module with no trailing slash
|
2017-02-22 16:10:28 +13:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $path = null;
|
|
|
|
|
|
|
|
/**
|
2017-09-21 17:54:06 +12:00
|
|
|
* Base folder of application with no trailing slash
|
2017-02-22 16:10:28 +13:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $basePath = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of composer data
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $composerData = null;
|
|
|
|
|
2017-09-21 17:54:06 +12:00
|
|
|
/**
|
|
|
|
* Loaded resources for this module
|
|
|
|
*
|
|
|
|
* @var ModuleResource[]
|
|
|
|
*/
|
|
|
|
protected $resources = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a module
|
|
|
|
*
|
|
|
|
* @param string $path Absolute filesystem path to this module
|
2018-01-12 16:25:02 +13:00
|
|
|
* @param string $basePath base path for the application this module is installed in
|
2017-09-21 17:54:06 +12:00
|
|
|
*/
|
2018-01-12 16:25:02 +13:00
|
|
|
public function __construct($path, $basePath)
|
2017-02-22 16:10:28 +13:00
|
|
|
{
|
2018-01-12 16:25:02 +13:00
|
|
|
$this->path = Path::normalise($path);
|
|
|
|
$this->basePath = Path::normalise($basePath);
|
2017-02-22 16:10:28 +13:00
|
|
|
$this->loadComposer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets name of this module. Used as unique key and identifier for this module.
|
|
|
|
*
|
|
|
|
* If installed by composer, this will be the full composer name (vendor/name).
|
|
|
|
* If not insalled by composer this will default to the basedir()
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->getComposerName() ?: $this->getShortName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get full composer name. Will be null if no composer.json is available
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getComposerName()
|
|
|
|
{
|
|
|
|
if (isset($this->composerData['name'])) {
|
|
|
|
return $this->composerData['name'];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:54:06 +12:00
|
|
|
/**
|
|
|
|
* Get list of folders that need to be made available
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getExposedFolders()
|
|
|
|
{
|
|
|
|
if (isset($this->composerData['extra']['expose'])) {
|
|
|
|
return $this->composerData['extra']['expose'];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-02-22 16:10:28 +13:00
|
|
|
/**
|
|
|
|
* Gets "short" name of this module. This is the base directory this module
|
|
|
|
* is installed in.
|
|
|
|
*
|
|
|
|
* If installed in root, this will be generated from the composer name instead
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getShortName()
|
|
|
|
{
|
|
|
|
// If installed in the root directory we need to infer from composer
|
|
|
|
if ($this->path === $this->basePath && $this->composerData) {
|
|
|
|
// Sometimes we customise installer name
|
|
|
|
if (isset($this->composerData['extra']['installer-name'])) {
|
|
|
|
return $this->composerData['extra']['installer-name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip from full composer name
|
|
|
|
$composerName = $this->getComposerName();
|
|
|
|
if ($composerName) {
|
|
|
|
list(, $name) = explode('/', $composerName);
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base name of directory
|
|
|
|
return basename($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get base path for this module
|
|
|
|
*
|
2017-09-21 17:54:06 +12:00
|
|
|
* @return string Path with no trailing slash E.g. /var/www/module
|
2017-02-22 16:10:28 +13:00
|
|
|
*/
|
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get path relative to base dir.
|
|
|
|
* If module path is base this will be empty string
|
|
|
|
*
|
2017-09-21 17:54:06 +12:00
|
|
|
* @return string Path with trimmed slashes. E.g. vendor/silverstripe/module.
|
2017-02-22 16:10:28 +13:00
|
|
|
*/
|
|
|
|
public function getRelativePath()
|
|
|
|
{
|
2018-01-12 16:25:02 +13:00
|
|
|
if ($this->path === $this->basePath) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return substr($this->path, strlen($this->basePath) + 1);
|
2017-02-22 16:10:28 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public function serialize()
|
|
|
|
{
|
|
|
|
return json_encode([$this->path, $this->basePath, $this->composerData]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unserialize($serialized)
|
|
|
|
{
|
|
|
|
list($this->path, $this->basePath, $this->composerData) = json_decode($serialized, true);
|
2017-09-21 17:54:06 +12:00
|
|
|
$this->resources = [];
|
2017-02-22 16:10:28 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate _config.php for this module, if one exists
|
|
|
|
*/
|
|
|
|
public function activate()
|
|
|
|
{
|
|
|
|
$config = "{$this->path}/_config.php";
|
|
|
|
if (file_exists($config)) {
|
2017-10-18 09:59:46 +01:00
|
|
|
requireFile($config);
|
2017-02-22 16:10:28 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function loadComposer()
|
|
|
|
{
|
|
|
|
// Load composer data
|
|
|
|
$path = "{$this->path}/composer.json";
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$result = json_decode($content, true);
|
|
|
|
if (json_last_error()) {
|
2017-06-12 10:08:12 +12:00
|
|
|
$errorMessage = json_last_error_msg();
|
|
|
|
throw new Exception("$path: $errorMessage");
|
2017-02-22 16:10:28 +13:00
|
|
|
}
|
|
|
|
$this->composerData = $result;
|
|
|
|
}
|
|
|
|
}
|
2017-03-24 09:58:54 +13:00
|
|
|
|
|
|
|
/**
|
2017-09-21 17:54:06 +12:00
|
|
|
* Get resource for this module
|
2017-03-24 09:58:54 +13:00
|
|
|
*
|
2017-09-21 17:54:06 +12:00
|
|
|
* @param string $path
|
|
|
|
* @return ModuleResource
|
2017-03-24 09:58:54 +13:00
|
|
|
*/
|
2017-09-21 17:54:06 +12:00
|
|
|
public function getResource($path)
|
2017-03-24 09:58:54 +13:00
|
|
|
{
|
2018-01-12 16:25:02 +13:00
|
|
|
$path = Path::normalise($path, true);
|
|
|
|
if (empty($path)) {
|
|
|
|
throw new InvalidArgumentException('$path is required');
|
|
|
|
}
|
2017-09-21 17:54:06 +12:00
|
|
|
if (isset($this->resources[$path])) {
|
|
|
|
return $this->resources[$path];
|
|
|
|
}
|
|
|
|
return $this->resources[$path] = new ModuleResource($this, $path);
|
2017-03-24 09:58:54 +13:00
|
|
|
}
|
|
|
|
|
2017-06-27 14:30:48 +12:00
|
|
|
/**
|
2017-09-21 17:54:06 +12:00
|
|
|
* @deprecated 4.0...5.0 Use getResource($path)->getRelativePath() instead
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRelativeResourcePath($path)
|
|
|
|
{
|
|
|
|
Deprecation::notice('5.0', 'Use getResource($path)->getRelativePath() instead');
|
|
|
|
return $this
|
|
|
|
->getResource($path)
|
|
|
|
->getRelativePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 4.0...5.0 Use ->getResource($path)->getPath() instead
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
2017-06-27 14:30:48 +12:00
|
|
|
*/
|
|
|
|
public function getResourcePath($path)
|
|
|
|
{
|
2017-09-21 17:54:06 +12:00
|
|
|
Deprecation::notice('5.0', 'Use getResource($path)->getPath() instead');
|
|
|
|
return $this
|
|
|
|
->getResource($path)
|
|
|
|
->getPath();
|
2017-06-27 14:30:48 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-21 17:54:06 +12:00
|
|
|
* @deprecated 4.0...5.0 Use ->getResource($path)->getURL() instead
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
2017-06-27 14:30:48 +12:00
|
|
|
*/
|
|
|
|
public function getResourceURL($path)
|
|
|
|
{
|
2017-09-21 17:54:06 +12:00
|
|
|
Deprecation::notice('5.0', 'Use getResource($path)->getURL() instead');
|
|
|
|
return $this
|
|
|
|
->getResource($path)
|
|
|
|
->getURL();
|
2017-06-27 14:30:48 +12:00
|
|
|
}
|
|
|
|
|
2017-03-24 09:58:54 +13:00
|
|
|
/**
|
2017-09-21 17:54:06 +12:00
|
|
|
* @deprecated 4.0...5.0 Use ->getResource($path)->exists() instead
|
2017-03-24 09:58:54 +13:00
|
|
|
* @param string $path
|
2017-09-21 17:54:06 +12:00
|
|
|
* @return string
|
2017-03-24 09:58:54 +13:00
|
|
|
*/
|
|
|
|
public function hasResource($path)
|
|
|
|
{
|
2017-09-21 17:54:06 +12:00
|
|
|
Deprecation::notice('5.0', 'Use getResource($path)->exists() instead');
|
|
|
|
return $this
|
|
|
|
->getResource($path)
|
|
|
|
->exists();
|
2017-03-24 09:58:54 +13:00
|
|
|
}
|
2017-02-22 16:10:28 +13:00
|
|
|
}
|
2017-10-18 09:59:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope isolated require - prevents access to $this, and prevents module _config.php
|
|
|
|
* files potentially leaking variables. Required argument $file is commented out
|
|
|
|
* to avoid leaking that into _config.php
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
function requireFile()
|
|
|
|
{
|
|
|
|
require_once func_get_arg(0);
|
|
|
|
}
|