Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

[v3] Refactor: Cut Redundant Dependency in order to use as Standalone #9

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
}
},
"require": {
"php": ">=5.5",
"zendframework/zend-stdlib": "dev-develop as 2.8.0"
"php": ">=5.5"
},
"require-dev": {
"zendframework/zend-cache": "dev-develop as 2.6.0",
Expand All @@ -24,6 +23,7 @@
"zendframework/zend-json": "~2.5",
"zendframework/zend-mvc": "dev-develop as 2.7.0",
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
"zendframework/zend-stdlib": "dev-develop as 2.8.0",
"zendframework/zend-view": "dev-develop as 2.6.0",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
Expand All @@ -34,6 +34,7 @@
"zendframework/zend-filter": "Zend\\Filter component",
"zendframework/zend-json": "Zend\\Json component",
"zendframework/zend-servicemanager": "Zend\\ServiceManager component",
"zendframework/zend-stdlib": "Zend\\Stdlib component",
"zendframework/zend-view": "Zend\\View component"
},
"minimum-stability": "dev",
Expand Down
147 changes: 147 additions & 0 deletions src/CachedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Paginator;

use Zend\Cache\Storage\IteratorInterface as CacheIterator;
use Zend\Cache\Storage\StorageInterface as CacheStorage;

trait CachedTrait
{

/**
* Enable or disable the cache by Zend\Paginator\Paginator instance
*
* @var bool
*/
protected $cacheEnabled = true;

/**
* Cache object
*
* @var CacheStorage
*/
protected static $cache;

/**
* Sets a cache object
*
* @param CacheStorage $cache
*/
public static function setCache(CacheStorage $cache)
{
static::$cache = $cache;
}

/**
* Enables/Disables the cache for this instance
*
* @param bool $enable
* @return Paginator
*/
public function setCacheEnabled($enable)
{
$this->cacheEnabled = (bool) $enable;
return $this;
}

/**
* Returns the page item cache.
*
* @return array
*/
public function getPageItemCache()
{
$data = [];
if ($this->cacheEnabled()) {
$prefixLength = strlen(self::CACHE_TAG_PREFIX);
$cacheIterator = static::$cache->getIterator();
$cacheIterator->setMode(CacheIterator::CURRENT_AS_VALUE);
foreach ($cacheIterator as $key => $value) {
if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) {
$data[(int) substr($key, $prefixLength)] = $value;
}
}
}
return $data;
}

/**
* Clear the page item cache.
*
* @param int $pageNumber
* @return Paginator
*/
public function clearPageItemCache($pageNumber = null)
{
if (!$this->cacheEnabled()) {
return $this;
}

if (null === $pageNumber) {
$prefixLength = strlen(self::CACHE_TAG_PREFIX);
$cacheIterator = static::$cache->getIterator();
$cacheIterator->setMode(CacheIterator::CURRENT_AS_KEY);
foreach ($cacheIterator as $key) {
if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) {
static::$cache->removeItem($this->_getCacheId((int)substr($key, $prefixLength)));
}
}
} else {
$cleanId = $this->_getCacheId($pageNumber);
static::$cache->removeItem($cleanId);
}
return $this;
}

/**
* Tells if there is an active cache object
* and if the cache has not been disabled
*
* @return bool
*/
protected function cacheEnabled()
{
return ((static::$cache !== null) && $this->cacheEnabled);
}

/**
* Makes an Id for the cache
* Depends on the adapter object and the page number
*
* Used to store item in cache from that Paginator instance
* and that current page
*
* @param int $page
* @return string
*/
protected function _getCacheId($page = null)
{
if ($page === null) {
$page = $this->getCurrentPageNumber();
}
return self::CACHE_TAG_PREFIX . $page . '_' . $this->_getCacheInternalId();
}

/**
* Get the internal cache id
* Depends on the adapter and the item count per page
*
* Used to tag that unique Paginator instance in cache
*
* @return string
*/
protected function _getCacheInternalId()
{
return md5(serialize([
spl_object_hash($this->getAdapter()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cache internal id fixed by #33 , this may need copy function from current master branch.

$this->getItemCountPerPage()
]));
}
}
46 changes: 46 additions & 0 deletions src/FilterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Paginator;

use Zend\Filter\FilterInterface;

trait FilterTrait
{

/**
* Result filter
*
* @var FilterInterface
*/
protected $filter = null;

/**
* Get the filter
*
* @return FilterInterface
*/
public function getFilter()
{
return $this->filter;
}

/**
* Set a filter chain
*
* @param FilterInterface $filter
* @return Paginator
*/
public function setFilter(FilterInterface $filter)
{
$this->filter = $filter;

return $this;
}
}
Loading