-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delay load pages #257
Closed
Closed
Delay load pages #257
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe974ca
Delay searching for pages until the pages variable is actually used.
kicken 3165cf1
Remove comment
kicken 45ee6cb
Move page collection class from the twig plugin to the core so the delay
kicken c3a6149
Keep getPage method private. Use findByPath instead.
kicken 1290893
Use a callback function for loading pages.
kicken 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,140 @@ | ||
<?php | ||
|
||
namespace Phile\Repository; | ||
|
||
use Phile\Core\Utility; | ||
|
||
/** | ||
* Page collection class for locating pages on demand. | ||
* | ||
* @author PhileCMS | ||
* @link https://philecms.com | ||
* @license http://opensource.org/licenses/MIT | ||
* @package Phile\Repository | ||
*/ | ||
class PageCollection implements \ArrayAccess, \IteratorAggregate, \Countable { | ||
/** | ||
* @var \Phile\Model\Page[] array of pages found | ||
*/ | ||
private $pages; | ||
|
||
/** | ||
* @var array array of search options | ||
*/ | ||
private $options; | ||
|
||
/** | ||
* @var string base search folder | ||
*/ | ||
private $folder; | ||
|
||
/** | ||
* @var \Phile\Repository\Page page repository | ||
*/ | ||
private $repository; | ||
|
||
public function __construct(array $options, $folder, \Phile\Repository\Page $repository){ | ||
$this->options = $options; | ||
$this->folder = $folder; | ||
$this->repository = $repository; | ||
} | ||
|
||
public function getIterator(){ | ||
$this->load(); | ||
return new \ArrayIterator($this->pages); | ||
} | ||
|
||
public function offsetExists ($offset){ | ||
$this->load(); | ||
return isset($this->pages[$offset]); | ||
} | ||
|
||
public function offsetGet($offset){ | ||
$this->load(); | ||
return $this->pages[$offset]; | ||
} | ||
|
||
public function offsetSet($offset, $value){ | ||
$this->load(); | ||
$this->pages[$offset] = $value; | ||
} | ||
|
||
public function offsetUnset($offset){ | ||
$this->load(); | ||
unset($this->pages[$offset]); | ||
} | ||
|
||
public function count(){ | ||
$this->load(); | ||
return count($this->pages); | ||
} | ||
|
||
private function load(){ | ||
if ($this->pages === null){ | ||
$this->findPages(); | ||
if (!empty($this->options['pages_order'])){ | ||
$this->sortPages(); | ||
} | ||
} | ||
} | ||
|
||
private function findPages(){ | ||
// ignore files with a leading '.' in its filename | ||
$files = Utility::getFiles($this->folder, '\Phile\FilterIterator\ContentFileFilterIterator'); | ||
$this->pages = []; | ||
foreach ($files as $file) { | ||
if (str_replace($this->folder, '', $file) == '404' . CONTENT_EXT) { | ||
// jump to next page if file is the 404 page | ||
continue; | ||
} | ||
$this->pages[] = $this->repository->getPage($file, $this->folder); | ||
} | ||
} | ||
|
||
private function sortPages(){ | ||
$criteria = $this->getSortCriteria(); | ||
|
||
// prepare search criteria for array_multisort | ||
foreach ($criteria as $sort) { | ||
$key = $sort['key']; | ||
$column = array(); | ||
foreach ($this->pages as $page) { | ||
/** @var \Phile\Model\Page $page */ | ||
$meta = $page->getMeta(); | ||
if ($sort['type'] === 'page') { | ||
$method = 'get' . ucfirst($key); | ||
$value = $page->$method(); | ||
} elseif ($sort['type'] === 'meta') { | ||
$value = $meta->get($key); | ||
} else { | ||
continue 2; // ignore unhandled search term | ||
} | ||
$column[] = $value; | ||
} | ||
$sortHelper[] = $column; | ||
$sortHelper[] = constant('SORT_' . strtoupper($sort['order'])); | ||
} | ||
|
||
$sortHelper[] = &$this->pages; | ||
|
||
call_user_func_array('array_multisort', $sortHelper); | ||
} | ||
|
||
private function getSortCriteria(){ | ||
// parse search criteria | ||
$terms = preg_split('/\s+/', $this->options['pages_order'], -1, PREG_SPLIT_NO_EMPTY); | ||
$criteria = []; | ||
foreach ($terms as $term) { | ||
$term = explode('.', $term); | ||
if (count($term) > 1) { | ||
$type = array_shift($term); | ||
} else { | ||
$type = null; | ||
} | ||
$term = explode(':', $term[0]); | ||
$criteria[] = array('type' => $type, 'key' => $term[0], 'order' => $term[1]); | ||
} | ||
|
||
return $criteria; | ||
} | ||
} |
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.
We should try to keep that protected. Users are supposed to call
findByPath
.