Merge branch 'develop' of https://github.com/getgrav/grav into develop

This commit is contained in:
Andy Miller
2016-01-27 11:45:46 -07:00
10 changed files with 383 additions and 62 deletions

View File

@@ -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!

View File

@@ -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
*

View File

@@ -13,6 +13,9 @@ class Browser
{
protected $useragent = [];
/**
* Browser constructor.
*/
public function __construct()
{
try {

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Common;
use \Doctrine\Common\Cache\Cache as DoctrineCache;
use \Doctrine\Common\Cache as DoctrineCache;
use Grav\Common\Config\Config;
use Grav\Common\Filesystem\Folder;
@@ -31,10 +31,11 @@ class Cache extends Getters
protected $lifetime;
protected $now;
/** @var Config $config */
protected $config;
/**
* @var DoctrineCache
* @var DoctrineCache\CacheProvider
*/
protected $driver;
@@ -79,7 +80,7 @@ class Cache extends Getters
/**
* Constructor
*
* @params Grav $grav
* @param Grav $grav
*/
public function __construct(Grav $grav)
{
@@ -127,7 +128,7 @@ class Cache extends Getters
* If there is no config option for $driver in the config, or it's set to 'auto', it will
* pick the best option based on which cache extensions are installed.
*
* @return DoctrineCacheDriver The cache driver to use
* @return DoctrineCache\CacheProvider The cache driver to use
*/
public function getCacheDriver()
{
@@ -152,26 +153,26 @@ class Cache extends Getters
switch ($driver_name) {
case 'apc':
$driver = new \Doctrine\Common\Cache\ApcCache();
$driver = new DoctrineCache\ApcCache();
break;
case 'apcu':
$driver = new \Doctrine\Common\Cache\ApcuCache();
$driver = new DoctrineCache\ApcuCache();
break;
case 'wincache':
$driver = new \Doctrine\Common\Cache\WinCacheCache();
$driver = new DoctrineCache\WinCacheCache();
break;
case 'xcache':
$driver = new \Doctrine\Common\Cache\XcacheCache();
$driver = new DoctrineCache\XcacheCache();
break;
case 'memcache':
$memcache = new \Memcache();
$memcache->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;
}

View File

@@ -35,6 +35,11 @@ class Composer
return $path;
}
/**
* Return the composer executable file path
*
* @return string
*/
public static function getComposerExecutor()
{
$executor = PHP_BINARY . ' ';

View File

@@ -1,8 +1,10 @@
<?php
namespace Grav\Common;
use DebugBar\DataCollector\ConfigCollector;
use DebugBar\JavascriptRenderer;
use DebugBar\StandardDebugBar;
use Grav\Common\Config\Config;
/**
* Class Debugger
@@ -10,45 +12,76 @@ use DebugBar\StandardDebugBar;
*/
class Debugger
{
/** @var Grav $grav*/
protected $grav;
protected $debugbar;
/** @var Config $config */
protected $config;
/** @var JavascriptRenderer $renderer */
protected $renderer;
/** @var StandardDebugBar $debugbar */
protected $debugbar;
protected $enabled;
protected $timers = [];
/**
* Debugger constructor.
*/
public function __construct()
{
$this->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()) {

View File

@@ -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;
}

View File

@@ -1,8 +1,10 @@
<?php
namespace Grav\Common;
use Grav\Common\Config\Config;
use Grav\Common\Language\Language;
use Grav\Common\Page\Medium\ImageMedium;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Service\ConfigServiceProvider;
use Grav\Common\Service\ErrorServiceProvider;
@@ -34,7 +36,13 @@ class Grav extends Container
*/
protected static $instance;
public static function instance(array $values = array())
/**
* Return the Grav instance. Create it if it's not already instanced
*
* @param array $values
* @return Grav
*/
public static function instance(array $values = [])
{
if (!self::$instance) {
self::$instance = static::load($values);
@@ -51,6 +59,12 @@ class Grav extends Container
return self::$instance;
}
/**
* Initialize and return a Grav instance
*
* @param array $values
* @return static
*/
protected static function load(array $values)
{
$container = new static($values);
@@ -98,7 +112,7 @@ class Grav extends Container
};
$container['pages'] = function ($c) {
return new Page\Pages($c);
return new Pages($c);
};
$container['assets'] = new Assets();
@@ -178,6 +192,9 @@ class Grav extends Container
return $container;
}
/**
* Process a request
*/
public function process()
{
/** @var Debugger $debugger */
@@ -304,10 +321,13 @@ class Grav extends Container
if ($uri->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);

View File

@@ -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)
{

View File

@@ -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, '<link href="/test.css" type="text/css" rel="stylesheet" />' . 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, '<script src="/test.js" type="text/javascript" ></script>' . 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, '<link href="/test.css" type="text/css" rel="stylesheet" />' . 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, '<script src="/test.js" type="text/javascript" ></script>' . 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, '<link href="/test.css" type="text/css" rel="stylesheet" />' . 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, '<script src="/test.js" type="text/javascript" ></script>' . 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, '<script src="/test.js" type="text/javascript" async></script>' . 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, '<script src="/test.js" type="text/javascript" defer></script>' . 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. '<script>' .PHP_EOL . 'alert("test")' . PHP_EOL.PHP_EOL .'</script>' . PHP_EOL);
$this->assertSame($js,
PHP_EOL . '<script>' . PHP_EOL . 'alert("test")' . PHP_EOL . PHP_EOL . '</script>' . PHP_EOL);
}
}
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);
}
}