diff --git a/admin.php b/admin.php index 9f2e7aed..8f831585 100644 --- a/admin.php +++ b/admin.php @@ -1,13 +1,12 @@ ['onPluginsInitialized', 1000] + ]; + } + /** * Initialize administration plugin if admin path matches. * * Disables system cache. */ - public function onAfterInitPlugins() + public function onPluginsInitialized() { $route = $this->config->get('plugins.admin.route'); @@ -49,12 +57,17 @@ class AdminPlugin extends Plugin return; } - $this->uri = Registry::get('Uri'); + $this->uri = $this->grav['uri']; $base = '/' . trim($route, '/'); // Only activate admin if we're inside the admin path. if (substr($this->uri->route(), 0, strlen($base)) == $base) { - $this->active = true; + $this->enable([ + 'onPagesInitialized' => ['onPagesInitialized', 1000], + 'onPageInitialized' => ['onPageInitialized', 1000], + 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], + 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000] + ]); // Disable system caching. $this->config->set('system.cache.enabled', false); @@ -76,40 +89,31 @@ class AdminPlugin extends Plugin // Initialize admin class. require_once __DIR__ . '/classes/admin.php'; - $this->admin = new Admin($base, $this->template, $this->route); + $this->admin = new Admin($this->grav, $base, $this->template, $this->route); - // And store the class into registry. - $registry = Registry::instance(); - $registry->store('Admin', $this->admin); + // And store the class into DI container. + $this->grav['admin'] = $this->admin; } } /** * Sets longer path to the home page allowing us to have list of pages when we enter to pages section. */ - public function onAfterGetPages() + public function onPagesInitialized() { - if (!$this->active) { - return; - } - // Set original route for the home page. $home = '/' . trim($this->config->get('system.home.alias'), '/'); /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = $this->grav['pages']; $pages->dispatch('/', true)->route($home); } /** * Main administration controller. */ - public function onAfterGetPage() + public function onPageInitialized() { - if (!$this->active) { - return; - } - // Set page if user hasn't been authorised. if (!$this->admin->authorise()) { $this->template = $this->admin->user ? 'denied' : 'login'; @@ -122,7 +126,7 @@ class AdminPlugin extends Plugin $task = !empty($post['task']) ? $post['task'] : $this->uri->param('task'); if ($task) { require_once __DIR__ . '/classes/controller.php'; - $controller = new AdminController($this->template, $task, $this->route, $post); + $controller = new AdminController($this->grav, $this->template, $task, $this->route, $post); $success = $controller->execute(); $controller->redirect(); } elseif ($this->template == 'logs' && $this->route) { @@ -132,39 +136,34 @@ class AdminPlugin extends Plugin } /** @var Grav $grav */ - $grav = Registry::get('Grav'); + $grav = $this->grav; // Finally create admin page. $page = new Page; $page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$this->template}.md")); $page->slug(basename($this->template)); - $grav->page = $page; + + unset($grav['page']); + $grav['page'] = $page; } /** * Add twig paths to plugin templates. */ - public function onAfterTwigTemplatesPaths() + public function onTwigTemplatePaths() { - if (!$this->active) { - return; - } - - $twig = Registry::get('Twig'); + $twig = $this->grav['twig']; $twig->twig_paths = array(__DIR__ . '/theme/templates'); } /** * Set all twig variables for generating output. */ - public function onAfterSiteTwigVars() + public function onTwigSiteVariables() { - if (!$this->active) { - return; - } - - $theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/' . basename(__DIR__) . '/theme'; - $twig = Registry::get('Twig'); + // TODO: use real plugin name instead + $theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/admin/theme'; + $twig = $this->grav['twig']; $twig->template = $this->template . '.html.twig'; $twig->twig_vars['location'] = $this->template; diff --git a/classes/admin.php b/classes/admin.php index 2679778b..f752c52d 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -6,7 +6,6 @@ use Grav\Common\User\Authentication; use Grav\Common\Filesystem\File; use Grav\Common\Grav; use Grav\Common\Plugins; -use Grav\Common\Registry; use Grav\Common\Session; use Grav\Common\Themes; use Grav\Common\Uri; @@ -16,6 +15,11 @@ use Grav\Common\Data; class Admin { + /** + * @var Grav + */ + public $grav; + /** * @var Uri $uri */ @@ -64,18 +68,20 @@ class Admin /** * Constructor. * + * @param Grav $grav * @param string $base * @param string $location * @param string $route */ - public function __construct($base, $location, $route) + public function __construct(Grav $grav, $base, $location, $route) { + $this->grav = $grav; $this->base = $base; $this->location = $location; $this->route = $route; /** @var Uri uri */ - $this->uri = Registry::get('Uri'); + $this->uri = $this->grav['uri']; // TODO: add session timeout into configuration $this->session = new Session\Session(1800, $this->uri->rootUrl(false) . $base); @@ -148,7 +154,7 @@ class Admin $this->user = $this->session->user = $user; /** @var Grav $grav */ - $grav = Registry::get('Grav'); + $grav = $this->grav; $grav->redirect($this->uri->route()); } } @@ -285,7 +291,7 @@ class Admin */ public function themes() { - return Themes::all(); + return $this->grav['themes']->all(); } /** @@ -324,7 +330,7 @@ class Admin protected function getPage($path) { /** @var Pages $pages */ - $pages = Registry::get('Pages'); + $pages = $this->grav['pages']; if ($path && $path[0] != '/') { $path = "/{$path}"; @@ -371,6 +377,6 @@ class Admin */ public static function route() { - return dirname('/' . Registry::get('Admin')->route); + return dirname('/' . Grav::instance()['admin']->route); } } diff --git a/classes/controller.php b/classes/controller.php index 4e43a686..4f69a95c 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -3,7 +3,7 @@ namespace Grav\Plugin; use Grav\Common\Config; use Grav\Common\Filesystem\Folder; -use Grav\Common\Registry; +use Grav\Common\Grav; use Grav\Common\Themes; use Grav\Common\Uri; use Grav\Common\Data; @@ -11,6 +11,11 @@ use Grav\Common\Page; class AdminController { + /** + * @var Grav + */ + public $grav; + /** * @var string */ @@ -47,18 +52,20 @@ class AdminController protected $redirectCode; /** + * @param Grav $grav * @param string $view * @param string $task * @param string $route * @param array $post */ - public function __construct($view, $task, $route, $post) + public function __construct(Grav $grav, $view, $task, $route, $post) { + $this->grav = $grav; $this->view = $view; $this->task = $task ? $task : 'display'; $this->post = $this->getPost($post); $this->route = $route; - $this->admin = Registry::get('Admin'); + $this->admin = $this->grav['admin']; } /** @@ -97,8 +104,7 @@ class AdminController $path = trim(substr($this->redirect, 0, strlen($base)) == $base ? substr($this->redirect, strlen($base)) : $this->redirect, '/'); - $grav = Registry::get('Grav'); - $grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode); + $this->grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode); } /** @@ -170,7 +176,7 @@ class AdminController // Force configuration reload and save. /** @var Config $config */ - $config = Registry::get('Config'); + $config = $this->grav['config']; $config->reload()->save(); // TODO: find out why reload and save doesn't always update the object itself (and remove this workaround). @@ -193,7 +199,7 @@ class AdminController // Special handler for pages data. if ($this->view == 'pages') { /** @var Page\Pages $pages */ - $pages = Registry::get('Pages'); + $pages = $this->grav['pages']; // Find new parent page in order to build the path. $route = !isset($data['route']) ? dirname($this->admin->route) : $data['route']; @@ -267,7 +273,7 @@ class AdminController try { /** @var Page\Pages $pages */ - $pages = Registry::get('Pages'); + $pages = $this->grav['pages']; $data = $this->post; // Find new parent page in order to build the path. @@ -336,7 +342,7 @@ class AdminController } /** @var Uri $uri */ - $uri = Registry::get('Uri'); + $uri = $this->grav['uri']; try { $page = $this->admin->page();