Skip to content

Commit 26605ad

Browse files
authored
Merge pull request #7182 from kenjis/fix-docs-factories.rst
docs: improve factories.rst
2 parents 9a59d99 + 25f120f commit 26605ad

7 files changed

Lines changed: 53 additions & 6 deletions

File tree

user_guide_src/source/concepts/factories.rst

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ What are Factories?
1515
Like :doc:`./services`, **Factories** are an extension of autoloading that helps keep your code
1616
concise yet optimal, without having to pass around object instances between classes.
1717

18+
Factories are similar to CodeIgniter 3's ``$this->load`` in the following points:
19+
20+
- Load a class
21+
- Share the loaded class instance
22+
1823
At its
1924
simplest, Factories provide a common way to create a class instance and access it from
2025
anywhere. This is a great way to reuse object states and reduce memory load from keeping
@@ -44,8 +49,10 @@ Example
4449
Take a look at **Models** as an example. You can access the Factory specific to Models
4550
by using the magic static method of the Factories class, ``Factories::models()``.
4651

52+
The static method name is called *component*.
53+
4754
By default, Factories first searches in the ``App`` namespace for the path corresponding to the magic static method name.
48-
``Factories::models()`` searches the path **Models/**.
55+
``Factories::models()`` searches the **app/Models** directory.
4956

5057
In the following code, if you have ``App\Models\UserModel``, the instance will be returned:
5158

@@ -54,6 +61,7 @@ In the following code, if you have ``App\Models\UserModel``, the instance will b
5461
Or you could also request a specific class:
5562

5663
.. literalinclude:: factories/002.php
64+
:lines: 2-
5765

5866
If you have only ``Blog\Models\UserModel``, the instance will be returned.
5967
But if you have both ``App\Models\UserModel`` and ``Blog\Models\UserModel``,
@@ -62,6 +70,7 @@ the instance of ``App\Models\UserModel`` will be returned.
6270
If you want to get ``Blog\Models\UserModel``, you need to disable the option ``preferApp``:
6371

6472
.. literalinclude:: factories/010.php
73+
:lines: 2-
6574

6675
See :ref:`factories-options` for the details.
6776

@@ -119,8 +128,8 @@ Key Type Description
119128
========== ============== ============================================================ ===================================================
120129
component string or null The name of the component (if different than the static ``null`` (defaults to the component name)
121130
method). This can be used to alias one component to another.
122-
path string or null The relative path within the namespace/folder to look for ``null`` (defaults to the component name)
123-
classes.
131+
path string or null The relative path within the namespace/folder to look for ``null`` (defaults to the component name,
132+
classes. but makes the first character uppercase)
124133
instanceOf string or null A required class name to match on the returned instance. ``null`` (no filtering)
125134
getShared boolean Whether to return a shared instance of the class or load a ``true``
126135
fresh one.
@@ -143,6 +152,9 @@ Configurations
143152
To set default component options, create a new Config files at **app/Config/Factory.php**
144153
that supplies options as an array property that matches the name of the component.
145154

155+
Example: Filters Factories
156+
--------------------------
157+
146158
For example, if you want to create **Filters** by Factories, the component name wll be ``filters``.
147159
And if you want to ensure that each filter is an instance of a class which implements CodeIgniter's ``FilterInterface``,
148160
your **app/Config/Factory.php** file might look like this:
@@ -155,6 +167,22 @@ and the returned instance will surely be a CodeIgniter's filter.
155167
This would prevent conflict of an third-party module which happened to have an
156168
unrelated ``Filters`` path in its namespace.
157169

170+
Example: Library Factories
171+
--------------------------
172+
173+
If you want to load your library classes in the **app/Libraries** directory with
174+
``Factories::library('SomeLib')``, the path `Libraries` is different from the
175+
default path `Library`.
176+
177+
In this case, your **app/Config/Factory.php** file will look like this:
178+
179+
.. literalinclude:: factories/011.php
180+
181+
Now you can load your libraries with the ``Factories::library()`` method:
182+
183+
.. literalinclude:: factories/012.php
184+
:lines: 2-
185+
158186
setOptions Method
159187
=================
160188

user_guide_src/source/concepts/factories/003.php

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

3+
use CodeIgniter\Config\Factories;
4+
35
class SomeOtherClass
46
{
57
public function someFunction()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
$conn = db_connect('AuthDatabase');
3+
$conn = db_connect('auth');
44
$users = Factories::models('UserModel', [], $conn);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
$users = Factories::models('UserModel', ['getShared' => true]); // Default; will always be the same instance
3+
$users = Factories::models('UserModel', ['getShared' => true]); // Default; will always be the same instance
44
$other = Factories::models('UserModel', ['getShared' => false]); // Will always create a new instance
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$widgets = Factories::models('Blog\Models\UserModel', ['preferApp' => false]);
3+
$users = Factories::models('Blog\Models\UserModel', ['preferApp' => false]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\Factory as BaseFactory;
6+
7+
class Factory extends BaseFactory
8+
{
9+
public $library = [
10+
'path' => 'Libraries',
11+
];
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use CodeIgniter\Config\Factories;
4+
5+
$someLib = Factories::library('SomeLib');

0 commit comments

Comments
 (0)