mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-07-28 04:00:43 +02:00
added AssetProvider interface and JavascriptRenderer::addAssets()
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
2014-03-22:
|
||||
|
||||
- added AssetProvider interface
|
||||
- added JavascriptRenderer::addAssets()
|
||||
|
||||
2014-01-04:
|
||||
|
||||
- added DataFormatter
|
||||
|
28
src/DebugBar/DataCollector/AssetProvider.php
Normal file
28
src/DebugBar/DataCollector/AssetProvider.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the DebugBar package.
|
||||
*
|
||||
* (c) 2013 Maxime Bouroumeau-Fuseau
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace DebugBar\DataCollector;
|
||||
|
||||
/**
|
||||
* Indicates that a DataCollector provides some assets
|
||||
*/
|
||||
interface AssetProvider
|
||||
{
|
||||
/**
|
||||
* Returns an array with the following keys:
|
||||
* - base_path
|
||||
* - base_url
|
||||
* - css: an array of filenames
|
||||
* - js: an array of filenames
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getAssets();
|
||||
}
|
@@ -11,6 +11,7 @@
|
||||
namespace DebugBar;
|
||||
|
||||
use DebugBar\DataCollector\Renderable;
|
||||
use DebugBar\DataCollector\AssetProvider;
|
||||
|
||||
/**
|
||||
* Renders the debug bar using the client side javascript implementation
|
||||
@@ -25,6 +26,10 @@ class JavascriptRenderer
|
||||
|
||||
const REPLACEABLE_TAG = "{--DEBUGBAR_OB_START_REPLACE_ME--}";
|
||||
|
||||
const RELATIVE_PATH = 'path';
|
||||
|
||||
const RELATIVE_URL = 'url';
|
||||
|
||||
protected $debugBar;
|
||||
|
||||
protected $baseUrl;
|
||||
@@ -41,6 +46,8 @@ class JavascriptRenderer
|
||||
|
||||
protected $jsFiles = array('debugbar.js', 'widgets.js', 'openhandler.js');
|
||||
|
||||
protected $additionalAssets = array();
|
||||
|
||||
protected $javascriptClass = 'PhpDebugBar.DebugBar';
|
||||
|
||||
protected $variableName = 'phpdebugbar';
|
||||
@@ -93,9 +100,15 @@ class JavascriptRenderer
|
||||
* - include_vendors
|
||||
* - javascript_class
|
||||
* - variable_name
|
||||
* - initialization
|
||||
* - enable_jquery_noconflict
|
||||
* - controls
|
||||
* - disable_controls
|
||||
* - ignore_collectors
|
||||
* - ajax_handler_classname
|
||||
* - ajax_handler_bind_to_jquery
|
||||
* - open_handler_classname
|
||||
* - open_handler_url
|
||||
*
|
||||
* @param array $options [description]
|
||||
*/
|
||||
@@ -383,21 +396,6 @@ class JavascriptRenderer
|
||||
return $this->ignoredCollectors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns needed asset files relative to the base path
|
||||
*
|
||||
* @param string $type 'css', 'js' or null for both
|
||||
* @return array
|
||||
*/
|
||||
public function getAssets($type = null)
|
||||
{
|
||||
list($cssFiles, $jsFiles) = $this->getAssetFiles();
|
||||
return $this->filterAssetArray(array(
|
||||
$this->makeUriRelativeTo($cssFiles, $this->basePath),
|
||||
$this->makeUriRelativeTo($jsFiles, $this->basePath)
|
||||
), $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the class name of the ajax handler
|
||||
*
|
||||
@@ -484,13 +482,33 @@ class JavascriptRenderer
|
||||
return $this->openHandlerUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add assets to render in the head
|
||||
*
|
||||
* @param array $cssFiles An array of filenames
|
||||
* @param array $jsFiles An array of filenames
|
||||
* @param string $basePath Base path of those files
|
||||
* @param string $baseUrl Base url of those files
|
||||
*/
|
||||
public function addAssets($cssFiles, $jsFiles, $basePath = null, $baseUrl = null)
|
||||
{
|
||||
$this->additionalAssets[] = array(
|
||||
'base_path' => $basePath,
|
||||
'base_url' => $baseUrl,
|
||||
'css' => (array) $cssFiles,
|
||||
'js' => (array) $jsFiles
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of asset files
|
||||
*
|
||||
* @param string $type Only return css or js files
|
||||
* @param string $relativeTo The type of path to which filenames must be relative (path, url or null)
|
||||
* @return array
|
||||
*/
|
||||
protected function getAssetFiles($type = null)
|
||||
public function getAssets($type = null, $relativeTo = self::RELATIVE_PATH)
|
||||
{
|
||||
$cssFiles = $this->cssFiles;
|
||||
$jsFiles = $this->jsFiles;
|
||||
@@ -504,9 +522,79 @@ class JavascriptRenderer
|
||||
}
|
||||
}
|
||||
|
||||
if ($relativeTo) {
|
||||
$root = $this->getRelativeRoot($relativeTo, $this->basePath, $this->baseUrl);
|
||||
$cssFiles = $this->makeUriRelativeTo($cssFiles, $root);
|
||||
$jsFiles = $this->makeUriRelativeTo($jsFiles, $root);
|
||||
}
|
||||
|
||||
$additionalAssets = $this->additionalAssets;
|
||||
// finds assets provided by collectors
|
||||
foreach ($this->debugBar->getCollectors() as $collector) {
|
||||
if (($collector instanceof AssetProvider) && !in_array($collector->getName(), $this->ignoredCollectors)) {
|
||||
$additionalAssets[] = $collector->getAssets();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($additionalAssets as $assets) {
|
||||
$basePath = isset($assets['base_path']) ? $assets['base_path'] : null;
|
||||
$baseUrl = isset($assets['base_url']) ? $assets['base_url'] : null;
|
||||
$root = $this->getRelativeRoot($relativeTo,
|
||||
$this->makeUriRelativeTo($basePath, $this->basePath),
|
||||
$this->makeUriRelativeTo($baseUrl, $this->baseUrl));
|
||||
$cssFiles = array_merge($cssFiles, $this->makeUriRelativeTo((array) $assets['css'], $root));
|
||||
$jsFiles = array_merge($jsFiles, $this->makeUriRelativeTo((array) $assets['js'], $root));
|
||||
}
|
||||
|
||||
return $this->filterAssetArray(array($cssFiles, $jsFiles), $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the correct base according to the type
|
||||
*
|
||||
* @param string $relativeTo
|
||||
* @param string $basePath
|
||||
* @param string $baseUrl
|
||||
* @return string
|
||||
*/
|
||||
protected function getRelativeRoot($relativeTo, $basePath, $baseUrl)
|
||||
{
|
||||
if ($relativeTo === self::RELATIVE_PATH) {
|
||||
return $basePath;
|
||||
}
|
||||
if ($relativeTo === self::RELATIVE_URL) {
|
||||
return $baseUrl;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a URI relative to another
|
||||
*
|
||||
* @param string|array $uri
|
||||
* @param string $root
|
||||
* @return string
|
||||
*/
|
||||
protected function makeUriRelativeTo($uri, $root)
|
||||
{
|
||||
if (!$root) {
|
||||
return $uri;
|
||||
}
|
||||
|
||||
if (is_array($uri)) {
|
||||
$uris = array();
|
||||
foreach ($uri as $u) {
|
||||
$uris[] = $this->makeUriRelativeTo($u, $root);
|
||||
}
|
||||
return $uris;
|
||||
}
|
||||
|
||||
if (substr($uri, 0, 1) === '/' || preg_match('/^([a-z]+:\/\/|[a-zA-Z]:\/)/', $uri)) {
|
||||
return $uri;
|
||||
}
|
||||
return rtrim($root, '/') . "/$uri";
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters a tuple of (css, js) assets according to $type
|
||||
*
|
||||
@@ -535,7 +623,7 @@ class JavascriptRenderer
|
||||
*/
|
||||
public function getAsseticCollection($type = null)
|
||||
{
|
||||
list($cssFiles, $jsFiles) = $this->getAssetFiles();
|
||||
list($cssFiles, $jsFiles) = $this->getAssets();
|
||||
return $this->filterAssetArray(array(
|
||||
$this->createAsseticCollection($cssFiles),
|
||||
$this->createAsseticCollection($jsFiles)
|
||||
@@ -554,7 +642,7 @@ class JavascriptRenderer
|
||||
{
|
||||
$assets = array();
|
||||
foreach ($files as $file) {
|
||||
$assets[] = new \Assetic\Asset\FileAsset($this->makeUriRelativeTo($file, $this->basePath));
|
||||
$assets[] = new \Assetic\Asset\FileAsset($file);
|
||||
}
|
||||
return new \Assetic\Asset\AssetCollection($assets);
|
||||
}
|
||||
@@ -607,45 +695,20 @@ class JavascriptRenderer
|
||||
*/
|
||||
public function renderHead()
|
||||
{
|
||||
list($cssFiles, $jsFiles) = $this->getAssetFiles();
|
||||
list($cssFiles, $jsFiles) = $this->getAssets(null, self::RELATIVE_URL);
|
||||
$html = '';
|
||||
|
||||
foreach ($cssFiles as $file) {
|
||||
$html .= sprintf('<link rel="stylesheet" type="text/css" href="%s">' . "\n",
|
||||
$this->makeUriRelativeTo($file, $this->baseUrl));
|
||||
$html .= sprintf('<link rel="stylesheet" type="text/css" href="%s">' . "\n", $file);
|
||||
}
|
||||
|
||||
foreach ($jsFiles as $file) {
|
||||
$html .= sprintf('<script type="text/javascript" src="%s"></script>' . "\n",
|
||||
$this->makeUriRelativeTo($file, $this->baseUrl));
|
||||
$html .= sprintf('<script type="text/javascript" src="%s"></script>' . "\n", $file);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a URI relative to another
|
||||
*
|
||||
* @param string|array $uri
|
||||
* @param string $root
|
||||
* @return string
|
||||
*/
|
||||
protected function makeUriRelativeTo($uri, $root)
|
||||
{
|
||||
if (is_array($uri)) {
|
||||
$uris = array();
|
||||
foreach ($uri as $u) {
|
||||
$uris[] = $this->makeUriRelativeTo($u, $root);
|
||||
}
|
||||
return $uris;
|
||||
}
|
||||
|
||||
if (substr($uri, 0, 1) === '/' || preg_match('/^([a-z]+:\/\/|[a-zA-Z]:\/)/', $uri)) {
|
||||
return $uri;
|
||||
}
|
||||
return rtrim($root, '/') . "/$uri";
|
||||
}
|
||||
|
||||
/**
|
||||
* Register shutdown to display the debug bar
|
||||
*
|
||||
|
@@ -12,6 +12,8 @@ class JavascriptRendererTest extends DebugBarTestCase
|
||||
{
|
||||
parent::setUp();
|
||||
$this->r = new JavascriptRenderer($this->debugbar);
|
||||
$this->r->setBasePath('/bpath');
|
||||
$this->r->setBaseUrl('/burl');
|
||||
}
|
||||
|
||||
public function testOptions()
|
||||
@@ -58,25 +60,35 @@ class JavascriptRendererTest extends DebugBarTestCase
|
||||
$this->assertEquals('open.php', $this->r->getOpenHandlerUrl());
|
||||
}
|
||||
|
||||
public function testAddAssets()
|
||||
{
|
||||
$this->r->addAssets('foo.css', 'foo.js', '/bar', '/foobar');
|
||||
|
||||
list($css, $js) = $this->r->getAssets();
|
||||
$this->assertContains('/bar/foo.css', $css);
|
||||
$this->assertContains('/bar/foo.js', $js);
|
||||
|
||||
$html = $this->r->renderHead();
|
||||
$this->assertTag(array('tag' => 'script', 'attributes' => array('src' => '/foobar/foo.js')), $html);
|
||||
}
|
||||
|
||||
public function testGetAssets()
|
||||
{
|
||||
$this->r->setBasePath('/foo');
|
||||
list($css, $js) = $this->r->getAssets();
|
||||
$this->assertContains('/foo/debugbar.css', $css);
|
||||
$this->assertContains('/foo/widgets.js', $js);
|
||||
$this->assertContains('/foo/vendor/jquery/jquery.min.js', $js);
|
||||
$this->assertContains('/bpath/debugbar.css', $css);
|
||||
$this->assertContains('/bpath/widgets.js', $js);
|
||||
$this->assertContains('/bpath/vendor/jquery/jquery.min.js', $js);
|
||||
|
||||
$this->r->setIncludeVendors(false);
|
||||
$js = $this->r->getAssets('js');
|
||||
$this->assertContains('/foo/debugbar.js', $js);
|
||||
$this->assertNotContains('/foo/vendor/jquery/jquery.min.js', $js);
|
||||
$this->assertContains('/bpath/debugbar.js', $js);
|
||||
$this->assertNotContains('/bpath/vendor/jquery/jquery.min.js', $js);
|
||||
}
|
||||
|
||||
public function testRenderHead()
|
||||
{
|
||||
$this->r->setBaseUrl('/foo');
|
||||
$html = $this->r->renderHead();
|
||||
$this->assertTag(array('tag' => 'script', 'attributes' => array('src' => '/foo/debugbar.js')), $html);
|
||||
$this->assertTag(array('tag' => 'script', 'attributes' => array('src' => '/burl/debugbar.js')), $html);
|
||||
}
|
||||
|
||||
public function testRenderFullInitialization()
|
||||
|
Reference in New Issue
Block a user