1313use Lit \Air \Recipe \FixedValueRecipe ;
1414use Lit \Air \Recipe \RecipeInterface ;
1515
16+ /**
17+ * Configurator helps to build an array configuration, and writes array configuration into a container.
18+ * http://litphp.github.io/docs/air-config
19+ */
1620class Configurator
1721{
1822 protected static $ decorators = [
1923 'callback ' => CallbackDecorator::class,
2024 'singleton ' => SingletonDecorator::class,
2125 ];
2226
27+ /**
28+ * Write a configuration array into a container
29+ *
30+ * @param Container $container The container.
31+ * @param array $config The configuration array.
32+ * @param boolean $force Whether overwrite existing values.
33+ * @return void
34+ */
2335 public static function config (Container $ container , array $ config , bool $ force = true ): void
2436 {
2537 foreach ($ config as $ key => $ value ) {
@@ -30,6 +42,12 @@ public static function config(Container $container, array $config, bool $force =
3042 }
3143 }
3244
45+ /**
46+ * Convert a mixed value into a recipe.
47+ *
48+ * @param mixed $value The value.
49+ * @return RecipeInterface
50+ */
3351 public static function convertToRecipe ($ value ): RecipeInterface
3452 {
3553 if (is_object ($ value ) && $ value instanceof RecipeInterface) {
@@ -47,11 +65,24 @@ public static function convertToRecipe($value): RecipeInterface
4765 return Container::value ($ value );
4866 }
4967
68+ /**
69+ * Configuration indicating a singleton
70+ *
71+ * @param string $classname The class name.
72+ * @param array $extra Extra parameters.
73+ * @return array
74+ */
5075 public static function singleton (string $ classname , array $ extra = []): array
5176 {
5277 return self ::decorateSingleton (self ::instance ($ classname , $ extra ));
5378 }
5479
80+ /**
81+ * Decorate a configuration, makes it a singleton (\Lit\Air\Recipe\Decorator\SingletonDecorator)
82+ *
83+ * @param array $config The configuration.
84+ * @return array
85+ */
5586 public static function decorateSingleton (array $ config ): array
5687 {
5788 $ config ['decorator ' ] = $ config ['decorator ' ] ?? [];
@@ -60,6 +91,13 @@ public static function decorateSingleton(array $config): array
6091 return $ config ;
6192 }
6293
94+ /**
95+ * Decorate a configuration with provided callback
96+ *
97+ * @param array $config The configuration.
98+ * @param callable $callback The callback.
99+ * @return array
100+ */
63101 public static function decorateCallback (array $ config , callable $ callback ): array
64102 {
65103 $ config ['decorator ' ] = $ config ['decorator ' ] ?? [];
@@ -68,6 +106,12 @@ public static function decorateCallback(array $config, callable $callback): arra
68106 return $ config ;
69107 }
70108
109+ /**
110+ * Provide extra parameter for autowired entry. The key should be a valid class name.
111+ *
112+ * @param array $extra Extra parameters.
113+ * @return array
114+ */
71115 public static function provideParameter (array $ extra = []): array
72116 {
73117 return [
@@ -77,6 +121,13 @@ public static function provideParameter(array $extra = []): array
77121 ];
78122 }
79123
124+ /**
125+ * Configuration indicating an autowired entry.
126+ *
127+ * @param string|null $classname The class name. Can be ignored but better use `provideParameter` in that case.
128+ * @param array $extra Extra parameters.
129+ * @return array
130+ */
80131 public static function produce (?string $ classname , array $ extra = []): array
81132 {
82133 return [
@@ -86,6 +137,13 @@ public static function produce(?string $classname, array $extra = []): array
86137 ];
87138 }
88139
140+ /**
141+ * Configuration indicating an instance created by factory.
142+ *
143+ * @param string $classname The class name.
144+ * @param array $extra Extra parameters.
145+ * @return array
146+ */
89147 public static function instance (string $ classname , array $ extra = []): array
90148 {
91149 return [
@@ -95,6 +153,12 @@ public static function instance(string $classname, array $extra = []): array
95153 ];
96154 }
97155
156+ /**
157+ * Configuration indicating an alias
158+ *
159+ * @param string ...$key Multiple keys will be auto joined.
160+ * @return array
161+ */
98162 public static function alias (string ... $ key ): array
99163 {
100164 return [
@@ -103,15 +167,28 @@ public static function alias(string... $key): array
103167 ];
104168 }
105169
170+ /**
171+ * Configuration wrapping a builder method
172+ *
173+ * @param callable $builder The builder method.
174+ * @param array $extra Extra parameters.
175+ * @return array
176+ */
106177 public static function builder (callable $ builder , array $ extra = []): array
107178 {
108179 return [
109180 '$ ' => 'builder ' ,
110181 $ builder ,
111- $ extra
182+ $ extra,
112183 ];
113184 }
114185
186+ /**
187+ * Configuration wraps an arbitary value. For arrays it's recommended to always wrap with this.
188+ *
189+ * @param mixed $value The value.
190+ * @return array
191+ */
115192 public static function value ($ value ): array
116193 {
117194 return [
@@ -144,10 +221,6 @@ protected static function write(Container $container, $key, $value)
144221 }
145222 }
146223
147- /**
148- * @param array $value
149- * @return array
150- */
151224 protected static function convertArray (array $ value ): array
152225 {
153226 $ result = [];
@@ -194,8 +267,10 @@ protected static function makeRecipe(array $value): RecipeInterface
194267 }
195268
196269 /**
197- * @param array $decorators
198- * @param RecipeInterface $recipe
270+ * Apply decorators to a recipe and return the decorated recipe
271+ *
272+ * @param array $decorators Assoc array of decorator names => options.
273+ * @param RecipeInterface $recipe The decorated recipe instance.
199274 * @return RecipeInterface
200275 */
201276 public static function wrapRecipeWithDecorators (array $ decorators , RecipeInterface $ recipe ): RecipeInterface
@@ -222,6 +297,12 @@ public static function wrapRecipeWithDecorators(array $decorators, RecipeInterfa
222297 return $ recipe ;
223298 }
224299
300+ /**
301+ * Join multiple strings with air conventional separator `::`
302+ *
303+ * @param string ...$args Parts of the key to be joined.
304+ * @return string
305+ */
225306 public static function join (string ... $ args ): string
226307 {
227308 return implode (':: ' , $ args );
0 commit comments