2017-02-22 16:12:46 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Core\Config;
|
|
|
|
|
2017-06-22 22:50:45 +12:00
|
|
|
use BadMethodCallException;
|
2017-02-22 16:12:46 +13:00
|
|
|
use SilverStripe\Config\Collections\ConfigCollectionInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers config sources via ConfigCollectionInterface
|
|
|
|
*/
|
|
|
|
class ConfigLoader
|
|
|
|
{
|
|
|
|
/**
|
2017-03-02 17:19:44 +13:00
|
|
|
* @internal
|
2017-02-22 16:12:46 +13:00
|
|
|
* @var self
|
|
|
|
*/
|
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ConfigCollectionInterface[] map of config collections
|
|
|
|
*/
|
|
|
|
protected $manifests = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return self
|
|
|
|
*/
|
2017-05-19 14:38:06 +12:00
|
|
|
public static function inst()
|
2017-02-22 16:12:46 +13:00
|
|
|
{
|
2017-06-22 22:50:45 +12:00
|
|
|
return self::$instance ? self::$instance : self::$instance = new static();
|
2017-02-22 16:12:46 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the currently active class manifest instance that is used for
|
|
|
|
* loading classes.
|
|
|
|
*
|
|
|
|
* @return ConfigCollectionInterface
|
|
|
|
*/
|
|
|
|
public function getManifest()
|
|
|
|
{
|
2017-06-22 22:50:45 +12:00
|
|
|
if ($this !== self::$instance) {
|
|
|
|
throw new BadMethodCallException(
|
|
|
|
"Non-current config manifest cannot be accessed. Please call ->activate() first"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (empty($this->manifests)) {
|
|
|
|
throw new BadMethodCallException("No config manifests available");
|
|
|
|
}
|
2017-02-22 16:12:46 +13:00
|
|
|
return $this->manifests[count($this->manifests) - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this class loader has a manifest.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasManifest()
|
|
|
|
{
|
|
|
|
return (bool)$this->manifests;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pushes a class manifest instance onto the top of the stack.
|
|
|
|
*
|
|
|
|
* @param ConfigCollectionInterface $manifest
|
|
|
|
*/
|
|
|
|
public function pushManifest(ConfigCollectionInterface $manifest)
|
|
|
|
{
|
|
|
|
$this->manifests[] = $manifest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ConfigCollectionInterface
|
|
|
|
*/
|
|
|
|
public function popManifest()
|
|
|
|
{
|
|
|
|
return array_pop($this->manifests);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check number of manifests
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function countManifests()
|
|
|
|
{
|
|
|
|
return count($this->manifests);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 22:50:45 +12:00
|
|
|
* Nest the config loader and activates it
|
2017-02-22 16:12:46 +13:00
|
|
|
*
|
2017-06-22 22:50:45 +12:00
|
|
|
* @return static
|
2017-02-22 16:12:46 +13:00
|
|
|
*/
|
|
|
|
public function nest()
|
|
|
|
{
|
2017-06-22 22:50:45 +12:00
|
|
|
// Nest config
|
|
|
|
$manifest = clone $this->getManifest();
|
|
|
|
|
|
|
|
// Create new blank loader with new stack (top level nesting)
|
|
|
|
$newLoader = new static;
|
|
|
|
$newLoader->pushManifest($manifest);
|
|
|
|
|
|
|
|
// Activate new loader
|
|
|
|
return $newLoader->activate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark this instance as the current instance
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function activate()
|
|
|
|
{
|
|
|
|
static::$instance = $this;
|
|
|
|
return $this;
|
2017-02-22 16:12:46 +13:00
|
|
|
}
|
|
|
|
}
|