mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Implement define()
This commit is contained in:
parent
bacda111b9
commit
7ace2ccab3
@ -122,7 +122,7 @@ class FileCollection implements Countable, IteratorAggregate
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Loads the Filesystem helper and stores initial files.
|
||||
* Loads the Filesystem helper and adds any initial files.
|
||||
*
|
||||
* @param string[] $files
|
||||
*/
|
||||
@ -130,7 +130,17 @@ class FileCollection implements Countable, IteratorAggregate
|
||||
{
|
||||
helper(['filesystem']);
|
||||
|
||||
$this->set($files);
|
||||
$this->add($files)->define();
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies any initial inputs after the constructor.
|
||||
* This method is a stub to be implemented by child classes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function define(): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -99,6 +99,38 @@ class FileCollectionTest extends CIUnitTestCase
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testConstructorAddsFiles()
|
||||
{
|
||||
$expected = [
|
||||
$this->directory . 'apple.php',
|
||||
$this->file,
|
||||
];
|
||||
|
||||
$collection = new class([$this->file]) extends FileCollection {
|
||||
|
||||
protected $files = [
|
||||
SUPPORTPATH . 'Files/able/apple.php',
|
||||
];
|
||||
};
|
||||
|
||||
$this->assertSame($expected, $collection->get());
|
||||
}
|
||||
|
||||
public function testConstructorCallsDefine()
|
||||
{
|
||||
$collection = new class([$this->file]) extends FileCollection {
|
||||
|
||||
protected function define(): void
|
||||
{
|
||||
$this->add(SUPPORTPATH . 'Files/baker/banana.php');
|
||||
}
|
||||
};
|
||||
|
||||
$this->assertSame([$this->file], $collection->get());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testAddStringFile()
|
||||
{
|
||||
$files = new FileCollection();
|
||||
|
@ -144,12 +144,37 @@ When your collection is complete, you can use ``get()`` to retrieve the final li
|
||||
|
||||
Below are the specific methods for working with a ``FileCollection``.
|
||||
|
||||
Inputting Files
|
||||
===============
|
||||
Starting a Collection
|
||||
=====================
|
||||
|
||||
**__construct(string[] $files = [])**
|
||||
|
||||
The constructor accepts an optional array of file paths to use as the initial collection. These are passed to
|
||||
**add()** so any files supplied by child classes in the **$files** will remain.
|
||||
|
||||
**define()**
|
||||
|
||||
Allows child classes to define their own initial files. This method is called by the constructor and allows
|
||||
predefined collections without having to use their methods. Example::
|
||||
|
||||
class ConfigCollection extends \CodeIgniter\Files\FileCollection
|
||||
{
|
||||
protected function define(): void {
|
||||
|
||||
$this->add(APPPATH . 'Config', true)->retainPattern('*.php');
|
||||
}
|
||||
}
|
||||
|
||||
Now you may use the ``ConfigCollection`` anywhere in your project to access all App Config files without
|
||||
having to re-call the collection methods every time.
|
||||
|
||||
**set(array $files)**
|
||||
|
||||
Sets the list of input files to the provided string array of file paths.
|
||||
Sets the list of input files to the provided string array of file paths. This will remove any existing
|
||||
files from the collection, so ``$collection->set([])`` is essentially a hard reset.
|
||||
|
||||
Inputting Files
|
||||
===============
|
||||
|
||||
**add(string[]|string $paths, bool $recursive = true)**
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user