diff --git a/README.md b/README.md index 01ce1fb0d..df4706876 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ What you mainly want to know is that: * [What is Grav?](http://learn.getgrav.org/basics/what-is-grav) * [Install](http://learn.getgrav.org/basics/installation) Grav in few seconds * Understand the [Configuration](http://learn.getgrav.org/basics/grav-configuration) -* Take a peek at our available free [Skeletons](http://getgrav.org/downloads/skeletons#extras) +* Take a peek at our available free [Skeletons](http://getgrav.org/downloads/skeletons) * If you have questions, jump on our [Gitter Room](https://gitter.im/getgrav/grav)! * Have fun! diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index 5790370fc..abc0e84f9 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -834,6 +834,7 @@ class Assets public function resetJs() { $this->js = []; + $this->inline_js = []; return $this; } @@ -846,14 +847,27 @@ class Assets public function resetCss() { $this->css = []; + $this->inline_css = []; return $this; } /** - * Add all CSS assets within $directory (relative to public dir). + * Add all JavaScript assets within $directory * - * @param string $directory Relative to $this->public_dir + * @param string $directory Relative to the Grav root path, or a stream identifier + * + * @return $this + */ + public function addDirJs($directory) + { + return $this->addDir($directory, self::JS_REGEX); + } + + /** + * Add all CSS assets within $directory + * + * @param string $directory Relative to the Grav root path, or a stream identifier * * @return $this */ @@ -865,7 +879,7 @@ class Assets /** * Add all assets matching $pattern within $directory. * - * @param string $directory Relative to $this->public_dir + * @param string $directory Relative to the Grav root path, or a stream identifier * @param string $pattern (regex) * * @return $this @@ -873,13 +887,15 @@ class Assets */ public function addDir($directory, $pattern = self::DEFAULT_REGEX) { - // Check if public_dir exists - if (!is_dir($this->assets_dir)) { - throw new Exception('Assets: Public dir not found'); + $root_dir = rtrim(ROOT_DIR, '/'); + + // Check if $directory is a stream. + if (strpos($directory, '://')) { + $directory = self::$grav['locator']->findResource($directory, null); } // Get files - $files = $this->rglob($this->assets_dir . DIRECTORY_SEPARATOR . $directory, $pattern, $this->assets_dir); + $files = $this->rglob($root_dir . DIRECTORY_SEPARATOR . $directory, $pattern, $root_dir . '/'); // No luck? Nothing to do if (!$files) { @@ -888,27 +904,23 @@ class Assets // Add CSS files if ($pattern === self::CSS_REGEX) { - $this->css = array_unique(array_merge($this->css, $files)); + foreach ($files as $file) { + $this->addCss($file); + } return $this; } // Add JavaScript files if ($pattern === self::JS_REGEX) { - $this->js = array_unique(array_merge($this->js, $files)); + foreach ($files as $file) { + $this->addJs($file); + } return $this; } - // Unknown pattern. We must poll to know the extension :( + // Unknown pattern. foreach ($files as $asset) { - $info = pathinfo($asset); - if (isset($info['extension'])) { - $ext = strtolower($info['extension']); - if ($ext === 'css' && !in_array($asset, $this->css)) { - $this->css[] = $asset; - } elseif ($ext === 'js' && !in_array($asset, $this->js)) { - $this->js[] = $asset; - } - } + $this->add($asset); } return $this; @@ -1127,18 +1139,6 @@ class Assets return $files; } - /** - * Add all JavaScript assets within $directory. - * - * @param string $directory Relative to $this->public_dir - * - * @return $this - */ - public function addDirJs($directory) - { - return $this->addDir($directory, self::JS_REGEX); - } - /** * Sets the state of CSS Pipeline * diff --git a/system/src/Grav/Common/Browser.php b/system/src/Grav/Common/Browser.php index f39ac1539..cf29ae62d 100644 --- a/system/src/Grav/Common/Browser.php +++ b/system/src/Grav/Common/Browser.php @@ -13,6 +13,9 @@ class Browser { protected $useragent = []; + /** + * Browser constructor. + */ public function __construct() { try { diff --git a/system/src/Grav/Common/Cache.php b/system/src/Grav/Common/Cache.php index f7a6d4bae..d3da1cc4a 100644 --- a/system/src/Grav/Common/Cache.php +++ b/system/src/Grav/Common/Cache.php @@ -1,7 +1,7 @@ connect($this->config->get('system.cache.memcache.server','localhost'), $this->config->get('system.cache.memcache.port', 11211)); - $driver = new \Doctrine\Common\Cache\MemcacheCache(); + $driver = new DoctrineCache\MemcacheCache(); $driver->setMemcache($memcache); break; @@ -180,12 +181,12 @@ class Cache extends Getters $redis->connect($this->config->get('system.cache.redis.server','localhost'), $this->config->get('system.cache.redis.port', 6379)); - $driver = new \Doctrine\Common\Cache\RedisCache(); + $driver = new DoctrineCache\RedisCache(); $driver->setRedis($redis); break; default: - $driver = new \Doctrine\Common\Cache\FilesystemCache($this->cache_dir); + $driver = new DoctrineCache\FilesystemCache($this->cache_dir); break; } diff --git a/system/src/Grav/Common/Composer.php b/system/src/Grav/Common/Composer.php index 5efde8912..804bc4c15 100644 --- a/system/src/Grav/Common/Composer.php +++ b/system/src/Grav/Common/Composer.php @@ -35,6 +35,11 @@ class Composer return $path; } + /** + * Return the composer executable file path + * + * @return string + */ public static function getComposerExecutor() { $executor = PHP_BINARY . ' '; diff --git a/system/src/Grav/Common/Debugger.php b/system/src/Grav/Common/Debugger.php index 64824ff69..3a91ca502 100644 --- a/system/src/Grav/Common/Debugger.php +++ b/system/src/Grav/Common/Debugger.php @@ -1,8 +1,10 @@ debugbar = new StandardDebugBar(); $this->debugbar['time']->addMeasure('Loading', $this->debugbar['time']->getRequestStartTime(), microtime(true)); } + /** + * Initialize the debugger + * + * @return $this + * @throws \DebugBar\DebugBarException + */ public function init() { $this->grav = Grav::instance(); + $this->config = $this->grav['config']; if ($this->enabled()) { - $this->debugbar->addCollector(new \DebugBar\DataCollector\ConfigCollector((array)$this->grav['config']->get('system'), 'Config')); - $this->debugbar->addCollector(new \DebugBar\DataCollector\ConfigCollector((array)$this->grav['config']->get('plugins'), 'Plugins')); + $this->debugbar->addCollector(new ConfigCollector((array)$this->config->get('system'), 'Config')); + $this->debugbar->addCollector(new ConfigCollector((array)$this->config->get('plugins'), 'Plugins')); } return $this; } + /** + * Set/get the enabled state of the debugger + * + * @param bool $state If null, the method returns the enabled value. If set, the method sets the enabled state + * @return null + */ public function enabled($state = null) { if (isset($state)) { $this->enabled = $state; } else { if (!isset($this->enabled)) { - $this->enabled = $this->grav['config']->get('system.debugger.enabled'); + $this->enabled = $this->config->get('system.debugger.enabled'); } } return $this->enabled; } + /** + * Add the debugger assets to the Grav Assets + * + * @return $this + */ public function addAssets() { if ($this->enabled()) { + /** @var Assets $assets */ $assets = $this->grav['assets']; // Add jquery library @@ -72,17 +105,36 @@ class Debugger return $this; } + /** + * Adds a data collector + * + * @param $collector + * @return $this + * @throws \DebugBar\DebugBarException + */ public function addCollector($collector) { $this->debugbar->addCollector($collector); return $this; } + /** + * Returns a data collector + * + * @param $collector + * @return \DebugBar\DataCollector\DataCollectorInterface + * @throws \DebugBar\DebugBarException + */ public function getCollector($collector) { return $this->debugbar->getCollector($collector); } + /** + * Displays the debug bar + * + * @return $this + */ public function render() { if ($this->enabled()) { @@ -91,31 +143,58 @@ class Debugger return $this; } + /** + * Sends the data through the HTTP headers + * + * @return $this + */ public function sendDataInHeaders() { $this->debugbar->sendDataInHeaders(); return $this; } + /** + * Start a timer with an associated name and description + * + * @param $name + * @param string|null $description + * + * @return $this + */ public function startTimer($name, $description = null) { - if ($name[0] == '_' || $this->grav['config']->get('system.debugger.enabled')) { + if ($name[0] == '_' || $this->config->get('system.debugger.enabled')) { $this->debugbar['time']->startMeasure($name, $description); $this->timers[] = $name; } return $this; } + /** + * Stop the named timer + * + * @param string $name + * @return $this + */ public function stopTimer($name) { - if (in_array($name, $this->timers) && ($name[0] == '_' || $this->grav['config']->get('system.debugger.enabled'))) { + if (in_array($name, $this->timers) && ($name[0] == '_' || $this->config->get('system.debugger.enabled'))) { $this->debugbar['time']->stopMeasure($name); } return $this; } - + /** + * Dump variables into the Messages tab of the Debug Bar + * + * @param $message + * @param string $label + * @param bool $isString + * + * @return $this + */ public function addMessage($message, $label = 'info', $isString = true) { if ($this->enabled()) { diff --git a/system/src/Grav/Common/Getters.php b/system/src/Grav/Common/Getters.php index 857dccaba..5e2372a34 100644 --- a/system/src/Grav/Common/Getters.php +++ b/system/src/Grav/Common/Getters.php @@ -140,7 +140,7 @@ abstract class Getters implements \ArrayAccess, \Countable return $this->{$var}; } else { $properties = (array) $this; - $list = array(); + $list = []; foreach ($properties as $property => $value) { if ($property[0] != "\0") $list[$property] = $value; } diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 1642211d2..df3e2d99f 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -1,8 +1,10 @@ isExternal($route)) { $url = $route; } else { - if ($this['config']->get('system.pages.redirect_trailing_slash', true)) - $url = rtrim($uri->rootUrl(), '/') .'/'. trim($route, '/'); // Remove trailing slash - else - $url = rtrim($uri->rootUrl(), '/') .'/'. ltrim($route, '/'); // Support trailing slash default routes + $url = rtrim($uri->rootUrl(), '/') .'/'; + + if ($this['config']->get('system.pages.redirect_trailing_slash', true)) { + $url .= trim($route, '/'); // Remove trailing slash + } else { + $url .= ltrim($route, '/'); // Support trailing slash default routes + } } header("Location: {$url}", true, $code); diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index d394b769c..b04ddd57f 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -986,8 +986,8 @@ class Page /** * Gets and sets the expires field. If not set will return the default * - * @param string $var The name of this page. - * @return string The name of this page. + * @param int $var The new expires value. + * @return int The expires value */ public function expires($var = null) { diff --git a/tests/unit/Grav/Common/AssetsTest.php b/tests/unit/Grav/Common/AssetsTest.php index ec6508a15..82f37ba22 100644 --- a/tests/unit/Grav/Common/AssetsTest.php +++ b/tests/unit/Grav/Common/AssetsTest.php @@ -2,6 +2,9 @@ use Codeception\Util\Fixtures; +/** + * Class AssetsTest + */ class AssetsTest extends \Codeception\TestCase\Test { /** @@ -38,22 +41,59 @@ class AssetsTest extends \Codeception\TestCase\Test $css = $assets->css(); $this->assertSame($css, '' . PHP_EOL); + $array = $assets->getCss(); + $this->assertSame(reset($array), [ + 'asset' => '/test.css', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'group' => 'head' + ]); + $assets->add('test.js'); $js = $assets->js(); $this->assertSame($js, '' . PHP_EOL); + $array = $assets->getCss(); + $this->assertSame(reset($array), [ + 'asset' => '/test.css', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'group' => 'head' + ]); + //test addCss(). Test adding asset to a separate group $assets->reset(); $assets->addCSS('test.css'); $css = $assets->css(); $this->assertSame($css, '' . PHP_EOL); + $array = $assets->getCss(); + $this->assertSame(reset($array), [ + 'asset' => '/test.css', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'group' => 'head' + ]); + //test addJs() $assets->reset(); $assets->addJs('test.js'); $js = $assets->js(); $this->assertSame($js, '' . PHP_EOL); + $array = $assets->getJs(); + $this->assertSame(reset($array), [ + 'asset' => '/test.js', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'loading' => '', + 'group' => 'head' + ]); + //Test CSS Groups $assets->reset(); $assets->addCSS('test.css', null, true, 'footer'); @@ -62,6 +102,15 @@ class AssetsTest extends \Codeception\TestCase\Test $css = $assets->css('footer'); $this->assertSame($css, '' . PHP_EOL); + $array = $assets->getCss(); + $this->assertSame(reset($array), [ + 'asset' => '/test.css', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'group' => 'footer' + ]); + //Test JS Groups $assets->reset(); $assets->addJs('test.js', null, true, null, 'footer'); @@ -70,17 +119,46 @@ class AssetsTest extends \Codeception\TestCase\Test $js = $assets->js('footer'); $this->assertSame($js, '' . PHP_EOL); + $array = $assets->getJs(); + $this->assertSame(reset($array), [ + 'asset' => '/test.js', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'loading' => '', + 'group' => 'footer' + ]); + //Test async / defer $assets->reset(); $assets->addJs('test.js', null, true, 'async', null); $js = $assets->js(); $this->assertSame($js, '' . PHP_EOL); + + $array = $assets->getJs(); + $this->assertSame(reset($array), [ + 'asset' => '/test.js', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'loading' => 'async', + 'group' => 'head' + ]); + $assets->reset(); $assets->addJs('test.js', null, true, 'defer', null); $js = $assets->js(); $this->assertSame($js, '' . PHP_EOL); - + $array = $assets->getJs(); + $this->assertSame(reset($array), [ + 'asset' => '/test.js', + 'priority' => 10, + 'order' => 0, + 'pipeline' => true, + 'loading' => 'defer', + 'group' => 'head' + ]); } public function testAddingAssetPropertiesWithArray() @@ -254,7 +332,142 @@ class AssetsTest extends \Codeception\TestCase\Test $assets->reset(); $assets->addInlineJs('alert("test")'); $js = $assets->js(); - $this->assertSame($js, PHP_EOL. '' . PHP_EOL); + $this->assertSame($js, + PHP_EOL . '' . PHP_EOL); } -} \ No newline at end of file + public function testGetCollections() + { + $assets = $this->assets(); + + $this->assertTrue(is_array($assets->getCollections())); + $this->assertTrue(in_array('jquery', array_keys($assets->getCollections()))); + $this->assertTrue(in_array('system://assets/jquery/jquery-2.x.min.js', $assets->getCollections())); + } + + public function testExists() + { + $assets = $this->assets(); + + $this->assertTrue($assets->exists('jquery')); + $this->assertFalse($assets->exists('another-unexisting-library')); + } + + public function testRegisterCollection() + { + $assets = $this->assets(); + + $assets->registerCollection('debugger', ['/system/assets/debugger.css']); + $this->assertTrue($assets->exists('debugger')); + $this->assertTrue(in_array('debugger', array_keys($assets->getCollections()))); + } + + public function testReset() + { + $assets = $this->assets(); + + $assets->addInlineJs('alert("test")'); + $assets->reset(); + $this->assertTrue(count($assets->js()) == 0); + + $assets->addAsyncJs('jquery'); + $assets->reset(); + + $this->assertTrue(count($assets->js()) == 0); + + $assets->addInlineCss('body { color: black }'); + $assets->reset(); + + $this->assertTrue(count($assets->css()) == 0); + + $assets->add('/system/assets/debugger.css', null, true); + $assets->reset(); + + $this->assertTrue(count($assets->css()) == 0); + } + + public function testResetJs() + { + $assets = $this->assets(); + + $assets->addInlineJs('alert("test")'); + $assets->resetJs(); + $this->assertTrue(count($assets->js()) == 0); + + $assets->addAsyncJs('jquery'); + $assets->resetJs(); + + $this->assertTrue(count($assets->js()) == 0); + } + + public function testResetCss() + { + $assets = $this->assets(); + + $this->assertTrue(count($assets->js()) == 0); + + $assets->addInlineCss('body { color: black }'); + $assets->resetCss(); + + $this->assertTrue(count($assets->css()) == 0); + + $assets->add('/system/assets/debugger.css', null, true); + $assets->resetCss(); + + $this->assertTrue(count($assets->css()) == 0); + } + + public function testAddDirCss() + { + $assets = $this->assets(); + $assets->reset(); + $assets->addDirCss('/system'); + + $this->assertTrue(is_array($assets->getCss())); + $this->assertTrue(count($assets->getCss()) > 0); + $this->assertTrue(is_array($assets->getJs())); + $this->assertTrue(count($assets->getJs()) == 0); + + $assets->reset(); + $assets->addDirCss('/system/assets'); + + $this->assertTrue(is_array($assets->getCss())); + $this->assertTrue(count($assets->getCss()) > 0); + $this->assertTrue(is_array($assets->getJs())); + $this->assertTrue(count($assets->getJs()) == 0); + + $assets->reset(); + $assets->addDirJs('/system'); + + $this->assertTrue(is_array($assets->getCss())); + $this->assertTrue(count($assets->getCss()) == 0); + $this->assertTrue(is_array($assets->getJs())); + $this->assertTrue(count($assets->getJs()) > 0); + + $assets->reset(); + $assets->addDirJs('/system/assets'); + + $this->assertTrue(is_array($assets->getCss())); + $this->assertTrue(count($assets->getCss()) == 0); + $this->assertTrue(is_array($assets->getJs())); + $this->assertTrue(count($assets->getJs()) > 0); + + $assets->reset(); + $assets->addDir('/system/assets'); + + $this->assertTrue(is_array($assets->getCss())); + $this->assertTrue(count($assets->getCss()) > 0); + $this->assertTrue(is_array($assets->getJs())); + $this->assertTrue(count($assets->getJs()) > 0); + + //Use streams + $assets->reset(); + $assets->addDir('system://assets'); + + $this->assertTrue(is_array($assets->getCss())); + $this->assertTrue(count($assets->getCss()) > 0); + $this->assertTrue(is_array($assets->getJs())); + $this->assertTrue(count($assets->getJs()) > 0); + + } +}