SCANDIR_SORT_* constants are available since PHP 5.4

Thanks @Lomanic
This commit is contained in:
Daniel Rudolf 2015-10-04 14:15:11 +02:00
parent ab8994593f
commit 3a4721a20c

View File

@ -26,6 +26,30 @@
*/ */
class Pico class Pico
{ {
/**
* Sort files in alphabetical ascending order
*
* @see Pico::getFiles()
* @var int
*/
const SORT_ASC = 0;
/**
* Sort files in alphabetical descending order
*
* @see Pico::getFiles()
* @var int
*/
const SORT_DESC = 1;
/**
* Don't sort files
*
* @see Pico::getFiles()
* @var int
*/
const SORT_NONE = 2;
/** /**
* Root directory of this Pico instance * Root directory of this Pico instance
* *
@ -708,7 +732,7 @@ class Pico
protected function readPages() protected function readPages()
{ {
$this->pages = array(); $this->pages = array();
$files = $this->getFiles($this->getConfig('content_dir'), $this->getConfig('content_ext'), SCANDIR_SORT_NONE); $files = $this->getFiles($this->getConfig('content_dir'), $this->getConfig('content_ext'), Pico::SORT_NONE);
foreach ($files as $i => $file) { foreach ($files as $i => $file) {
// skip 404 page // skip 404 page
if (basename($file) == '404' . $this->getConfig('content_ext')) { if (basename($file) == '404' . $this->getConfig('content_ext')) {
@ -990,12 +1014,12 @@ class Pico
* @param string $fileExtension return files with the given file extension * @param string $fileExtension return files with the given file extension
* only (optional) * only (optional)
* @param int $order specify whether and how files should be * @param int $order specify whether and how files should be
* sorted; use SCANDIR_SORT_ASCENDING for a alphabetical ascending * sorted; use Pico::SORT_ASC for a alphabetical ascending order (this
* order (default), SCANDIR_SORT_DESCENDING for a descending order or * is the default behaviour), Pico::SORT_DESC for a descending order
* SCANDIR_SORT_NONE to leave the result unsorted * or Pico::SORT_NONE to leave the result unsorted
* @return array list of found files * @return array list of found files
*/ */
protected function getFiles($directory, $fileExtension = '', $order = SCANDIR_SORT_ASCENDING) protected function getFiles($directory, $fileExtension = '', $order = self::SORT_ASC)
{ {
$directory = rtrim($directory, '/'); $directory = rtrim($directory, '/');
$result = array(); $result = array();
@ -1013,7 +1037,7 @@ class Pico
if (is_dir($directory . '/' . $file)) { if (is_dir($directory . '/' . $file)) {
// get files recursively // get files recursively
$result = array_merge($result, $this->getFiles($directory . '/' . $file, $fileExtension)); $result = array_merge($result, $this->getFiles($directory . '/' . $file, $fileExtension, $order));
} elseif (empty($fileExtension) || (substr($file, -$fileExtensionLength) === $fileExtension)) { } elseif (empty($fileExtension) || (substr($file, -$fileExtensionLength) === $fileExtension)) {
$result[] = $directory . '/' . $file; $result[] = $directory . '/' . $file;
} }