Skip to content

Commit eafbcd8

Browse files
authored
Merge pull request #8867 from kenjis/docs-file_collections.rst
docs: improve file_collections.rst
2 parents 95d13c5 + 0125bc7 commit eafbcd8

3 files changed

Lines changed: 43 additions & 26 deletions

File tree

user_guide_src/source/libraries/file_collections.rst

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
File Collections
33
################
44

5-
Working with groups of files can be cumbersome, so the framework supplies the ``FileCollection`` class to facilitate
6-
locating and working with groups of files across the filesystem.
5+
Working with groups of files can be cumbersome, so the framework supplies the
6+
``FileCollection`` class to facilitate locating and working with groups of files
7+
across the filesystem.
78

89
.. contents::
910
:local:
@@ -18,13 +19,15 @@ of files you set or build:
1819

1920
.. literalinclude:: files/011.php
2021

21-
After you have input the files you would like to work with you may remove files or use the filtering commands to remove
22-
or retain files matching a certain regex or glob-style pattern:
22+
After you have input the files you would like to work with you may remove files
23+
or use the filtering commands to remove or retain files matching a certain regex
24+
or glob-style pattern:
2325

2426
.. literalinclude:: files/012.php
2527

26-
When your collection is complete, you can use ``get()`` to retrieve the final list of file paths, or take advantage of
27-
``FileCollection`` being countable and iterable to work directly with each ``File``:
28+
When your collection is complete, you can use ``get()`` to retrieve the final
29+
list of file paths, or take advantage of ``FileCollection`` being countable and
30+
iterable to work directly with each ``File``:
2831

2932
.. literalinclude:: files/013.php
3033

@@ -37,25 +40,31 @@ Starting a Collection
3740
__construct(string[] $files = [])
3841
=================================
3942

40-
The constructor accepts an optional array of file paths to use as the initial collection. These are passed to
41-
``add()`` so any files supplied by child classes in the ``$files`` will remain.
43+
The constructor accepts an optional array of file paths to use as the initial
44+
collection. These are passed to ``add()`` so any files supplied by child classes
45+
in the ``$files`` will remain.
4246

4347
define()
4448
========
4549

46-
Allows child classes to define their own initial files. This method is called by the constructor and allows
47-
predefined collections without having to use their methods. Example:
50+
Allows child classes to define their own initial files. This method is called by
51+
the constructor and allows predefined collections without having to use their
52+
methods.
53+
54+
Example:
4855

4956
.. literalinclude:: files/014.php
5057

51-
Now you may use the ``ConfigCollection`` anywhere in your project to access all App Config files without
52-
having to re-call the collection methods every time.
58+
Now you may use the ``ConfigCollection`` anywhere in your project to access all
59+
PHP files in **app/Config/** without having to re-call the collection methods
60+
every time.
5361

5462
set(array $files)
5563
=================
5664

57-
Sets the list of input files to the provided string array of file paths. This will remove any existing
58-
files from the collection, so ``$collection->set([])`` is essentially a hard reset.
65+
Sets the list of input files to the provided string array of file paths. This
66+
will remove any existing files from the collection, so ``$collection->set([])``
67+
is essentially a hard reset.
5968

6069
***************
6170
Inputting Files
@@ -64,13 +73,14 @@ Inputting Files
6473
add(string[]|string $paths, bool $recursive = true)
6574
===================================================
6675

67-
Adds all files indicated by the path or array of paths. If the path resolves to a directory then ``$recursive``
68-
will include sub-directories.
76+
Adds all files indicated by the path or array of paths. If the path resolves to
77+
a directory then ``$recursive`` will include sub-directories.
6978

7079
addFile(string $file) / addFiles(array $files)
7180
==============================================
7281

73-
Adds the file or files to the current list of input files. Files are absolute paths to actual files.
82+
Adds the file or files to the current list of input files. Files are absolute
83+
paths to actual files.
7484

7585
removeFile(string $file) / removeFiles(array $files)
7686
====================================================
@@ -82,8 +92,8 @@ addDirectory(string $directory, bool $recursive = false)
8292
addDirectories(array $directories, bool $recursive = false)
8393
===========================================================
8494

85-
Adds all files from the directory or directories, optionally recursing into sub-directories. Directories are
86-
absolute paths to actual directories.
95+
Adds all files from the directory or directories, optionally recursing into
96+
sub-directories. Directories are absolute paths to actual directories.
8797

8898
***************
8999
Filtering Files
@@ -94,11 +104,15 @@ removePattern(string $pattern, string $scope = null)
94104
retainPattern(string $pattern, string $scope = null)
95105
====================================================
96106

97-
Filters the current file list through the pattern (and optional scope), removing or retaining matched
98-
files. ``$pattern`` may be a complete regex (like ``'#[A-Za-z]+\.php#'``) or a pseudo-regex similar
99-
to ``glob()`` (like ``*.css``).
100-
If a ``$scope`` is provided then only files in or under that directory will be considered (i.e. files
101-
outside of ``$scope`` are always retained). When no scope is provided then all files are subject.
107+
Filters the current file list through the pattern (and optional scope), removing
108+
or retaining matched files.
109+
110+
``$pattern`` may be a complete regex (like ``'#\A[A-Za-z]+\.php\z#'``) or a
111+
pseudo-regex similar to ``glob()`` (like ``'*.css'``).
112+
113+
If a ``$scope`` is provided then only files in or under that directory will be
114+
considered (i.e. files outside of ``$scope`` are always retained). When no scope
115+
is provided then all files are subject.
102116

103117
Examples:
104118

@@ -113,4 +127,5 @@ get(): string[]
113127

114128
Returns an array of all the loaded input files.
115129

116-
.. note:: ``FileCollection`` is an ``IteratorAggregate`` so you can work with it directly (e.g. ``foreach ($collection as $file)``).
130+
.. note:: ``FileCollection`` is an ``IteratorAggregate`` so you can work with it
131+
directly (e.g. ``foreach ($collection as $file)``).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$files->removeFile(APPPATH . 'Filters/DevelopToolbar');
3+
$files->removeFile(APPPATH . 'Filters/DevelopToolbar.php');
44

55
$files->removePattern('#\.gitkeep#');
66
$files->retainPattern('*.php');

user_guide_src/source/libraries/files/014.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace App;
4+
35
use CodeIgniter\Files\FileCollection;
46

57
class ConfigCollection extends FileCollection

0 commit comments

Comments
 (0)