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 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
94 changes: 48 additions & 46 deletions lib/Phile/Repository/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,63 +84,65 @@ 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;
return new PageCollection(function() use ($options, $folder){
$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);
}
$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;
if (empty($options['pages_order'])) {
return $pages;
}
$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);
// 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 {
continue 2; // ignore unhandled search term
$type = null;
}
$column[] = $value;
$term = explode(':', $term[0]);
$sorting[] = array('type' => $type, 'key' => $term[0], 'order' => $term[1]);
}
$sortHelper[] = $column;
$sortHelper[] = constant('SORT_' . strtoupper($sort['order']));
}
$sortHelper[] = &$pages;

call_user_func_array('array_multisort', $sortHelper);
// 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 $pages;
});
}

/**
Expand Down
64 changes: 64 additions & 0 deletions lib/Phile/Repository/PageCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Phile\Repository;


/**
* Page collection which delays searching for and loading pages until necessary.
*
* @author PhileCMS
* @link https://philecms.com
* @license http://opensource.org/licenses/MIT
* @package Phile\Repository
*/
class PageCollection implements \ArrayAccess, \IteratorAggregate, \Countable {
/**
* @var callback A function to be used for loading the pages.
*/
private $loader;

/**
* @var \Phile\Model\Page[] Array of loaded pages.
*/
private $pages;

public function __construct($loader){
$this->loader = $loader;
}

private function load(){
if ($this->pages === null){
$this->pages = call_user_func($this->loader);
}
}

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);
}
}