PDF rausgenommen

This commit is contained in:
aschwarz
2023-01-23 11:03:31 +01:00
parent 82d562a322
commit a6523903eb
28078 changed files with 4247552 additions and 2 deletions

View File

@ -0,0 +1,18 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Container;
use RuntimeException;
/**
* Thrown if the root container has not been created and set in StaticContainer.
*/
class ContainerDoesNotExistException extends RuntimeException
{
}

View File

@ -0,0 +1,152 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Container;
use DI\Container;
use DI\ContainerBuilder;
use Doctrine\Common\Cache\ArrayCache;
use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Application\Kernel\PluginList;
use Piwik\Plugin\Manager;
/**
* Creates a configured DI container.
*/
class ContainerFactory
{
/**
* @var PluginList
*/
private $pluginList;
/**
* @var GlobalSettingsProvider
*/
private $settings;
/**
* Optional environment configs to load.
*
* @var string[]
*/
private $environments;
/**
* @var array[]
*/
private $definitions;
/**
* @param PluginList $pluginList
* @param GlobalSettingsProvider $settings
* @param string[] $environment Optional environment configs to load.
* @param array[] $definitions
*/
public function __construct(PluginList $pluginList, GlobalSettingsProvider $settings, array $environments = array(), array $definitions = array())
{
$this->pluginList = $pluginList;
$this->settings = $settings;
$this->environments = $environments;
$this->definitions = $definitions;
}
/**
* @link http://php-di.org/doc/container-configuration.html
* @throws \Exception
* @return Container
*/
public function create()
{
$builder = new ContainerBuilder();
$builder->useAnnotations(false);
$builder->setDefinitionCache(new ArrayCache());
// INI config
$builder->addDefinitions(new IniConfigDefinitionSource($this->settings));
// Global config
$builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php');
// Plugin configs
$this->addPluginConfigs($builder);
// Development config
if ($this->isDevelopmentModeEnabled()) {
$this->addEnvironmentConfig($builder, 'dev');
}
// Environment config
foreach ($this->environments as $environment) {
$this->addEnvironmentConfig($builder, $environment);
}
// User config
if (file_exists(PIWIK_USER_PATH . '/config/config.php')
&& !in_array('test', $this->environments, true)) {
$builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php');
}
if (!empty($this->definitions)) {
foreach ($this->definitions as $definitionArray) {
$builder->addDefinitions($definitionArray);
}
}
$container = $builder->build();
$container->set('Piwik\Application\Kernel\PluginList', $this->pluginList);
$container->set('Piwik\Application\Kernel\GlobalSettingsProvider', $this->settings);
return $container;
}
private function addEnvironmentConfig(ContainerBuilder $builder, $environment)
{
if (!$environment) {
return;
}
$file = sprintf('%s/config/environment/%s.php', PIWIK_USER_PATH, $environment);
if (file_exists($file)) {
$builder->addDefinitions($file);
}
// add plugin environment configs
$plugins = $this->pluginList->getActivatedPlugins();
foreach ($plugins as $plugin) {
$baseDir = Manager::getPluginDirectory($plugin);
$environmentFile = $baseDir . '/config/' . $environment . '.php';
if (file_exists($environmentFile)) {
$builder->addDefinitions($environmentFile);
}
}
}
private function addPluginConfigs(ContainerBuilder $builder)
{
$plugins = $this->pluginList->getActivatedPlugins();
foreach ($plugins as $plugin) {
$baseDir = Manager::getPluginDirectory($plugin);
$file = $baseDir . '/config/config.php';
if (file_exists($file)) {
$builder->addDefinitions($file);
}
}
}
private function isDevelopmentModeEnabled()
{
$section = $this->settings->getSection('Development');
return (bool) @$section['enabled']; // TODO: code redundancy w/ Development. hopefully ok for now.
}
}

View File

@ -0,0 +1,95 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Container;
use DI\Definition\Exception\DefinitionException;
use DI\Definition\Source\DefinitionSource;
use DI\Definition\ValueDefinition;
use Piwik\Application\Kernel\GlobalSettingsProvider;
/**
* Expose the INI config into PHP-DI.
*
* The INI config can be used by prefixing `ini.` before the setting we want to get:
*
* $maintenanceMode = $container->get('ini.General.maintenance_mode');
*/
class IniConfigDefinitionSource implements DefinitionSource
{
/**
* @var GlobalSettingsProvider
*/
private $config;
/**
* @var string
*/
private $prefix;
/**
* @param GlobalSettingsProvider $config
* @param string $prefix Prefix for the container entries.
*/
public function __construct(GlobalSettingsProvider $config, $prefix = 'ini.')
{
$this->config = $config;
$this->prefix = $prefix;
}
/**
* {@inheritdoc}
*/
public function getDefinition($name)
{
if (strpos($name, $this->prefix) !== 0) {
return null;
}
list($sectionName, $configKey) = $this->parseEntryName($name);
$section = $this->getSection($sectionName);
if ($configKey === null) {
return new ValueDefinition($name, $section);
}
if (! array_key_exists($configKey, $section)) {
return null;
}
return new ValueDefinition($name, $section[$configKey]);
}
private function parseEntryName($name)
{
$parts = explode('.', $name, 3);
array_shift($parts);
if (! isset($parts[1])) {
$parts[1] = null;
}
return $parts;
}
private function getSection($sectionName)
{
$section = $this->config->getSection($sectionName);
if (!is_array($section)) {
throw new DefinitionException(sprintf(
'IniFileChain did not return an array for the config section %s',
$section
));
}
return $section;
}
}

View File

@ -0,0 +1,87 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Container;
use DI\Container;
/**
* This class provides a static access to the container.
*
* @deprecated This class is introduced only to keep BC with the current static architecture. It will be removed in 3.0.
* - it is global state (that class makes the container a global variable)
* - using the container directly is the "service locator" anti-pattern (which is not dependency injection)
*/
class StaticContainer
{
/**
* @var Container[]
*/
private static $containerStack = array();
/**
* Definitions to register in the container.
*
* @var array[]
*/
private static $definitions = array();
/**
* @return Container
*/
public static function getContainer()
{
if (empty(self::$containerStack)) {
throw new ContainerDoesNotExistException("The root container has not been created yet.");
}
return end(self::$containerStack);
}
public static function clearContainer()
{
self::pop();
}
/**
* Only use this in tests.
*
* @param Container $container
*/
public static function push(Container $container)
{
self::$containerStack[] = $container;
}
public static function pop()
{
array_pop(self::$containerStack);
}
public static function addDefinitions(array $definitions)
{
self::$definitions[] = $definitions;
}
/**
* Proxy to Container::get()
*
* @param string $name Container entry name.
* @return mixed
* @throws \DI\NotFoundException
*/
public static function get($name)
{
return self::getContainer()->get($name);
}
public static function getDefinitions()
{
return self::$definitions;
}
}