Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
58 changes: 3 additions & 55 deletions lib/Phile/Repository/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,63 +84,11 @@ public function findByPath($pageId, $folder = CONTENT_DIR) {
* @param array $options
* @param string $folder
*
* @return array of \Phile\Model\Page objects
* @return PageCollection of \Phile\Model\Page objects
*/
public function findAll(array $options = array(), $folder = CONTENT_DIR) {
$options += $this->settings;
// ignore files with a leading '.' in its filename
$files = Utility::getFiles($folder, '\Phile\FilterIterator\ContentFileFilterIterator');
$pages = array();
foreach ($files as $file) {
if (str_replace($folder, '', $file) == '404' . CONTENT_EXT) {
// jump to next page if file is the 404 page
continue;
}
$pages[] = $this->getPage($file, $folder);
}

if (empty($options['pages_order'])) {
return $pages;
}

// parse search criteria
$terms = preg_split('/\s+/', $options['pages_order'], -1, PREG_SPLIT_NO_EMPTY);
foreach ($terms as $term) {
$term = explode('.', $term);
if (count($term) > 1) {
$type = array_shift($term);
} else {
$type = null;
}
$term = explode(':', $term[0]);
$sorting[] = array('type' => $type, 'key' => $term[0], 'order' => $term[1]);
}

// prepare search criteria for array_multisort
foreach ($sorting as $sort) {
$key = $sort['key'];
$column = array();
foreach ($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[] = &$pages;

call_user_func_array('array_multisort', $sortHelper);

return $pages;
return new PageCollection($options, $folder, $this);
}

/**
Expand Down Expand Up @@ -171,7 +119,7 @@ public function getPageOffset(\Phile\Model\Page $page, $offset = 0) {
*
* @return mixed|\Phile\Model\Page
*/
protected function getPage($filePath, $folder = CONTENT_DIR) {
public function getPage($filePath, $folder = CONTENT_DIR) {
Copy link
Member

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.

$key = 'Phile_Model_Page_' . md5($filePath);
if (isset($this->storage[$key])) {
return $this->storage[$key];
Expand Down
140 changes: 140 additions & 0 deletions lib/Phile/Repository/PageCollection.php
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;
}
}