Permalink
Browse files

in Container, add newFactory() method, and add new Factory class

  • Loading branch information...
1 parent 6f2c728 commit f84cc615b5d101d23d2a99161c11550829ba7580 @pmjones pmjones committed Feb 20, 2013
Showing with 62 additions and 3 deletions.
  1. +1 −0 src.php
  2. +13 −3 src/Aura/Di/Container.php
  3. +48 −0 src/Aura/Di/Factory.php
View
@@ -7,6 +7,7 @@
require_once __DIR__ . '/src/Aura/Di/Exception/ContainerLocked.php';
require_once __DIR__ . '/src/Aura/Di/Exception/ServiceInvalid.php';
require_once __DIR__ . '/src/Aura/Di/Exception/ServiceNotFound.php';
+require_once __DIR__ . '/src/Aura/Di/Factory.php';
require_once __DIR__ . '/src/Aura/Di/ForgeInterface.php';
require_once __DIR__ . '/src/Aura/Di/Forge.php';
require_once __DIR__ . '/src/Aura/Di/Lazy.php';
View
@@ -287,7 +287,7 @@ public function getDefs()
* have complex logic or heavy overhead when creating a param that may or
* may not need to be loaded.
*
- * $di->params['ClassName']['param_name'] = Lazy(function () {
+ * $di->params['ClassName']['param_name'] = $di->lazy(function () {
* return include 'filename.php';
* });
*
@@ -306,7 +306,7 @@ public function lazy(callable $callable)
* Returns a Lazy that gets a service. This allows you to replace the
* following idiom ...
*
- * $di->params['ClassName']['param_name'] = new \Aura\Di\Lazy(function() use ($di)) {
+ * $di->params['ClassName']['param_name'] = $di->lazy(function() use ($di)) {
* return $di->get('service');
* }
*
@@ -353,7 +353,7 @@ public function newInstance($class, array $params = [], array $setters = [])
* Returns a Lazy that creates a new instance. This allows you to replace
* the following idiom:
*
- * $di->params['ClassName']['param_name'] = new \Aura\Di\Lazy(function () use ($di)) {
+ * $di->params['ClassName']['param_name'] = $di->lazy(function () use ($di)) {
* return $di->newInstance('OtherClass', [...]);
* });
*
@@ -462,4 +462,14 @@ public function lazyCall($callable)
// return wrapped in a Lazy, and done
return $this->lazy($call);
}
+
+ // returns a factory that creates an object over and over again (as vs
+ // creating it one time like the lazyNew() or newInstance() methods).
+ // this is good for complex objects that need setup after creation.
+ // we don't want this to be a Lazy because we don't want the Forge to
+ // recognize and invoke it when passed as a param or setter value.
+ public function newFactory($class, array $params = [], array $setters = [])
+ {
+ return new Factory($this->forge, $class, $params, $setters);
+ }
}
View
@@ -0,0 +1,48 @@
+<?php
+/**
+ *
+ * This file is part of the Aura Project for PHP.
+ *
+ * @package Aura.Di
+ *
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ *
+ */
+namespace Aura\Di;
+
+/**
+ *
+ * A generic factory to create objects of a single class.
+ *
+ * @package Aura.Di
+ *
+ */
+class Factory
+{
+ protected $forge;
+
+ protected $class;
+
+ protected $params;
+
+ protected $setter;
+
+ public function __construct(
+ ForgeInterface $forge,
+ $class,
+ array $params = [],
+ array $setter = []
+ ) {
+ $this->forge = $forge;
+ $this->class = $class;
+ $this->params = $params;
+ $this->setter = $setter;
+ }
+
+ // func_get_args() are overrides to the overrides
+ public function __invoke()
+ {
+ $params = array_merge($this->params, func_get_args());
+ return $this->forge($this->class, $params, $this->setter);
+ }
+}

0 comments on commit f84cc61

Please sign in to comment.