diff --git a/formwork/Core/Page.php b/formwork/Core/Page.php index 8f1a8343..ef36186e 100755 --- a/formwork/Core/Page.php +++ b/formwork/Core/Page.php @@ -500,7 +500,7 @@ class Page extends AbstractPage $this->template = new Template($contentFiles[$key]['template'], $this); } - $this->files = new Files($files, $this->path); + $this->files = Files::fromPath($this->path, $files); } /** diff --git a/formwork/Files/Files.php b/formwork/Files/Files.php index a79fce2b..ecf9e3bf 100755 --- a/formwork/Files/Files.php +++ b/formwork/Files/Files.php @@ -7,37 +7,6 @@ use Formwork\Utils\FileSystem; class Files extends AssociativeCollection { - /** - * Root path of the file collection - * - * @var string - */ - protected $path; - - /** - * Create a new instance of Files - * - * @param array $files - * @param mixed $path - */ - public function __construct(array $files = array(), $path) - { - $this->path = FileSystem::normalize($path); - foreach ($files as $file) { - $this->items[$file] = new File($this->path . $file); - } - } - - /** - * Get files path - * - * @return string - */ - public function path() - { - return $this->path; - } - /** * Filter files by a given type * @@ -53,4 +22,27 @@ class Files extends AssociativeCollection }); return $files; } + + /** + * Create a collection getting files from a given path + * + * @param string $path + * @param array|null $filenames Array of file names to include (all files by default) + * + * @return self + */ + public static function fromPath($path, array $filenames = null) + { + if (is_null($filenames)) { + $filenames = FileSystem::listFiles($path); + } + + $files = array(); + + foreach ($filenames as $filename) { + $files[$filename] = new File($path . $filename); + } + + return new static($files); + } }