Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW TemplateCache tab (showing partial caching hits/misses) added (#78)
- Loading branch information
1 parent
856f7c4
commit 9f8837a
Showing
10 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace LeKoala\DebugBar\Aspect; | ||
|
||
use LeKoala\DebugBar\Collector\PartialCacheCollector; | ||
use SilverStripe\Core\Convert; | ||
use SilverStripe\Core\Injector\AfterCallAspect; | ||
|
||
class CacheAfterCallAspect implements AfterCallAspect | ||
{ | ||
/** | ||
* Logs all hits/misses after a CacheInterface::get call is made. | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function afterCall($proxied, $method, $args, $result) | ||
{ | ||
$message = (empty($result)) ? "Missed: {$args[0]}" : "Hit: {$args[0]}"; | ||
$result = preg_replace('/\s+/', ' ', trim($result)); | ||
$result = Convert::raw2att($result); | ||
PartialCacheCollector::addTemplateCache( | ||
$message, | ||
array( | ||
'cache_result' => | ||
array('result' => $result) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace LeKoala\DebugBar\Collector; | ||
|
||
use DebugBar\DataCollector\DataCollector; | ||
use DebugBar\DataCollector\Renderable; | ||
|
||
/** | ||
* Collects data about the partial cache hits and misses during a SilverStripe request | ||
*/ | ||
class PartialCacheCollector extends DataCollector implements Renderable | ||
{ | ||
/** | ||
* Contains a list of all partial caches found. | ||
* @var array | ||
*/ | ||
protected static $templateCache = []; | ||
|
||
public function getName() | ||
{ | ||
return 'partial-cache'; | ||
} | ||
|
||
public function collect() | ||
{ | ||
$result = self::getTemplateCache(); | ||
return [ | ||
'count' => count($result), | ||
'calls' => $result | ||
]; | ||
} | ||
|
||
public function getWidgets() | ||
{ | ||
$widgets = [ | ||
'Partial Cache' => [ | ||
'icon' => 'asterisk', | ||
'widget' => 'PhpDebugBar.Widgets.ConfigWidget', | ||
'map' => 'partial-cache.calls', | ||
'default' => '{}' | ||
] | ||
]; | ||
if (count(self::getTemplateCache()) > 0) { | ||
$widgets['Partial Cache:badge'] = [ | ||
'map' => 'partial-cache.count', | ||
'default' => 0 | ||
]; | ||
} | ||
|
||
return $widgets; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getTemplateCache() | ||
{ | ||
return (self::$templateCache) ?: []; | ||
} | ||
|
||
/** | ||
* Adds an item to the templateCache array | ||
* @param string $key | ||
* @param array $item | ||
*/ | ||
public static function addTemplateCache($key, $item) | ||
{ | ||
self::$templateCache[$key] = $item; | ||
} | ||
|
||
/** | ||
* @param array $templateCache | ||
*/ | ||
public static function setTemplateCache($templateCache) | ||
{ | ||
self::$templateCache = $templateCache; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace LeKoala\DebugBar\Test\Aspect; | ||
|
||
use LeKoala\DebugBar\Aspect\CacheAfterCallAspect; | ||
use LeKoala\DebugBar\Collector\PartialCacheCollector; | ||
use Psr\SimpleCache\CacheInterface; | ||
use SilverStripe\Core\Injector\AopProxyService; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\Dev\SapphireTest; | ||
|
||
class CacheAfterCallAspectTest extends SapphireTest | ||
{ | ||
/** | ||
* Tests if an entry was added to PartialCacheCollector::$template_cache_info array | ||
*/ | ||
public function testAfterCall() | ||
{ | ||
$proxy = new AopProxyService(); | ||
$aspect = new CacheAfterCallAspect(); | ||
$proxy->afterCall = array( | ||
'get' => $aspect | ||
); | ||
$count = count(PartialCacheCollector::getTemplateCache()); | ||
$proxy->proxied = Injector::inst()->get(CacheInterface::class . '.backend'); | ||
$cacheKey = 'myCacheKey'; | ||
$proxy->get($cacheKey); | ||
$this->assertCount($count + 1, PartialCacheCollector::getTemplateCache()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace LeKoala\DebugBar\Test\Collector; | ||
|
||
use LeKoala\DebugBar\Collector\PartialCacheCollector; | ||
use SilverStripe\Dev\SapphireTest; | ||
|
||
class PartialCacheCollectorTest extends SapphireTest | ||
{ | ||
/** | ||
* @var PartialCacheCollector | ||
*/ | ||
private $collector; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->collector = new PartialCacheCollector(); | ||
} | ||
|
||
public function testGetName() | ||
{ | ||
$this->assertNotEmpty($this->collector->getName()); | ||
} | ||
|
||
/** | ||
* Tests that the tab is returned and that the badge is optional | ||
*/ | ||
public function testGetWidgets() | ||
{ | ||
PartialCacheCollector::setTemplateCache(array()); | ||
$widgets = $this->collector->getWidgets(); | ||
$this->assertArrayHasKey('Partial Cache', $widgets); | ||
$this->assertArrayNotHasKey('Partial Cache:badge', $widgets); | ||
|
||
PartialCacheCollector::addTemplateCache('test', array('test')); | ||
$this->assertArrayHasKey('Partial Cache:badge', $this->collector->getWidgets()); | ||
} | ||
|
||
/** | ||
* Tests that an array is returned with specific indexes set | ||
*/ | ||
public function testCollect() | ||
{ | ||
$result = $this->collector->collect(); | ||
$this->assertArrayHasKey('count', $result); | ||
$this->assertArrayHasKey('calls', $result); | ||
} | ||
|
||
/** | ||
* Tests that adding an item to the cache increases its count | ||
*/ | ||
public function testAddTemplateCache() | ||
{ | ||
$count = count(PartialCacheCollector::getTemplateCache()); | ||
PartialCacheCollector::addTemplateCache('test1234', array('test')); | ||
$this->assertCount($count + 1, PartialCacheCollector::getTemplateCache()); | ||
} | ||
|
||
/** | ||
* Tests that the setter works | ||
*/ | ||
public function testSetTemplateCache() | ||
{ | ||
PartialCacheCollector::setTemplateCache(array()); | ||
$this->assertCount(0, PartialCacheCollector::getTemplateCache()); | ||
} | ||
} |