Complex classes like Injector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Injector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
128 | class Injector { |
||
129 | |||
130 | /** |
||
131 | * Local store of all services |
||
132 | * |
||
133 | * @var array |
||
134 | */ |
||
135 | private $serviceCache; |
||
136 | |||
137 | /** |
||
138 | * Cache of items that need to be mapped for each service that gets injected |
||
139 | * |
||
140 | * @var array |
||
141 | */ |
||
142 | private $injectMap; |
||
143 | |||
144 | /** |
||
145 | * A store of all the service configurations that have been defined. |
||
146 | * |
||
147 | * @var array |
||
148 | */ |
||
149 | private $specs; |
||
150 | |||
151 | /** |
||
152 | * A map of all the properties that should be automagically set on all |
||
153 | * objects instantiated by the injector |
||
154 | */ |
||
155 | private $autoProperties; |
||
156 | |||
157 | /** |
||
158 | * A singleton if you want to use it that way |
||
159 | * |
||
160 | * @var Injector |
||
161 | */ |
||
162 | private static $instance; |
||
163 | |||
164 | /** |
||
165 | * Indicates whether or not to automatically scan properties in injected objects to auto inject |
||
166 | * stuff, similar to the way grails does things. |
||
167 | * |
||
168 | * @var boolean |
||
169 | */ |
||
170 | private $autoScanProperties = false; |
||
171 | |||
172 | /** |
||
173 | * The default factory used to create new instances. |
||
174 | * |
||
175 | * The {@link InjectionCreator} is used by default, which simply directly |
||
176 | * creates objects. This can be changed to use a different default creation |
||
177 | * method if desired. |
||
178 | * |
||
179 | * Each individual component can also specify a custom factory to use by |
||
180 | * using the `factory` parameter. |
||
181 | * |
||
182 | * @var Factory |
||
183 | */ |
||
184 | protected $objectCreator; |
||
185 | |||
186 | /** |
||
187 | * Locator for determining Config properties for services |
||
188 | * |
||
189 | * @var ServiceConfigurationLocator |
||
190 | */ |
||
191 | protected $configLocator; |
||
192 | |||
193 | /** |
||
194 | * Create a new injector. |
||
195 | * |
||
196 | * @param array $config |
||
197 | * Service configuration |
||
198 | */ |
||
199 | public function __construct($config = null) { |
||
221 | |||
222 | /** |
||
223 | * The injector instance this one was copied from when Injector::nest() was called. |
||
224 | * |
||
225 | * @var Injector |
||
226 | */ |
||
227 | protected $nestedFrom = null; |
||
228 | |||
229 | /** |
||
230 | * If a user wants to use the injector as a static reference |
||
231 | * |
||
232 | * @param array $config |
||
233 | * @return Injector |
||
234 | */ |
||
235 | public static function inst($config=null) { |
||
241 | |||
242 | /** |
||
243 | * Sets the default global injector instance. |
||
244 | * |
||
245 | * @param Injector $instance |
||
246 | * @return Injector Reference to new active Injector instance |
||
247 | */ |
||
248 | public static function set_inst(Injector $instance) { |
||
251 | |||
252 | /** |
||
253 | * Make the newly active {@link Injector} be a copy of the current active |
||
254 | * {@link Injector} instance. |
||
255 | * |
||
256 | * You can then make changes to the injector with methods such as |
||
257 | * {@link Injector::inst()->registerService()} which will be discarded |
||
258 | * upon a subsequent call to {@link Injector::unnest()} |
||
259 | * |
||
260 | * @return Injector Reference to new active Injector instance |
||
261 | */ |
||
262 | public static function nest() { |
||
269 | |||
270 | /** |
||
271 | * Change the active Injector back to the Injector instance the current active |
||
272 | * Injector object was copied from. |
||
273 | * |
||
274 | * @return Injector Reference to restored active Injector instance |
||
275 | */ |
||
276 | public static function unnest() { |
||
288 | |||
289 | /** |
||
290 | * Indicate whether we auto scan injected objects for properties to set. |
||
291 | * |
||
292 | * @param boolean $val |
||
293 | */ |
||
294 | public function setAutoScanProperties($val) { |
||
297 | |||
298 | /** |
||
299 | * Sets the default factory to use for creating new objects. |
||
300 | * |
||
301 | * @param Factory $obj |
||
302 | */ |
||
303 | public function setObjectCreator(Factory $obj) { |
||
306 | |||
307 | /** |
||
308 | * @return Factory |
||
309 | */ |
||
310 | public function getObjectCreator() { |
||
313 | |||
314 | /** |
||
315 | * Set the configuration locator |
||
316 | * @param ServiceConfigurationLocator $configLocator |
||
317 | */ |
||
318 | public function setConfigLocator($configLocator) { |
||
321 | |||
322 | /** |
||
323 | * Retrieve the configuration locator |
||
324 | * @return ServiceConfigurationLocator |
||
325 | */ |
||
326 | public function getConfigLocator() { |
||
329 | |||
330 | /** |
||
331 | * Add in a specific mapping that should be catered for on a type. |
||
332 | * This allows configuration of what should occur when an object |
||
333 | * of a particular type is injected, and what items should be injected |
||
334 | * for those properties / methods. |
||
335 | * |
||
336 | * @param string $class The class to set a mapping for |
||
337 | * @param string $property The property to set the mapping for |
||
338 | * @param string $toInject The registered type that will be injected |
||
339 | * @param string $injectVia Whether to inject by setting a property or calling a setter |
||
340 | */ |
||
341 | public function setInjectMapping($class, $property, $toInject, $injectVia = 'property') { |
||
348 | |||
349 | /** |
||
350 | * Add an object that should be automatically set on managed objects |
||
351 | * |
||
352 | * This allows you to specify, for example, that EVERY managed object |
||
353 | * will be automatically inject with a log object by the following |
||
354 | * |
||
355 | * $injector->addAutoProperty('log', new Logger()); |
||
356 | * |
||
357 | * @param string $property |
||
358 | * the name of the property |
||
359 | * @param object $object |
||
360 | * the object to be set |
||
361 | */ |
||
362 | public function addAutoProperty($property, $object) { |
||
366 | |||
367 | /** |
||
368 | * Load services using the passed in configuration for those services |
||
369 | * |
||
370 | * @param array $config |
||
371 | */ |
||
372 | public function load($config = array()) { |
||
440 | |||
441 | /** |
||
442 | * Update the configuration of an already defined service |
||
443 | * |
||
444 | * Use this if you don't want to register a complete new config, just append |
||
445 | * to an existing configuration. Helpful to avoid overwriting someone else's changes |
||
446 | * |
||
447 | * updateSpec('RequestProcessor', 'filters', '%$MyFilter') |
||
448 | * |
||
449 | * @param string $id |
||
450 | * The name of the service to update the definition for |
||
451 | * @param string $property |
||
452 | * The name of the property to update. |
||
453 | * @param mixed $value |
||
454 | * The value to set |
||
455 | * @param boolean $append |
||
456 | * Whether to append (the default) when the property is an array |
||
457 | */ |
||
458 | public function updateSpec($id, $property, $value, $append = true) { |
||
475 | |||
476 | /** |
||
477 | * Update a class specification to convert constructor configuration information if needed |
||
478 | * |
||
479 | * We do this as a separate process to avoid unneeded calls to convertServiceProperty |
||
480 | * |
||
481 | * @param array $spec |
||
482 | * The class specification to update |
||
483 | */ |
||
484 | protected function updateSpecConstructor(&$spec) { |
||
489 | |||
490 | /** |
||
491 | * Recursively convert a value into its proper representation with service references |
||
492 | * resolved to actual objects |
||
493 | * |
||
494 | * @param string $value |
||
495 | */ |
||
496 | public function convertServiceProperty($value) { |
||
511 | |||
512 | /** |
||
513 | * Instantiate a managed object |
||
514 | * |
||
515 | * Given a specification of the form |
||
516 | * |
||
517 | * array( |
||
518 | * 'class' => 'ClassName', |
||
519 | * 'properties' => array('property' => 'scalar', 'other' => '%$BeanRef') |
||
520 | * 'id' => 'ServiceId', |
||
521 | * 'type' => 'singleton|prototype' |
||
522 | * ) |
||
523 | * |
||
524 | * will create a new object, store it in the service registry, and |
||
525 | * set any relevant properties |
||
526 | * |
||
527 | * Optionally, you can pass a class name directly for creation |
||
528 | * |
||
529 | * To access this from the outside, you should call ->get('Name') to ensure |
||
530 | * the appropriate checks are made on the specific type. |
||
531 | * |
||
532 | * |
||
533 | * @param array $spec |
||
534 | * The specification of the class to instantiate |
||
535 | * @param string $id |
||
536 | * The name of the object being created. If not supplied, then the id will be inferred from the |
||
537 | * object being created |
||
538 | * @param string $type |
||
539 | * Whether to create as a singleton or prototype object. Allows code to be explicit as to how it |
||
540 | * wants the object to be returned |
||
541 | */ |
||
542 | protected function instantiate($spec, $id=null, $type = null) { |
||
585 | |||
586 | /** |
||
587 | * Inject $object with available objects from the service cache |
||
588 | * |
||
589 | * @todo Track all the existing objects that have had a service bound |
||
590 | * into them, so we can update that binding at a later point if needbe (ie |
||
591 | * if the managed service changes) |
||
592 | * |
||
593 | * @param object $object |
||
594 | * The object to inject |
||
595 | * @param string $asType |
||
596 | * The ID this item was loaded as. This is so that the property configuration |
||
597 | * for a type is referenced correctly in case $object is no longer the same |
||
598 | * type as the loaded config specification had it as. |
||
599 | */ |
||
600 | public function inject($object, $asType=null) { |
||
698 | |||
699 | /** |
||
700 | * Helper to set a property's value |
||
701 | * |
||
702 | * @param object $object |
||
703 | * Set an object's property to a specific value |
||
704 | * @param string $name |
||
705 | * The name of the property to set |
||
706 | * @param mixed $value |
||
707 | * The value to set |
||
708 | */ |
||
709 | protected function setObjectProperty($object, $name, $value) { |
||
716 | |||
717 | /** |
||
718 | * Does the given service exist, and if so, what's the stored name for it? |
||
719 | * |
||
720 | * We do a special check here for services that are using compound names. For example, |
||
721 | * we might want to say that a property should be injected with Log.File or Log.Memory, |
||
722 | * but have only registered a 'Log' service, we'll instead return that. |
||
723 | * |
||
724 | * Will recursively call hasService for each depth of dotting |
||
725 | * |
||
726 | * @return string |
||
727 | * The name of the service (as it might be different from the one passed in) |
||
728 | */ |
||
729 | public function hasService($name) { |
||
743 | |||
744 | /** |
||
745 | * Register a service object with an optional name to register it as the |
||
746 | * service for |
||
747 | * |
||
748 | * @param stdClass $service |
||
749 | * The object to register |
||
750 | * @param string $replace |
||
751 | * The name of the object to replace (if different to the |
||
752 | * class name of the object to register) |
||
753 | * |
||
754 | */ |
||
755 | public function registerService($service, $replace = null) { |
||
765 | |||
766 | /** |
||
767 | * Register a service with an explicit name |
||
768 | * |
||
769 | * @deprecated since 4.0 |
||
770 | */ |
||
771 | public function registerNamedService($name, $service) { |
||
775 | |||
776 | /** |
||
777 | * Removes a named object from the cached list of objects managed |
||
778 | * by the inject |
||
779 | * |
||
780 | * @param string $name The name to unregister |
||
781 | */ |
||
782 | public function unregisterNamedObject($name) { |
||
785 | |||
786 | /** |
||
787 | * Clear out all objects that are managed by the injetor. |
||
788 | */ |
||
789 | public function unregisterAllObjects() { |
||
792 | |||
793 | /** |
||
794 | * Get a named managed object |
||
795 | * |
||
796 | * Will first check to see if the item has been registered as a configured service/bean |
||
797 | * and return that if so. |
||
798 | * |
||
799 | * Next, will check to see if there's any registered configuration for the given type |
||
800 | * and will then try and load that |
||
801 | * |
||
802 | * Failing all of that, will just return a new instance of the |
||
803 | * specificied object. |
||
804 | * |
||
805 | * @param string $name |
||
806 | * the name of the service to retrieve. If not a registered |
||
807 | * service, then a class of the given name is instantiated |
||
808 | * @param boolean $asSingleton |
||
809 | * Whether to register the created object as a singleton |
||
810 | * if no other configuration is found |
||
811 | * @param array $constructorArgs |
||
812 | * Optional set of arguments to pass as constructor arguments |
||
813 | * if this object is to be created from scratch |
||
814 | * (ie asSingleton = false) |
||
815 | * @return mixed the instance of the specified object |
||
816 | */ |
||
817 | public function get($name, $asSingleton = true, $constructorArgs = null) { |
||
871 | |||
872 | /** |
||
873 | * Magic method to return an item directly |
||
874 | * |
||
875 | * @param string $name |
||
876 | * The named object to retrieve |
||
877 | * @return mixed |
||
878 | */ |
||
879 | public function __get($name) { |
||
882 | |||
883 | /** |
||
884 | * Similar to get() but always returns a new object of the given type |
||
885 | * |
||
886 | * Additional parameters are passed through as |
||
887 | * |
||
888 | * @param string $name |
||
889 | * @return mixed A new instance of the specified object |
||
890 | */ |
||
891 | public function create($name) { |
||
896 | |||
897 | /** |
||
898 | * Creates an object with the supplied argument array |
||
899 | * |
||
900 | * @param string $name |
||
901 | * Name of the class to create an object of |
||
902 | * @param array $args |
||
903 | * Arguments to pass to the constructor |
||
904 | * @return mixed |
||
905 | */ |
||
906 | public function createWithArgs($name, $constructorArgs) { |
||
909 | } |
||
910 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.