From e91b9c84bfe2ed9177ea87cadf3545934d096e6d Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 18 Aug 2014 14:13:51 +0300 Subject: [PATCH] Implement DI container --- composer.json | 5 +- index.php | 31 ++- system/defines.php | 2 +- system/src/Grav/Common/Cache.php | 25 ++- system/src/Grav/Common/Config.php | 13 +- system/src/Grav/Common/Data/Blueprint.php | 1 - system/src/Grav/Common/Debugger.php | 52 +++++ system/src/Grav/Common/Grav.php | 187 ++++++++---------- system/src/Grav/Common/Page/Collection.php | 5 +- system/src/Grav/Common/Page/Media.php | 7 +- system/src/Grav/Common/Page/Medium.php | 10 +- system/src/Grav/Common/Page/Page.php | 48 ++--- system/src/Grav/Common/Page/Pages.php | 71 ++++--- system/src/Grav/Common/Plugins.php | 21 +- system/src/Grav/Common/Registry.php | 41 ++-- system/src/Grav/Common/Taxonomy.php | 7 +- system/src/Grav/Common/Themes.php | 17 +- system/src/Grav/Common/Twig.php | 122 ++++++------ system/src/Grav/Common/Utils.php | 2 +- system/src/Grav/Component/DI/Container.php | 6 + .../Component/DI/ServiceProviderInterface.php | 6 + user/config/system.yaml | 2 +- 22 files changed, 375 insertions(+), 306 deletions(-) create mode 100644 system/src/Grav/Common/Debugger.php create mode 100644 system/src/Grav/Component/DI/Container.php create mode 100644 system/src/Grav/Component/DI/ServiceProviderInterface.php diff --git a/composer.json b/composer.json index c937e27a8..e506e7ecf 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "php": ">=5.3.10", + "php": ">=5.4.0", "twig/twig": "1.16.*@dev", "erusev/parsedown": "dev-master", "symfony/yaml": "2.5.*@dev", @@ -20,6 +20,7 @@ "doctrine/cache": "1.4.*@dev", "tracy/tracy": "dev-master", "gregwar/image": "dev-master", - "ircmaxell/password-compat": "1.0.*" + "ircmaxell/password-compat": "1.0.*", + "pimple/pimple": "~3.0" } } diff --git a/index.php b/index.php index 02a6a352a..f35aa9b98 100644 --- a/index.php +++ b/index.php @@ -1,11 +1,12 @@ PHP %s to run.', $ver, $req)); } -use Tracy\Debugger; +use Grav\Common\Grav; +use Grav\Common\Debugger; // Register system libraries to the auto-loader. $loader = require_once __DIR__ . '/system/autoload.php'; @@ -14,28 +15,18 @@ if (!ini_get('date.timezone')) { date_default_timezone_set('UTC'); } +$grav = Grav::instance( + [ + 'Loader' => $loader, + 'Debugger' => new Debugger + ] +); + // Use output buffering to prevent headers from being sent too early. ob_start(); -// Start the timer and enable debugger in production mode as we do not have system configuration yet. -// Debugger catches all errors and logs them, for example if the script doesn't have write permissions. -Debugger::timer(); -Debugger::enable(Debugger::DEVELOPMENT, is_dir(LOG_DIR) ? LOG_DIR : null); - -$grav = new Grav; - try { - // Register all the Grav bits into registry. - $registry = Registry::instance(); - $registry->store('autoloader', $loader); - $registry->store('Grav', $grav); - $registry->store('Uri', new Uri); - $registry->store('Config', Config::instance(CACHE_DIR . 'config.php')); - $registry->store('Cache', new Cache); - $registry->store('Twig', new Twig); - $registry->store('Pages', new Page\Pages); - $registry->store('Taxonomy', new Taxonomy); - + $grav['Debugger']->init(); $grav->process(); } catch (\Exception $e) { diff --git a/system/defines.php b/system/defines.php index b7d1a3526..1000f058b 100644 --- a/system/defines.php +++ b/system/defines.php @@ -2,7 +2,7 @@ // Some standard defines define('GRAV', true); -define('GRAV_VERSION', '0.8.0'); +define('GRAV_VERSION', '0.8.1'); define('DS', '/'); // Directories and Paths diff --git a/system/src/Grav/Common/Cache.php b/system/src/Grav/Common/Cache.php index a032e554d..27eb942ec 100644 --- a/system/src/Grav/Common/Cache.php +++ b/system/src/Grav/Common/Cache.php @@ -1,6 +1,8 @@ init($c); + } + /** * Initialization that sets a base key and the driver based on configuration settings * + * @param Grav $c * @return void */ - public function init() + public function init(Grav $c) { /** @var Config $config */ - $config = Registry::get('Config'); - $prefix = $config->get('system.cache.prefix'); + $config = $c['Config']; + /** @var Uri $uri */ - $uri = Registry::get('Uri'); + $uri = $c['Uri']; + + $prefix = $config->get('system.cache.prefix'); $this->enabled = (bool) $config->get('system.cache.enabled'); diff --git a/system/src/Grav/Common/Config.php b/system/src/Grav/Common/Config.php index 68865b6df..9b96db61f 100644 --- a/system/src/Grav/Common/Config.php +++ b/system/src/Grav/Common/Config.php @@ -101,16 +101,17 @@ class Config extends Data } /** - * Gets configuration instance. + * Load configuration. * - * @param string $filename + * @param Grav $grav * @return \Grav\Common\Config */ - public static function instance($filename) + public static function instance(Grav $grav) { + $filename = $grav['config_path']; + // Load cached version if available.. if (file_exists($filename)) { - clearstatcache(true, $filename); require_once $filename; if (class_exists('\Grav\Config')) { @@ -131,11 +132,11 @@ class Config extends Data // If not set, add manually current base url. if (empty($instance->items['system']['base_url_absolute'])) { - $instance->items['system']['base_url_absolute'] = Registry::get('Uri')->rootUrl(true); + $instance->items['system']['base_url_absolute'] = $grav['Uri']->rootUrl(true); } if (empty($instance->items['system']['base_url_relative'])) { - $instance->items['system']['base_url_relative'] = Registry::get('Uri')->rootUrl(false); + $instance->items['system']['base_url_relative'] = $grav['Uri']->rootUrl(false); } return $instance; diff --git a/system/src/Grav/Common/Data/Blueprint.php b/system/src/Grav/Common/Data/Blueprint.php index 6339f873a..362a999cf 100644 --- a/system/src/Grav/Common/Data/Blueprint.php +++ b/system/src/Grav/Common/Data/Blueprint.php @@ -1,7 +1,6 @@ get('system.debugger.log.enabled') ? LOG_DIR : null; + TracyDebugger::$maxDepth = $config->get('system.debugger.max_depth'); + + // Switch debugger into development mode if configured + if ($config->get('system.debugger.enabled')) { + if ($config->get('system.debugger.strict')) { + TracyDebugger::$strictMode = true; + } + + if (function_exists('ini_set')) { + ini_set('display_errors', true); + } + TracyDebugger::$productionMode = TracyDebugger::DEVELOPMENT; + } + } + + /** + * Log a message. + * + * @param string $message + */ + public function log($message) + { + if (TracyDebugger::$logDirectory) { + TracyDebugger::log(sprintf($message, TracyDebugger::timer() * 1000)); + } + } +} diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 454e5fb98..ac0ebc726 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -1,10 +1,7 @@ $value) { + $instance->offsetSet($key, $value); + } + } - /** - * @var Cache - */ - protected $cache; + return self::$instance; + } - /** - * @var Uri - */ - protected $uri; + protected static function load(array $values) + { + $container = new static($values); - /** - * @var Pages - */ - protected $pages; + $container['config_path'] = CACHE_DIR . 'config.php'; - /** - * @var Page - */ - protected $page; + $container['Grav'] = $container; - /** - * @var Twig - */ - protected $twig; + $container['Uri'] = function ($c) { + return new Uri($c); + }; + $container['Config'] = function ($c) { + return Config::instance($c); + }; + $container['Cache'] = function ($c) { + return new Cache($c); + }; + $container['Plugins'] = function ($c) { + return new Plugins($c); + }; + $container['Themes'] = function ($c) { + return new Themes($c); + }; + $container['Twig'] = function ($c) { + return new Twig($c); + }; + $container['Taxonomy'] = function ($c) { + return new Taxonomy($c); + }; + $container['Pages'] = function ($c) { + return new Page\Pages($c); + }; + $container['Page'] = function ($c) { + return $c['Pages']->dispatch($c['Uri']->route()); + }; - /** - * @var Taxonomy - */ - protected $taxonomy; + return $container; + } public function process() { - // Get the URI and URL (needed for configuration) - $this->uri = Registry::get('Uri'); + $this['Plugins']->init(); - // Get the Configuration settings and caching - $this->config = Registry::get('Config'); - - Debugger::$logDirectory = $this->config->get('system.debugger.log.enabled') ? LOG_DIR : null; - Debugger::$maxDepth = $this->config->get('system.debugger.max_depth'); - - // Switch debugger into development mode if configured - if ($this->config->get('system.debugger.enabled')) { - if ($this->config->get('system.debugger.strict')) { - Debugger::$strictMode = true; - } - - if (function_exists('ini_set')) { - ini_set('display_errors', true); - } - Debugger::$productionMode = Debugger::DEVELOPMENT; - } - - // Get the Caching setup - $this->cache = Registry::get('Cache'); - $this->cache->init(); - - // Get Plugins - $plugins = new Plugins(); - $this->plugins = $plugins->load(); $this->fireEvent('onAfterInitPlugins'); - // Get current theme and hook it into plugins. - $themes = new Themes(); - $this->plugins['Theme'] = $themes->load(); + $this['Twig']->init(); + $this['Pages']->init(); - // Get twig object - $this->twig = Registry::get('Twig'); - $this->twig->init(); - - // Get all the Pages that Grav knows about - $this->pages = Registry::get('Pages'); - $this->pages->init(); $this->fireEvent('onAfterGetPages'); - // Get the taxonomy and set it on the grav object - $this->taxonomy = Registry::get('Taxonomy'); - - // Get current page - $this->page = $this->pages->dispatch($this->uri->route()); $this->fireEvent('onAfterGetPage'); // If there's no page, throw exception - if (!$this->page) { + if (!$this['Page']) { throw new \RuntimeException('Page Not Found', 404); } // Process whole page as required - $this->output = $this->twig->processSite($this->uri->extension()); + $this->output = $this['Twig']->processSite($this['Uri']->extension()); + $this->fireEvent('onAfterGetOutput'); // Set the header type @@ -146,7 +123,9 @@ class Grav extends Getters */ public function redirect($route, $code = 303) { - header("Location: " . rtrim($this->uri->rootUrl(), '/') .'/'. trim($route, '/'), true, $code); + /** @var Uri $uri */ + $uri = $this['Uri']; + header("Location: " . rtrim($uri->rootUrl(), '/') .'/'. trim($route, '/'), true, $code); exit(); } @@ -178,19 +157,9 @@ class Grav extends Getters */ public function header() { - header('Content-type: ' . $this->mime($this->uri->extension())); - } - - /** - * Log a message. - * - * @param string $message - */ - protected static function log($message) - { - if (Debugger::$logDirectory) { - Debugger::log(sprintf($message, Debugger::timer() * 1000)); - } + /** @var Uri $uri */ + $uri = $this['Uri']; + header('Content-type: ' . $this->mime($uri->extension())); } /** @@ -202,16 +171,26 @@ class Grav extends Getters $hook_id = array_shift($args); $no_timing_hooks = array('onAfterPageProcessed','onAfterFolderProcessed', 'onAfterCollectionProcessed'); - if (!empty($this->plugins)) { - foreach ($this->plugins as $plugin) { + /** @var Plugins $plugins */ + $plugins = $this['Plugins']; + + if (!empty($plugins)) { + foreach ($plugins as $plugin) { if (is_callable(array($plugin, $hook_id))) { call_user_func_array(array($plugin, $hook_id), $args); } } } - if ($this->config && $this->config->get('system.debugger.log.timing') && !in_array($hook_id, $no_timing_hooks)) { - static::log($hook_id.': %f ms'); + if (isset($this['Debugger'])) { + /** @var Config $config */ + $config = $this['Config']; + + if ($config && $config->get('system.debugger.log.timing') && !in_array($hook_id, $no_timing_hooks)) { + /** @var Debugger $debugger */ + $debugger = $this['Debugger']; + $debugger->log($hook_id.': %f ms'); + } } } } diff --git a/system/src/Grav/Common/Page/Collection.php b/system/src/Grav/Common/Page/Collection.php index 857675af6..2d5bf4766 100644 --- a/system/src/Grav/Common/Page/Collection.php +++ b/system/src/Grav/Common/Page/Collection.php @@ -1,7 +1,8 @@ params = $params; - $this->pages = $pages ? $pages : Registry::get('Pages'); + $this->pages = $pages ? $pages : Grav::instance()['Pages']; } public function params() diff --git a/system/src/Grav/Common/Page/Media.php b/system/src/Grav/Common/Page/Media.php index 4dcddc78f..fbfc47365 100644 --- a/system/src/Grav/Common/Page/Media.php +++ b/system/src/Grav/Common/Page/Media.php @@ -2,9 +2,8 @@ namespace Grav\Common\Page; use Grav\Common\Getters; -use Grav\Common\Registry; -use Grav\Config; -use Symfony\Component\Yaml\Yaml; +use Grav\Common\Grav; +use Grav\Common\Config; /** * Media is a holder object that contains references to the media of page. This object is created and @@ -76,7 +75,7 @@ class Media extends Getters $basename = implode('.', $parts); /** @var Config $config */ - $config = Registry::get('Config'); + $config = Grav::instance()['Config']; // Check if medium type has been configured. $params = $config->get("media.{$ext}"); diff --git a/system/src/Grav/Common/Page/Medium.php b/system/src/Grav/Common/Page/Medium.php index f966af980..cefb90906 100644 --- a/system/src/Grav/Common/Page/Medium.php +++ b/system/src/Grav/Common/Page/Medium.php @@ -1,11 +1,11 @@ image) { $output = $this->image->cacheFile($this->type, $this->quality); @@ -172,7 +173,8 @@ class Medium extends Data } if ($this->linkTarget) { - $config = Registry::get('Config'); + /** @var Config $config */ + $config = Grav::instance()['Config']; $output = 'linkAttributes. ' class="'. $class . '">' . $output . ''; diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 310ade904..ca9a7033b 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -84,7 +84,7 @@ class Page public function __construct($array = array()) { /** @var Config $config */ - $config = Registry::get('Config'); + $config = Grav::instance()['Config']; $this->routable = true; $this->taxonomy = array(); @@ -231,7 +231,7 @@ class Page // Return calculated summary based on setting in site config file /** @var Config $config */ - $config = Registry::get('Config'); + $config = Grav::instance()['Config']; if (!$size && $config->get('site.summary.size')) { $size = $config->get('site.summary.size'); } @@ -271,7 +271,7 @@ class Page // Load cached content /** @var Cache $cache */ - $cache = Registry::get('Cache'); + $cache = Grav::instance()['Cache']; $cache_id = md5('page'.$this->id()); $content = $cache->fetch($cache_id); @@ -297,7 +297,7 @@ class Page // Do we need to process twig this time? if ($update_cache || $process_twig) { /** @var Twig $twig */ - $twig = Registry::get('Twig'); + $twig = Grav::instance()['Twig']; $content = $twig->processPage($this, $content); } } @@ -465,7 +465,7 @@ class Page public function blueprints() { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; return $pages->blueprints($this->template()); } @@ -544,7 +544,7 @@ class Page public function media($var = null) { /** @var Cache $cache */ - $cache = Registry::get('Cache'); + $cache = Grav::instance()['Cache']; if ($var) { $this->media = $var; @@ -762,7 +762,7 @@ class Page public function url($include_host = false) { /** @var Uri $uri */ - $uri = Registry::get('Uri'); + $uri = Grav::instance()['Uri']; $rootUrl = $uri->rootUrl($include_host); $url = $rootUrl.'/'.trim($this->route(), '/'); @@ -958,7 +958,7 @@ class Page } if (empty($this->max_count)) { /** @var Config $config */ - $config = Registry::get('Config'); + $config = Grav::instance()['Config']; $this->max_count = (int) $config->get('system.pages.list.count'); } return $this->max_count; @@ -1029,11 +1029,13 @@ class Page */ public function parent(Page $var = null) { - if ($var !== null) { - $this->parent = $var ? $var->path() : ''; + if ($var) { + $this->parent = $var->path(); + return $var; } + /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; return $pages->get($this->parent); } @@ -1046,7 +1048,7 @@ class Page public function children() { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; return $pages->children($this->path()); } @@ -1126,7 +1128,7 @@ class Page public function isFirst() { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; $parent = $pages->get($this->parent); if ($this->path() == array_values($parent->items)[0]) { @@ -1144,7 +1146,7 @@ class Page public function isLast() { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; $parent = $pages->get($this->parent); if ($this->path() == array_values($parent->items)[count($parent->items)-1]) { @@ -1183,7 +1185,7 @@ class Page public function adjacentSibling($direction = 1) { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; $parent = $pages->get($this->parent); $current = $this->slug(); @@ -1202,7 +1204,7 @@ class Page public function active() { /** @var Uri $uri */ - $uri = Registry::get('Uri'); + $uri = Grav::instance()['Uri']; if ($this->url() == $uri->url()) { return true; } @@ -1217,7 +1219,7 @@ class Page */ public function activeChild() { - $uri = Registry::get('Uri'); + $uri = Grav::instance()['Uri']; if (!$this->home() && (strpos($uri->url(), $this->url()) !== false)) { return true; } @@ -1258,7 +1260,7 @@ class Page public function find($url) { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; return $pages->dispatch($url); } @@ -1289,9 +1291,9 @@ class Page // TODO: MOVE THIS INTO SOMEWHERE ELSE? /** @var Uri $uri */ - $uri = Registry::get('Uri'); + $uri = Grav::instance()['Uri']; /** @var Config $config */ - $config = Registry::get('Config'); + $config = Grav::instance()['Config']; foreach ((array) $config->get('site.taxonomies') as $taxonomy) { if ($uri->param($taxonomy)) { @@ -1323,7 +1325,7 @@ class Page } /** @var Grav $grav */ - $grav = Registry::get('Grav'); + $grav = Grav::instance()['Grav']; // New Custom event to handle things like pagination. $grav->fireEvent('onAfterCollectionProcessed', $collection); @@ -1391,7 +1393,7 @@ class Page // @taxonomy: { category: [ blog, featured ], level: 1 } /** @var Taxonomy $taxonomy_map */ - $taxonomy_map = Registry::get('Taxonomy'); + $taxonomy_map = Grav::instance()['Taxonomy']; if (!empty($parts)) { $params = [implode('.', $parts) => $params]; @@ -1540,7 +1542,7 @@ class Page // Do reordering. if ($reorder && $this->order() != $this->_original->order()) { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; $parent = $this->parent(); diff --git a/system/src/Grav/Common/Page/Pages.php b/system/src/Grav/Common/Page/Pages.php index 2de156167..19932d0aa 100644 --- a/system/src/Grav/Common/Page/Pages.php +++ b/system/src/Grav/Common/Page/Pages.php @@ -5,7 +5,6 @@ use \Grav\Common\Filesystem\Folder; use \Grav\Common\Grav; use \Grav\Common\Config; use \Grav\Common\Data; -use \Grav\Common\Registry; use \Grav\Common\Utils; use \Grav\Common\Cache; use \Grav\Common\Taxonomy; @@ -20,11 +19,6 @@ class Pages */ protected $grav; - /** - * @var Config - */ - protected $config; - /** * @var array|Page[] */ @@ -55,14 +49,21 @@ class Pages */ protected $last_modified; + /** + * Constructor + * + * @params Grav $c + */ + public function __construct(Grav $c) + { + $this->grav = $c; + } + /** * Class initialization. Must be called before using this class. */ public function init() { - $this->grav = Registry::get('Grav'); - $this->config = Registry::get('Config'); - $this->buildPages(); } @@ -221,7 +222,10 @@ class Pages // If the page cannot be reached, look into site wide routes. if (!$all && (!$page || !$page->routable())) { - $route = $this->config->get("site.routes.{$url}"); + /** @var Config $config */ + $config = $this->grav['Config']; + + $route = $config->get("site.routes.{$url}"); if ($route) { $page = $this->dispatch($route, $all); } @@ -249,7 +253,10 @@ class Pages public function blueprints($type) { if (!isset($this->blueprints)) { - $this->blueprints = new Data\Blueprints(THEMES_DIR . $this->config->get('system.pages.theme') . '/blueprints/'); + /** @var Config $config */ + $config = $this->grav['Config']; + + $this->blueprints = new Data\Blueprints(THEMES_DIR . $config->get('system.pages.theme') . '/blueprints/'); } try { @@ -259,9 +266,7 @@ class Pages } if (!$blueprint->initialized) { - /** @var Grav $grav */ - $grav = Registry::get('Grav'); - $grav->fireEvent('onCreateBlueprint', $blueprint); + $this->grav->fireEvent('onCreateBlueprint', $blueprint); $blueprint->initialized = true; } @@ -306,7 +311,8 @@ class Pages static public function types() { /** @var Config $config */ - $config = Registry::get('Config'); + $config = Grav::instance()['Config']; + $blueprints = new Data\Blueprints(THEMES_DIR . $config->get('system.pages.theme') . '/blueprints/'); return $blueprints->types(); @@ -320,7 +326,7 @@ class Pages static public function parents() { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = Grav::instance()['Pages']; return $pages->getList(); } @@ -332,12 +338,16 @@ class Pages protected function buildPages() { $this->sort = array(); - if ($this->config->get('system.cache.enabled')) { + + /** @var Config $config */ + $config = $this->grav['Config']; + + if ($config->get('system.cache.enabled')) { /** @var Cache $cache */ - $cache = Registry::get('Cache'); + $cache = $this->grav['Cache']; /** @var Taxonomy $taxonomy */ - $taxonomy = Registry::get('Taxonomy'); - $last_modified = $this->config->get('system.cache.check.pages', true) + $taxonomy = $this->grav['Taxonomy']; + $last_modified = $config->get('system.cache.check.pages', true) ? Folder::lastModified(PAGES_DIR) : 0; $page_cache_id = md5(USER_DIR.$last_modified); @@ -370,16 +380,18 @@ class Pages * @throws \RuntimeException * @internal */ - protected function recurse($directory = PAGES_DIR, &$parent = null) + protected function recurse($directory = PAGES_DIR, Page &$parent = null) { $directory = rtrim($directory, DS); $iterator = new \DirectoryIterator($directory); $page = new Page; + $config = $this->grav['Config']; $page->path($directory); - $page->parent($parent); - $page->orderDir($this->config->get('system.pages.order.dir')); - $page->orderBy($this->config->get('system.pages.order.by')); + if ($parent) $page->parent($parent); + + $page->orderDir($config->get('system.pages.order.dir')); + $page->orderBy($config->get('system.pages.order.by')); // Add into instances if (!isset($this->instances[$page->path()])) { @@ -399,7 +411,7 @@ class Pages $page->init($file); - if ($this->config->get('system.pages.events.page')) { + if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onAfterPageProcessed', $page); } @@ -426,7 +438,7 @@ class Pages // set the last modified time on pages $this->lastModified($file->getMTime()); - if ($this->config->get('system.pages.events.page')) { + if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onAfterFolderProcessed', $page); } } @@ -444,7 +456,7 @@ class Pages protected function buildRoutes() { /** @var $taxonomy Taxonomy */ - $taxonomy = Registry::get('Taxonomy'); + $taxonomy = $this->grav['Taxonomy']; // Build routes and taxonomy map. /** @var $page Page */ @@ -465,8 +477,11 @@ class Pages } } + /** @var Config $config */ + $config = $this->grav['Config']; + // Alias and set default route to home page. - $home = trim($this->config->get('system.home.alias'), '/'); + $home = trim($config->get('system.home.alias'), '/'); if ($home && isset($this->routes['/' . $home])) { $this->routes['/'] = $this->routes['/' . $home]; $this->get($this->routes['/' . $home])->route('/'); diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 1be3f9e7a..c275c72e2 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -10,13 +10,13 @@ use Grav\Common\Filesystem\File; * @author RocketTheme * @license MIT */ -class Plugins +class Plugins extends Iterator { - /** - * @var array|Plugin[] - */ - protected $plugins; + protected $grav; + public function __construct(Grav $grav) { + $this->grav = $grav; + } /** * Recurses through the plugins directory creating Plugin objects for each plugin it finds. @@ -24,12 +24,13 @@ class Plugins * @return array|Plugin[] array of Plugin objects * @throws \RuntimeException */ - public function load() + public function init() { /** @var Config $config */ - $config = Registry::get('Config'); + $config = $this->grav['Config']; $plugins = (array) $config->get('plugins'); + $instances = ['Theme' => $this->grav['Themes']->load()]; foreach ($plugins as $plugin => $data) { if (empty($data['enabled'])) { // Only load enabled plugins. @@ -50,16 +51,16 @@ class Plugins throw new \RuntimeException(sprintf("Plugin '%s' class not found!", $plugin)); } - $this->plugins[$pluginClass] = new $pluginClass($config); + $instances[$pluginClass] = new $pluginClass($config); } - return $this->plugins; + return $instances; } public function add($plugin) { if (is_object($plugin)) { - $this->plugins[get_class($plugin)] = $plugin; + $this->items[get_class($plugin)] = $plugin; } } diff --git a/system/src/Grav/Common/Registry.php b/system/src/Grav/Common/Registry.php index 59fbe5d7a..97e8ce686 100644 --- a/system/src/Grav/Common/Registry.php +++ b/system/src/Grav/Common/Registry.php @@ -7,18 +7,10 @@ namespace Grav\Common; * * @author RocketTheme * @license MIT + * @deprecated */ class Registry { - /** - * @var array - */ - private $registry = array(); - - /** - * @var Registry - */ - private static $instance = null; /** * Return global instance. @@ -27,11 +19,8 @@ class Registry */ public static function instance() { - if (self::$instance === null) { - self::$instance = new Registry(); - } - - return self::$instance; + user_error(__METHOD__ . '()', E_USER_DEPRECATED); + return new Registry; } /** @@ -43,11 +32,9 @@ class Registry */ public static function get($key) { - if (!isset(self::$instance->registry[$key])) { - throw new \Exception("There is no entry for key " . $key); - } - - return self::$instance->registry[$key]; + user_error(__METHOD__ . '()', E_USER_DEPRECATED); + $instance = Grav::instance(); + return $instance[$key]; } /** @@ -73,11 +60,9 @@ class Registry */ public function store($key, $value) { - if (isset($this->registry[$key])) { - throw new \Exception("There is already an entry for key " . $key); - } - - $this->registry[$key] = $value; + user_error(__CLASS__ . '::' . __METHOD__ . '()', E_USER_DEPRECATED); + $instance = Grav::instance(); + $instance[$key] = $value; } /** @@ -89,10 +74,8 @@ class Registry */ public function retrieve($key) { - if (!isset($this->registry[$key])) { - throw new \Exception("There is no entry for key " . $key); - } - - return $this->registry[$key]; + user_error(__CLASS__ . '::' . __METHOD__ . '()', E_USER_DEPRECATED); + $instance = Grav::instance(); + return $instance[$key]; } } diff --git a/system/src/Grav/Common/Taxonomy.php b/system/src/Grav/Common/Taxonomy.php index 3143e2d4e..93e1eca62 100644 --- a/system/src/Grav/Common/Taxonomy.php +++ b/system/src/Grav/Common/Taxonomy.php @@ -26,13 +26,15 @@ use \Grav\Common\Page; class Taxonomy { protected $taxonomy_map; + protected $grav; /** * Constructor that resets the map */ - public function __construct() + public function __construct(Grav $grav) { $this->taxonomy_map = array(); + $this->grav = $grav; } /** @@ -48,7 +50,8 @@ class Taxonomy $page_taxonomy = $page->taxonomy(); } - $config = Registry::get('Config'); + /** @var Config $config */ + $config = $this->grav['Config']; if ($config->get('site.taxonomies') && count($page_taxonomy) > 0) { foreach ((array) $config->get('site.taxonomies') as $taxonomy) { if (isset($page_taxonomy[$taxonomy])) { diff --git a/system/src/Grav/Common/Themes.php b/system/src/Grav/Common/Themes.php index f21e8faac..fcc8ed6fc 100644 --- a/system/src/Grav/Common/Themes.php +++ b/system/src/Grav/Common/Themes.php @@ -11,12 +11,22 @@ use Grav\Common\Filesystem\File; */ class Themes { + /** + * @var Grav + */ + protected $grav; + + public function __construct(Grav $grav) + { + $this->grav = $grav; + } + /** * Return list of all theme data with their blueprints. * * @return array|Data\Data[] */ - static public function all() + public function all() { $list = array(); $iterator = new \DirectoryIterator(THEMES_DIR); @@ -43,7 +53,7 @@ class Themes * @return Data\Data * @throws \RuntimeException */ - static public function get($type) + public function get($type) { if (!$type) { throw new \RuntimeException('Theme name not provided.'); @@ -77,7 +87,8 @@ class Themes public function load($name = null) { if (!$name) { - $config = Registry::get('Config'); + /** @var Config $config */ + $config = $this->grav['Config']; $name = $config->get('system.pages.theme'); } diff --git a/system/src/Grav/Common/Twig.php b/system/src/Grav/Common/Twig.php index f0c88bf5c..9dc8bb340 100644 --- a/system/src/Grav/Common/Twig.php +++ b/system/src/Grav/Common/Twig.php @@ -43,6 +43,11 @@ class Twig */ public $twig_vars; + /** + * @var array + */ + public $twig_paths; + /** * @var string */ @@ -58,68 +63,70 @@ class Twig */ protected $loaderArray; + /** + * Constructor + */ + public function __construct(Grav $grav) + { + $this->grav = $grav; + } + /** * Twig initialization that sets the twig loader chain, then the environment, then extensions * and also the base set of twig vars */ public function init() { - if (!isset($this->twig)) { + // get Grav and Config + $this->config = $this->grav['Config']; + $this->uri = $this->grav['Uri']; + $this->taxonomy = $this->grav['Taxonomy']; - // get Grav and Config - $this->grav = Registry::get('Grav'); - $this->config = $this->grav->config; - $this->uri = Registry::get('Uri'); - $this->taxonomy = Registry::get('Taxonomy'); + $this->twig_paths = array(THEMES_DIR . $this->config->get('system.pages.theme') . '/templates'); + $this->grav->fireEvent('onAfterTwigTemplatesPaths'); + $this->loader = new \Twig_Loader_Filesystem($this->twig_paths); + $this->loaderArray = new \Twig_Loader_Array(array()); + $loader_chain = new \Twig_Loader_Chain(array($this->loaderArray, $this->loader)); - $this->twig_paths = array(THEMES_DIR . $this->config->get('system.pages.theme') . '/templates'); - $this->grav->fireEvent('onAfterTwigTemplatesPaths'); - - $this->loader = new \Twig_Loader_Filesystem($this->twig_paths); - $this->loaderArray = new \Twig_Loader_Array(array()); - $loader_chain = new \Twig_Loader_Chain(array($this->loaderArray, $this->loader)); - - $params = $this->config->get('system.twig'); - if (!empty($params['cache'])) { - $params['cache'] = CACHE_DIR; - } - - $this->twig = new \Twig_Environment($loader_chain, $params); - $this->grav->fireEvent('onAfterTwigInit'); - - // set default date format if set in config - if ($this->config->get('system.pages.dateformat.long')) { - $this->twig->getExtension('core')->setDateFormat($this->config->get('system.pages.dateformat.long')); - } - // enable the debug extension if required - if ($this->config->get('system.twig.debug')) { - $this->twig->addExtension(new \Twig_Extension_Debug()); - } - $this->twig->addExtension(new TwigExtension()); - $this->grav->fireEvent('onAfterTwigExtensions'); - - $baseUrlAbsolute = $this->config->get('system.base_url_absolute'); - $baseUrlRelative = $this->config->get('system.base_url_relative'); - $theme = $this->config->get('system.pages.theme'); - $themeUrl = $baseUrlRelative .'/'. USER_PATH . basename(THEMES_DIR) .'/'. $theme; - - // Set some standard variables for twig - $this->twig_vars = array( - 'config' => $this->config, - 'uri' => $this->uri, - 'base_dir' => rtrim(ROOT_DIR, '/'), - 'base_url_absolute' => $baseUrlAbsolute, - 'base_url_relative' => $baseUrlRelative, - 'theme_dir' => THEMES_DIR . $theme, - 'theme_url' => $themeUrl, - 'site' => $this->config->get('site'), - 'stylesheets' => array(), - 'scripts' => array(), - 'taxonomy' => $this->taxonomy, - ); - + $params = $this->config->get('system.twig'); + if (!empty($params['cache'])) { + $params['cache'] = CACHE_DIR; } + + $this->twig = new \Twig_Environment($loader_chain, $params); + $this->grav->fireEvent('onAfterTwigInit'); + + // set default date format if set in config + if ($this->config->get('system.pages.dateformat.long')) { + $this->twig->getExtension('core')->setDateFormat($this->config->get('system.pages.dateformat.long')); + } + // enable the debug extension if required + if ($this->config->get('system.twig.debug')) { + $this->twig->addExtension(new \Twig_Extension_Debug()); + } + $this->twig->addExtension(new TwigExtension()); + $this->grav->fireEvent('onAfterTwigExtensions'); + + $baseUrlAbsolute = $this->config->get('system.base_url_absolute'); + $baseUrlRelative = $this->config->get('system.base_url_relative'); + $theme = $this->config->get('system.pages.theme'); + $themeUrl = $baseUrlRelative .'/'. USER_PATH . basename(THEMES_DIR) .'/'. $theme; + + // Set some standard variables for twig + $this->twig_vars = array( + 'config' => $this->config, + 'uri' => $this->uri, + 'base_dir' => rtrim(ROOT_DIR, '/'), + 'base_url_absolute' => $baseUrlAbsolute, + 'base_url_relative' => $baseUrlRelative, + 'theme_dir' => THEMES_DIR . $theme, + 'theme_url' => $themeUrl, + 'site' => $this->config->get('site'), + 'stylesheets' => array(), + 'scripts' => array(), + 'taxonomy' => $this->taxonomy, + ); } /** @@ -161,7 +168,6 @@ class Twig */ public function processPage(Page $item, $content = null) { - $this->init(); $content = $content !== null ? $content : $item->content(); // override the twig header vars for local resolution @@ -194,8 +200,6 @@ class Twig */ public function processString($string, array $vars = array()) { - $this->init(); - // override the twig header vars for local resolution $this->grav->fireEvent('onAfterTwigVars'); $vars += $this->twig_vars; @@ -213,17 +217,15 @@ class Twig * * @param string $format Output format (defaults to HTML). * @return string the rendered output - * @throws \Twig_Error_Loader + * @throws \RuntimeException */ public function processSite($format = null) { - $this->init(); - // set the page now its been processed $this->grav->fireEvent('onAfterTwigSiteVars'); $twig_vars = $this->twig_vars; - $pages = $this->grav->pages; - $page = $this->grav->page; + $pages = $this->grav['Pages']; + $page = $this->grav['Page']; $twig_vars['pages'] = $pages->root(); $twig_vars['page'] = $page; diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 09e564ebd..977128b88 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -51,6 +51,7 @@ abstract class Utils * @return string */ public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) { + $open_tags = array(); if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { @@ -59,7 +60,6 @@ abstract class Utils // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); - $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output diff --git a/system/src/Grav/Component/DI/Container.php b/system/src/Grav/Component/DI/Container.php new file mode 100644 index 000000000..59fafe882 --- /dev/null +++ b/system/src/Grav/Component/DI/Container.php @@ -0,0 +1,6 @@ +