From 2d5f721301a7044f966c6d82b7ba92151ba4d7db Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 28 Jan 2016 09:52:30 +0100 Subject: [PATCH] :art: Improve type hints and coding style --- system/src/Grav/Common/Grav.php | 44 ++++++++++++++----- system/src/Grav/Common/Page/Media.php | 10 ++--- system/src/Grav/Common/Page/Medium/Medium.php | 2 +- system/src/Grav/Common/Uri.php | 6 ++- system/src/Grav/Common/Utils.php | 4 +- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index df3e2d99f..aad8eabcc 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -4,6 +4,7 @@ namespace Grav\Common; use Grav\Common\Config\Config; use Grav\Common\Language\Language; use Grav\Common\Page\Medium\ImageMedium; +use Grav\Common\Page\Medium\Medium; use Grav\Common\Page\Page; use Grav\Common\Page\Pages; use Grav\Common\Service\ConfigServiceProvider; @@ -79,45 +80,56 @@ class Grav extends Container $container->register(new ErrorServiceProvider); $container['uri'] = function ($c) { + /** @var Grav $c */ return new Uri($c); }; $container['task'] = function ($c) { + /** @var Grav $c */ return !empty($_POST['task']) ? $_POST['task'] : $c['uri']->param('task'); }; - $container['events'] = function ($c) { + $container['events'] = function () { return new EventDispatcher; }; $container['cache'] = function ($c) { + /** @var Grav $c */ return new Cache($c); }; $container['session'] = function ($c) { + /** @var Grav $c */ return new Session($c); }; - $container['plugins'] = function ($c) { + $container['plugins'] = function () { return new Plugins(); }; $container['themes'] = function ($c) { + /** @var Grav $c */ return new Themes($c); }; $container['twig'] = function ($c) { + /** @var Grav $c */ return new Twig($c); }; $container['taxonomy'] = function ($c) { + /** @var Grav $c */ return new Taxonomy($c); }; $container['language'] = function ($c) { + /** @var Grav $c */ return new Language($c); }; $container['pages'] = function ($c) { + /** @var Grav $c */ return new Pages($c); }; $container['assets'] = new Assets(); $container['page'] = function ($c) { + /** @var Grav $c */ + /** @var Pages $pages */ $pages = $c['pages']; /** @var Language $language */ @@ -152,7 +164,7 @@ class Grav extends Container if (!$page || !$page->routable()) { // Try fallback URL stuff... - $c->fallbackUrl($page, $path); + $c->fallbackUrl($path); // If no page found, fire event $event = $c->fireEvent('onPageNotFound'); @@ -165,20 +177,28 @@ class Grav extends Container } return $page; }; + $container['output'] = function ($c) { + /** @var Grav $c */ return $c['twig']->processSite($c['uri']->extension()); }; - $container['browser'] = function ($c) { + + $container['browser'] = function () { return new Browser(); }; $container['base_url_absolute'] = function ($c) { + /** @var Grav $c */ return $c['config']->get('system.base_url_absolute') ?: $c['uri']->rootUrl(true); }; + $container['base_url_relative'] = function ($c) { + /** @var Grav $c */ return $c['config']->get('system.base_url_relative') ?: $c['uri']->rootUrl(false); }; + $container['base_url'] = function ($c) { + /** @var Grav $c */ return $c['config']->get('system.absolute_urls') ? $c['base_url_absolute'] : $c['base_url_relative']; }; @@ -346,9 +366,9 @@ class Grav extends Container $language = $this['language']; if (!$this['uri']->isExternal($route) && $language->enabled() && $language->isIncludeDefaultLanguage()) { - return $this->redirect($language->getLanguage() . $route, $code); + $this->redirect($language->getLanguage() . $route, $code); } else { - return $this->redirect($route, $code); + $this->redirect($route, $code); } } @@ -491,10 +511,10 @@ class Grav extends Container /** * This attempts to find media, other files, and download them - * @param $page + * * @param $path */ - protected function fallbackUrl($page, $path) + protected function fallbackUrl($path) { /** @var Uri $uri */ $uri = $this['uri']; @@ -514,20 +534,22 @@ class Grav extends Container } $path_parts = pathinfo($path); + + /** @var Page $page */ $page = $this['pages']->dispatch($path_parts['dirname'], true); + if ($page) { $media = $page->media()->all(); - $parsed_url = parse_url(rawurldecode($uri->basename())); - $media_file = $parsed_url['path']; // if this is a media object, try actions first if (isset($media[$media_file])) { + /** @var Medium $medium */ $medium = $media[$media_file]; foreach ($uri->query(null, true) as $action => $params) { if (in_array($action, ImageMedium::$magic_actions)) { - call_user_func_array(array(&$medium, $action), explode(',', $params)); + call_user_func_array([&$medium, $action], explode(',', $params)); } } Utils::download($medium->path(), false); diff --git a/system/src/Grav/Common/Page/Media.php b/system/src/Grav/Common/Page/Media.php index e9eeff101..dd1a38110 100644 --- a/system/src/Grav/Common/Page/Media.php +++ b/system/src/Grav/Common/Page/Media.php @@ -20,11 +20,11 @@ class Media extends Getters protected $gettersVariable = 'instances'; protected $path; - protected $instances = array(); - protected $images = array(); - protected $videos = array(); - protected $audios = array(); - protected $files = array(); + protected $instances = []; + protected $images = []; + protected $videos = []; + protected $audios = []; + protected $files = []; /** * @param $path diff --git a/system/src/Grav/Common/Page/Medium/Medium.php b/system/src/Grav/Common/Page/Medium/Medium.php index 714eeb5e8..f1ce336fd 100644 --- a/system/src/Grav/Common/Page/Medium/Medium.php +++ b/system/src/Grav/Common/Page/Medium/Medium.php @@ -118,7 +118,7 @@ class Medium extends Data implements RenderableInterface * Return PATH to file. * * @param bool $reset - * @return string path to file + * @return string path to file */ public function path($reset = true) { diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index 6761c5e2c..4cbe6f8e4 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -349,8 +349,10 @@ class Uri /** * Return full query string or a single query attribute. * - * @param string $id Optional attribute. - * @return string + * @param string $id Optional attribute. Get a single query attribute if set + * @param bool $raw If true and $id is not set, return the full query array. Otherwise return the query string + * + * @return string|array Returns an array if $id = null and $raw = true */ public function query($id = null, $raw = false) { diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index eb88c1937..22941a428 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -214,8 +214,8 @@ abstract class Utils /** * Provides the ability to download a file to the browser * - * @param $file the full path to the file to be downloaded - * @param bool $force_download as opposed to letting browser choose if to download or render + * @param string $file the full path to the file to be downloaded + * @param bool $force_download as opposed to letting browser choose if to download or render */ public static function download($file, $force_download = true) {