diff --git a/LEGALNOTICE b/LEGALNOTICE index 20c372a493aef0b658dd61ed91c7ee68fee6e070..c438523071b90eb287c8d33aaa53790139f53362 100644 --- a/LEGALNOTICE +++ b/LEGALNOTICE @@ -236,6 +236,10 @@ THIRD-PARTY COMPONENTS AND LIBRARIES Link: https://github.com/ccampbell/mousetrap License: Apache 2.0 + Name: PHP-DI + Link: http://php-di.org/ + License: MIT (Expat) + THIRD-PARTY CONTENT diff --git a/composer.json b/composer.json index 47886d558bc53e56dfb0b4b2f6de244fcafd68d6..91fffba957e2a0213c6c38e3fc2003161bd1e056 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,8 @@ "mustangostang/spyc": "~0.5.0", "piwik/device-detector": "~2.0", "piwik/decompress": "~0.1.1", - "piwik/network": "~0.1.0" + "piwik/network": "~0.1.0", + "mnapoli/php-di": "5.0.x-dev" }, "require-dev": { "aws/aws-sdk-php": "2.7.1", diff --git a/composer.lock b/composer.lock index f68cc7508c4a70d29b5ac60f3861f920fb82d6af..3221f0ad253aaa7093cbbca56a38cb4353986d64 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,224 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "7fe1e059c2372246332679bfd15ca4a6", + "hash": "92499fc432a629fefcd5c1b912e44d9c", "packages": [ + { + "name": "container-interop/container-interop", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/container-interop/container-interop.git", + "reference": "b4274c871bfd1ae8d8e527ba11734f4df1573e48" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/container-interop/container-interop/zipball/b4274c871bfd1ae8d8e527ba11734f4df1573e48", + "reference": "b4274c871bfd1ae8d8e527ba11734f4df1573e48", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Interop\\Container\\": "src/Interop/Container/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", + "time": "2014-03-16 14:50:05" + }, + { + "name": "doctrine/annotations", + "version": "v1.2.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/annotations.git", + "reference": "6a6bec0670bb6e71a263b08bc1b98ea242928633" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/annotations/zipball/6a6bec0670bb6e71a263b08bc1b98ea242928633", + "reference": "6a6bec0670bb6e71a263b08bc1b98ea242928633", + "shasum": "" + }, + "require": { + "doctrine/lexer": "1.*", + "php": ">=5.3.2" + }, + "require-dev": { + "doctrine/cache": "1.*", + "phpunit/phpunit": "4.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Annotations\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Docblock Annotations Parser", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "annotations", + "docblock", + "parser" + ], + "time": "2014-09-25 16:45:30" + }, + { + "name": "doctrine/cache", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/cache.git", + "reference": "cf483685798a72c93bf4206e3dd6358ea07d64e7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/cache/zipball/cf483685798a72c93bf4206e3dd6358ea07d64e7", + "reference": "cf483685798a72c93bf4206e3dd6358ea07d64e7", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "conflict": { + "doctrine/common": ">2.2,<2.4" + }, + "require-dev": { + "phpunit/phpunit": ">=3.7", + "satooshi/php-coveralls": "~0.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Cache\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Caching library offering an object-oriented API for many cache backends", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "cache", + "caching" + ], + "time": "2014-09-17 14:24:04" + }, + { + "name": "doctrine/lexer", + "version": "v1.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/lexer.git", + "reference": "2f708a85bb3aab5d99dab8be435abd73e0b18acb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/lexer/zipball/2f708a85bb3aab5d99dab8be435abd73e0b18acb", + "reference": "2f708a85bb3aab5d99dab8be435abd73e0b18acb", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "type": "library", + "autoload": { + "psr-0": { + "Doctrine\\Common\\Lexer\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "lexer", + "parser" + ], + "time": "2013-01-12 18:59:04" + }, { "name": "leafo/lessphp", "version": "v0.4.0", @@ -47,6 +263,99 @@ "homepage": "http://leafo.net/lessphp/", "time": "2013-08-09 17:09:19" }, + { + "name": "mnapoli/php-di", + "version": "5.0.x-dev", + "source": { + "type": "git", + "url": "https://github.com/mnapoli/PHP-DI.git", + "reference": "018126838b9c3922ee23afff2d779af3d88f6740" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mnapoli/PHP-DI/zipball/018126838b9c3922ee23afff2d779af3d88f6740", + "reference": "018126838b9c3922ee23afff2d779af3d88f6740", + "shasum": "" + }, + "require": { + "container-interop/container-interop": "~1.0", + "doctrine/annotations": "1.*", + "doctrine/cache": "1.*", + "mnapoli/phpdocreader": "~1.3", + "myclabs/php-enum": "1.*", + "php": ">=5.3.3" + }, + "require-dev": { + "ocramius/proxy-manager": "~0.3", + "phpunit/phpunit": "~4.0" + }, + "suggest": { + "ocramius/proxy-manager": "Install it if you want to use lazy injection" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "DI\\": "src/", + "IntegrationTests\\": "tests/", + "UnitTests\\": "tests/" + }, + "files": [ + "src/DI/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHP-DI is a Container that makes Dependency Injection as practical as possible in PHP", + "homepage": "http://mnapoli.github.com/PHP-DI/", + "keywords": [ + "container", + "dependency injection", + "di" + ], + "time": "2014-10-31 00:34:51" + }, + { + "name": "mnapoli/phpdocreader", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/mnapoli/PhpDocReader.git", + "reference": "8a6e123fd1ce54f7fcbd71747b3bf04e465da229" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mnapoli/PhpDocReader/zipball/8a6e123fd1ce54f7fcbd71747b3bf04e465da229", + "reference": "8a6e123fd1ce54f7fcbd71747b3bf04e465da229", + "shasum": "" + }, + "require": { + "doctrine/annotations": "1.*", + "php": ">=5.3.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "PhpDocReader": "src/", + "UnitTest": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "keywords": [ + "phpdoc", + "reflection" + ], + "time": "2014-08-21 08:20:45" + }, { "name": "mustangostang/spyc", "version": "0.5.1", @@ -94,6 +403,37 @@ ], "time": "2013-02-21 10:52:01" }, + { + "name": "myclabs/php-enum", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/myclabs/php-enum.git", + "reference": "b52c2f215f5b251693369309ea7f537f9d92ec5e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/php-enum/zipball/b52c2f215f5b251693369309ea7f537f9d92ec5e", + "reference": "b52c2f215f5b251693369309ea7f537f9d92ec5e", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-0": { + "MyCLabs": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHP Enum implementation", + "homepage": "http://github.com/myclabs/php-enum", + "keywords": [ + "enum" + ], + "time": "2013-11-11 18:29:08" + }, { "name": "piwik/decompress", "version": "0.1.1", @@ -1455,6 +1795,7 @@ ], "minimum-stability": "stable", "stability-flags": { + "mnapoli/php-di": 20, "facebook/xhprof": 20 }, "prefer-stable": false, diff --git a/config/global.php b/config/global.php new file mode 100644 index 0000000000000000000000000000000000000000..9a2590724204fa139b520cf3db57b1dab98efcab --- /dev/null +++ b/config/global.php @@ -0,0 +1,4 @@ +<?php + +return array( +); diff --git a/core/FrontController.php b/core/FrontController.php index 0fa143d4b6b62ff129eace8ea61a2c2c61ad0358..16d5930fe2283933875a0298c898258ef16eda9e 100644 --- a/core/FrontController.php +++ b/core/FrontController.php @@ -19,7 +19,6 @@ use Piwik\Plugin\Report; use Piwik\Plugin\Widgets; use Piwik\Plugins\CoreAdminHome\CustomLogo; use Piwik\Session; -use Piwik\Plugins\CoreHome\Controller as CoreHomeController; /** * This singleton dispatches requests to the appropriate plugin Controller. @@ -113,6 +112,8 @@ class FrontController extends Singleton protected function makeController($module, $action, &$parameters) { + $container = StaticContainer::getContainer(); + $controllerClassName = $this->getClassNameController($module); // TRY TO FIND ACTION IN CONTROLLER @@ -120,7 +121,7 @@ class FrontController extends Singleton $class = $this->getClassNameController($module); /** @var $controller Controller */ - $controller = new $class; + $controller = $container->make($class); $controllerAction = $action; if ($controllerAction === false) { @@ -146,7 +147,7 @@ class FrontController extends Singleton $parameters['widgetModule'] = $module; $parameters['widgetMethod'] = $action; - return array(new CoreHomeController(), 'renderWidget'); + return array($container->make('Piwik\Plugins\CoreHome\Controller'), 'renderWidget'); } // TRY TO FIND ACTION IN REPORT @@ -157,7 +158,7 @@ class FrontController extends Singleton $parameters['reportModule'] = $module; $parameters['reportAction'] = $action; - return array(new CoreHomeController(), 'renderReportWidget'); + return array($container->make('Piwik\Plugins\CoreHome\Controller'), 'renderReportWidget'); } if (!empty($action) && Report::PREFIX_ACTION_IN_MENU === substr($action, 0, strlen(Report @@ -169,7 +170,7 @@ class FrontController extends Singleton $parameters['reportModule'] = $module; $parameters['reportAction'] = $reportAction; - return array(new CoreHomeController(), 'renderReportMenu'); + return array($container->make('Piwik\Plugins\CoreHome\Controller'), 'renderReportMenu'); } } diff --git a/core/StaticContainer.php b/core/StaticContainer.php new file mode 100644 index 0000000000000000000000000000000000000000..35f486d801d0aa2ed55baeaf903ccbc7e567d84b --- /dev/null +++ b/core/StaticContainer.php @@ -0,0 +1,62 @@ +<?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; + +use DI\Container; +use DI\ContainerBuilder; +use Doctrine\Common\Cache\ArrayCache; + +/** + * 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 $container; + + /** + * @return Container + */ + public static function getContainer() + { + if (self::$container === null) { + self::$container = self::createContainer(); + } + + return self::$container; + } + + /** + * @link http://php-di.org/doc/container-configuration.html + */ + private static function createContainer() + { + $builder = new ContainerBuilder(); + + // TODO add cache + $builder->setDefinitionCache(new ArrayCache()); + // $builder->writeProxiesToFile(true, PIWIK_USER_PATH . '/tmp/proxies'); + + // Global config + $builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php'); + + // User config + if (file_exists(PIWIK_USER_PATH . '/config/config.php')) { + $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php'); + } + + return $builder->build(); + } +}