This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
[v3] Refactor: Cut Redundant Dependency in order to use as Standalone #9
Open
sasezaki
wants to merge
15
commits into
zendframework:develop
Choose a base branch
from
sasezaki:redundant_dependency
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0d91c05
Move toJson() method To JsonSerializeTrait
sasezaki 9615fcd
Move render() & __toString() requires To RenderTrait
sasezaki f91f696
Move caching To CachedTrait
sasezaki fb6a8a7
Introduce PaginatorInterface
sasezaki 4d7e3df
oops, missing CachedTrait
sasezaki aba0bcc
Move Global (static access) to GlobalPaginator
sasezaki b9aad18
fix namespace requirement
sasezaki 3143850
remove duplicate method
sasezaki dcacdab
fix namespace import
sasezaki a20555d
default itemCountPerPage must be null, when global count exists
sasezaki 07f259a
Add doc-comment in JsonSerializeTrait::jsonSerialize()
sasezaki 26f3bd8
move stdlib dependency as dev
sasezaki f1d4967
TypeHint must be PaginatorInterface
sasezaki 5ef2ea7
Apply php-cs-fixer
sasezaki a98d725
remove jsonSerialize()
sasezaki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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()), | ||
$this->getItemCountPerPage() | ||
])); | ||
} | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.