11#########
2- Factories
2+ 工廠模式
33#########
44
55.. contents ::
66 :local:
77 :depth: 2
88
9- Introduction
9+ 簡介
1010============
1111
12- Like ``Services ``, ``Factories `` are an extension of autoloading that helps keep your code
13- concise yet optimal, without having to pass around object instances between classes. At its
14- simplest, Factories provide a common way to create a class instance and access it from
15- anywhere. This is a great way to reuse object states and reduce memory load from keeping
16- multiple instances loaded across your app.
12+ 如同 ``services `` 一樣,工廠( ``Factories `` )模式是自動載入的一種延伸,它有助於維持程式碼的簡潔,不必在類別之間傳遞物件實體。簡單來說,工廠模式提供了新建實體後從任何地方存取它的方法。這是一種能夠重複使用物件狀態以減少記憶體負載,並保持多個實體在應用程式中載入的好方式。
1713
18- Anything can be loaded by Factories, but the best examples are those classes that are used
19- to work on or transmit common data. The framework itself uses Factories internally, e.g., to
20- make sure the correct configuration is loaded when using the ``Config `` class.
14+ 任何物件都可以被工廠載入,但最好的使用場景是處理那些用於傳輸普通資料的類別。CodeIgniter 本身也使用了工廠模式,使 ``Config `` 類別總是能載入正確的組態設定。
2115
22- Take a look at ``Models `` as an example. You can access the Factory specific to ``Models ``
23- by using the magic static method of the Factories class, ``Factories::models() ``. Because of
24- the common path structure for namespaces and folders, Factories know that the model files
25- and classes are found within **Models **, so you can request a model by its shorthand base name::
16+ 以 ``Models `` 為例,你可以使用工廠類別中的魔術靜態方法 ``Factories::models() `` 來存取 ``Models `` 的特定工廠。由於命名空間和資料夾有著相同的路徑結構,工廠了解模型檔案與類別被歸類在 **Models ** 資料夾下,所以你可以透過名稱直接向工廠請求一個模型的載入:
17+
18+ ::
2619
2720 use CodeIgniter\Config\Factories;
2821
2922 $users = Factories::models('UserModel');
3023
31- Or you could also request a specific class::
24+ 或是你也可以請求一個特定的類別
25+
26+ ::
3227
3328 $widgets = Factories::models('Some\Namespace\Models\WidgetModel');
3429
35- Next time you ask for the same class anywhere in your code, ``Factories `` will be sure
36- you get back the instance as before::
30+ 無論在何處請求相同的類別時, ``Factories `` 都會確保你取得取得一模一樣的實體。
31+
32+ ::
3733
3834 class SomeOtherClass
3935 {
4036 $widgets = Factories::models('WidgetModel');
4137 // ...
4238 }
4339
44- Factory Parameters
40+ 參數
4541==================
4642
47- ``Factories `` takes as a second parameter an array of option values (described below).
48- These directives will override the default options configured for each component.
43+ 你能夠將控制可選功能的陣列傳入 ``Factories `` 的第二個參數,你所傳入的設定將會覆蓋預設的選項。
44+
45+ 同時你也能緊接著傳入更多參數,這些參數將被轉送到類別的建構函數中,方便你從外部初始化你的類別。例如:假設你的應用程式使用獨立資料庫進行身分認證,並且你希望任何存取使用者紀錄的行為總是使用特定的連線進行時:
4946
50- Any more parameters passed at the same time will be forwarded on to the class
51- constructor, making it easy to configure your class instance on-the-fly. For example, say
52- your app uses a separate database for authentication and you want to be sure that any attempts
53- to access user records always go through that connection::
47+ ::
5448
5549 $conn = db_connect('AuthDatabase');
5650 $users = Factories::models('UserModel', [], $conn);
5751
58- Now any time the ``UserModel `` is loaded from ``Factories `` it will in fact be returning a
59- class instance that uses the alternate database connection.
52+ 現在,每當 ``UserModel `` 從 ``Factories `` 載入時,它都會回傳類別實體,並使用你所希望的資料庫連線。
6053
61- Factories Options
54+ 可選設定
6255==================
6356
64- The default behavior might not work for every component. For example, say your component
65- name and its path do not align, or you need to limit instances to a certain type of class.
66- Each component takes a set of options to direct discovery and instantiation.
67-
68- ========== ============== ==================================================================================================================== ===================================================
69- Key Type Description Default
70- ========== ============== ==================================================================================================================== ===================================================
71- component string or null The name of the component (if different than the static method). This can be used to alias one component to another. ``null `` (defaults to the component name)
72- path string or null The relative path within the namespace/folder to look for classes. ``null `` (defaults to the component name)
73- instanceOf string or null A required class name to match on the returned instance. ``null `` (no filtering)
74- getShared boolean Whether to return a shared instance of the class or load a fresh one. ``true ``
75- preferApp boolean Whether a class with the same basename in the App namespace overrides other explicit class requests. ``true ``
76- ========== ============== ==================================================================================================================== ===================================================
77-
78- Factories Behavior
57+ 預設的載入模式可能不適合每個元件,例如:你的元件名稱與它的路徑不一致,或者是你需要將實體限制為特定類型的類別上。每個元件可能都需要特定的可選設定來指揮 ``Factories `` 探索與實體化。
58+
59+ +------------+----------------+-------------------------------------------------------------------------+---------------------------+
60+ | Key | 型別 | 說明 | 預設值 |
61+ +============+================+=========================================================================+===========================+
62+ | component | string or null | 元件的名稱(如果與靜態方法不同)。替一個元件別名為另一個元件。 | ``null `` (預設元件名稱) |
63+ +------------+----------------+-------------------------------------------------------------------------+---------------------------+
64+ | path | string or null | 命名空間/資料夾內查找類的相對路徑。 | ``null `` (預設元件名稱) |
65+ +------------+----------------+-------------------------------------------------------------------------+---------------------------+
66+ | instanceOf | string or null | 所需的類別名稱,以確認回傳的實體。 | ``null `` (不過濾) |
67+ +------------+----------------+-------------------------------------------------------------------------+---------------------------+
68+ | getShared | boolean | 回傳一個共用實體還是載入一個新實體。 | ``true `` |
69+ +------------+----------------+-------------------------------------------------------------------------+---------------------------+
70+ | preferApp | boolean | 若是在 App 命名空間中具有相同基本名稱的類別,是否優先於其他明確的請求。 | ``true `` |
71+ +------------+----------------+-------------------------------------------------------------------------+---------------------------+
72+
73+ 行為
7974==================
8075
81- Options can be applied in one of three ways (listed in ascending priority):
76+ 可以透過下列三種方式應用可選設定(按照優先級列出):
8277
83- * A configuration file ``Factory `` with a component property.
84- * The static method ``Factories::setOptions ``.
85- * Passing options directly at call time with a parameter.
78+ * 帶有元件屬性的組態設定檔案 ``Factory ``
79+ * 靜態方法 ``Factories::setOptions ``
80+ * 在呼叫時直接透過參數傳遞可選設定
8681
87- Configurations
82+ 組態設定
8883--------------
8984
90- To set default component options, create a new Config files at **app/Config/Factory.php **
91- that supplies options as an array property that matches the name of the component. For example,
92- if you wanted to ensure that all Filters used by your app were valid framework instances,
93- your **Factories.php ** file might look like this::
85+ 要設定預設的元件選項,請在 **app/Config/Factory.php ** 中建立一個成員變數,並命名為元件名稱。這個變數為一個鍵值陣列,你可以宣告你所需要的可選設定內容。例如:如果你想要保證你的應用程式使用的所有過濾器都是有效的框架實體,你的 **Factories.php** 可能看起來會像這樣:
86+
87+ ::
9488
9589 <?php
9690
@@ -106,32 +100,28 @@ your **Factories.php** file might look like this::
106100 ];
107101 }
108102
109- This would prevent conflict of an unrelated third-party module which happened to have an
110- unrelated "Filters" path in its namespace.
103+ 如果某個模組碰巧在命名空間中擁有一個不相關的「Filters」路徑,這將防止第三方模組發生衝突。
111104
112- setOptions Method
105+ setOptions 方法
113106-----------------
114107
115- The ``Factories `` class has a static method to allow runtime option configuration: simply
116- supply the desired array of options using the `` setOptions() `` method and they will be
117- merged with the default values and stored for the next call ::
108+ ``Factories `` 類別提供一個靜態方法,它能夠在執行期間進行可選設定的配置,只需要將設定用的鍵值陣列傳入 `` setOptions() `` ,它們將會與預設值合併並儲存以供下一個呼叫
109+
110+ ::
118111
119112 Factories::setOptions('filters', [
120113 'instanceOf' => FilterInterface::class,
121114 'prefersApp' => false,
122115 ]);
123116
124- Parameter Options
117+ 參數傳遞
125118-----------------
126119
127- ``Factories ``'s magic static call takes as a second parameter an array of option values.
128- These directives will override the stored options configured for each component and can be
129- used at call time to get exactly what you need. The input should be an array with option
130- names as keys to each overriding value.
120+ ``Factories `` 的魔術靜態呼叫允許你將可選設定作為第二個參數傳入。這些指令將會覆蓋每個元件的可選設定,並且可以在呼叫時使用以準確的獲得你所需要的內容。你應該要傳入一個陣列,其中的鍵為選項名稱,其值將會覆蓋預設設定。
121+
122+ 舉個例子,在預設的情況下, ``Factories `` 會假設你所需要的元件是共用實體。透過傳入第二個參數至魔術靜態呼叫中,你可以控制該次呼叫是回傳新實體還是共用實體:
131123
132- For example, by default ``Factories `` assumes that you want to locate a shared instance of
133- a component. By adding a second parameter to the magic static call, you can control whether
134- that single call will return a new or shared instance::
124+ ::
135125
136- $users = Factories::models('UserModel', ['getShared' => true]); // Default; will always be the same instance
137- $other = Factories::models('UserModel', ['getShared' => false]); // Will always create a new instance
126+ $users = Factories::models('UserModel', ['getShared' => true]); // 預設;; 將永遠為相同實體
127+ $other = Factories::models('UserModel', ['getShared' => false]); // 將永遠建立新的實體
0 commit comments