Skip to content

Commit

Permalink
Merge pull request #43 from tedivm/refactor
Browse files Browse the repository at this point in the history
Testing and Refactoring
  • Loading branch information
tedivm committed May 7, 2014
2 parents a42ee0a + 10e3085 commit 1367d69
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src_dir: ./
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/.idea
/.settings
/.buildpath
/.project
/composer.lock
/vendor
/report
/build
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ php:
- hhvm

before_script:
- composer install --dev
- composer self-update && composer install --dev

script: phpunit --verbose --coverage-text
script: ./Tests/runTests.sh

after_script:
- php vendor/bin/coveralls -v

matrix:
allow_failures:
- php: hhvm
allow_failures:
- php: hhvm
11 changes: 5 additions & 6 deletions Adapters/DoctrineAdapter.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

namespace Tedivm\StashBundle\Adapters;
use Stash\Item as StashItem;
use Stash\Drivers;
use Doctrine\Common\Cache\Cache as DoctrineCacheInterface;

class DoctrineAdapter implements DoctrineCacheInterface
Expand Down Expand Up @@ -60,15 +58,15 @@ public function fetch($id)
{
$id = $this->normalizeId($id);

if(isset($this->caches[$id])) {
if (isset($this->caches[$id])) {
$cache = $this->caches[$id];
unset($this->caches[$id]);
} else {
$cache = $this->cacheService->getItem($id);
}

$value = $cache->get();
if($cache->isMiss()) {
if ($cache->isMiss()) {
return false;
} else {
return $value;
Expand All @@ -84,7 +82,7 @@ public function contains($id)

$this->caches[$id] = $this->cacheService->getItem($id);

return !$this->caches[$id]->isMiss();
return !$this->caches[$id]->isMiss();
}

/**
Expand Down Expand Up @@ -143,9 +141,10 @@ public function flushAll()
*/
protected function normalizeId($id)
{
if(isset($this->namespace)) {
if (isset($this->namespace)) {
$id = sprintf('zz_%s_zz/%s', $this->namespace, $id);
$id = trim($id, '/');

return $id;
} else {
return $id;
Expand Down
4 changes: 2 additions & 2 deletions Adapters/SessionHandlerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Stash\Session;

if(version_compare(phpversion(), '5.4.0', '>=')){
if (version_compare(phpversion(), '5.4.0', '>=')) {
class SessionHandlerAdapterShim extends Session {}
} else {
class SessionHandlerAdapterShim extends Session implements \SessionHandlerInterface {}
Expand All @@ -27,4 +27,4 @@ public function clearAll()
$item = $this->pool->getItem('ss_ss');
$item->clear();
}
}
}
8 changes: 3 additions & 5 deletions Collector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Stash\Item;
use Stash\Drivers;


/**
* Collects data stored in the static variables of the Stash class for use in profiling/debugging. Currently
* records total cache calls and returns, along with calls and returns on each individual cache node.
Expand Down Expand Up @@ -67,7 +65,7 @@ public function addLogger($logger)
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$info = array('calls' => 0, 'hits' => 0);
foreach($this->loggers as $logger) {
foreach ($this->loggers as $logger) {
$name = $logger->getName();
$calls = $logger->getCalls();
$hits = $logger->getHits();
Expand All @@ -82,10 +80,10 @@ public function collect(Request $request, Response $response, \Exception $except
}

$handlers = Drivers::getDrivers();
foreach($handlers as $handler) {
foreach ($handlers as $handler) {
$pieces = explode('\\', $handler);
$name = array_pop($pieces);
if(!in_array($name, array('Ephemeral', 'Composite'))) {
if (!in_array($name, array('Ephemeral', 'Composite'))) {
$info['availableDrivers'][] = $name;
}
}
Expand Down
12 changes: 5 additions & 7 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tedivm\StashBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Stash\Drivers;

Expand Down Expand Up @@ -105,8 +104,8 @@ protected function getCachesNode()
->booleanNode('inMemory')->defaultTrue()->end()
;

foreach($handlers as $handler) {
if($handler !== 'Composite') {
foreach ($handlers as $handler) {
if ($handler !== 'Composite') {
$this->addHandlerSettings($handler, $childNode);
}
}
Expand All @@ -117,14 +116,13 @@ protected function getCachesNode()
return $node;
}


public function addHandlerSettings($handler, $rootNode)
{
$handlerNode = $rootNode
->arrayNode($handler)
->fixXmlConfig('server');

if($handler == 'Memcache') {
if ($handler == 'Memcache') {
$finalNode = $handlerNode
->info('All options except "servers" are Memcached options. See http://www.php.net/manual/en/memcached.constants.php')
->addDefaultsIfNotSet()
Expand Down Expand Up @@ -163,7 +161,7 @@ public function addHandlerSettings($handler, $rootNode)
->end()
->end()
;
} elseif($handler == 'Redis') {
} elseif ($handler == 'Redis') {
$finalNode = $handlerNode
->info("Accepts server info, password, and database.")
->addDefaultsIfNotSet()
Expand Down Expand Up @@ -193,7 +191,7 @@ public function addHandlerSettings($handler, $rootNode)
->addDefaultsIfNotSet()
->children();

foreach($defaults as $setting => $default) {
foreach ($defaults as $setting => $default) {
$node
->scalarNode($setting)
->defaultValue($default)
Expand Down
10 changes: 5 additions & 5 deletions DependencyInjection/TedivmStashExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function load(array $configs, ContainerBuilder $container)

$caches = array();
$options = array();
foreach($config['caches'] as $name => $cache) {
foreach ($config['caches'] as $name => $cache) {
$caches[$name] = sprintf('stash.%s_cache', $name);
$options[$name] = $cache;
$this->addCacheService($name, $cache, $container);
Expand All @@ -55,7 +55,7 @@ protected function addCacheService($name, $cache, $container)
$handlers = $cache['handlers'];
unset($cache['handlers']);

if(isset($cache['inMemory']) && $cache['inMemory']) {
if (isset($cache['inMemory']) && $cache['inMemory']) {
array_unshift($handlers, 'Ephemeral');
}
unset($cache['inMemory']);
Expand Down Expand Up @@ -94,7 +94,7 @@ protected function addCacheService($name, $cache, $container)
->setAbstract(false)
;

if(interface_exists("\\Doctrine\\Common\\Cache\\Cache") && $doctrine) {
if (interface_exists("\\Doctrine\\Common\\Cache\\Cache") && $doctrine) {
$container
->setDefinition(sprintf('stash.adapter.doctrine.%s_cache', $name), new DefinitionDecorator('stash.adapter.doctrine'))
->setArguments(array(
Expand All @@ -104,7 +104,7 @@ protected function addCacheService($name, $cache, $container)
;
}

if($session) {
if ($session) {
$container
->setDefinition(sprintf('stash.adapter.session.%s_cache', $name), new DefinitionDecorator('stash.adapter.session'))
->setArguments(array(
Expand All @@ -127,4 +127,4 @@ public function getAlias()
{
return 'stash';
}
}
}
10 changes: 5 additions & 5 deletions Factory/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
use Stash\Drivers,
Stash\Interfaces\DriverInterface;

class HandlerFactory {

class HandlerFactory
{
/**
* Given a list of cache types and options, creates a CompositeDrivers wrapping the specified drivers.
*
* @param $types
* @param $options
* @return DriverInterface
*/
static function createHandler($types, $options)
public static function createHandler($types, $options)
{
$handlers = Drivers::getDrivers();

$h = array();

foreach($types as $type) {
foreach ($types as $type) {
$class = $handlers[$type];
if ($type === 'Memcache' && isset($options[$type])) {
// Fix servers spec since underlying drivers expect plain arrays, not hashes.
Expand All @@ -39,7 +39,7 @@ static function createHandler($types, $options)
$h[] = new $class($opts);
}

if(count($h) == 1) {
if (count($h) == 1) {
return reset($h);
}

Expand Down
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2014, Robert Hafner, Josh Hall-Bachner
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Stash Project nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Robert Hafner BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
<tag name="data_collector" id="stash" template="TedivmStashBundle:Profiler:layout"/>
</service>
</services>
</container>
</container>
24 changes: 24 additions & 0 deletions Resources/meta/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2014, Robert Hafner, Josh Hall-Bachner
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Stash Project nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Robert Hafner BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion Resources/views/Profiler/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@


{% endfor %}
{% endblock %}
{% endblock %}
6 changes: 3 additions & 3 deletions Service/CacheLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ public function enableQueryLogging($lq = true)
public function logRequest($key, $hit, $value)
{
$this->calls++;
if($hit) {
if ($hit) {
$this->hits++;
}

if(!$this->logQueries) {
if (!$this->logQueries) {
return;
}

$leader = sprintf('@@_%s_@@/', $this->name);
if(strpos($key, $leader) === 0) {
if (strpos($key, $leader) === 0) {
$key = substr($key, strlen($leader));
}

Expand Down
9 changes: 4 additions & 5 deletions Service/CacheResultObject.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace Tedivm\StashBundle\Service;
use Stash\Drivers;

/**
* Simple result-object provider for the Stash class.
Expand Down Expand Up @@ -44,9 +43,9 @@ public function __construct($cache, $logger = null)
*/
public function __call($name, $args)
{
if($name === 'get') {
if ($name === 'get') {
return $this->getAndLog($args);
} elseif($name === 'getKey') {
} elseif ($name === 'getKey') {
return $this->getShortenedKey();
} else {
return call_user_func_array(array($this->cache, $name), $args);
Expand All @@ -62,7 +61,7 @@ protected function getShortenedKey()
{
$key = $this->cache->getKey();
$parts = explode('/', $key);
if( (strpos($parts[0], '@@_') === 0) && (strpos(strrev($parts[0]), '@@_') === 0) ) {
if ( (strpos($parts[0], '@@_') === 0) && (strpos(strrev($parts[0]), '@@_') === 0) ) {
array_shift($parts);
}

Expand All @@ -81,7 +80,7 @@ protected function getAndLog($args)
$miss = $this->cache->isMiss();
$key = $this->cache->getKey();

if(isset($this->logger)) {
if (isset($this->logger)) {
$this->logger->logRequest($key, !($miss), $result);
}

Expand Down
Loading

0 comments on commit 1367d69

Please sign in to comment.