Use Files::fromPath() instead of constructor

This commit is contained in:
Giuseppe Criscione 2019-06-02 00:31:15 +02:00
parent 0e6e573fa1
commit aca961e4bf
2 changed files with 24 additions and 32 deletions

View File

@ -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);
}
/**

View File

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