Open Journal Systems  3.3.0
CompositeFactory.php
1 <?php
2 
4 
7 
11 class CompositeFactory implements \IteratorAggregate, \Countable, FactoryInterface
12 {
14  protected $factories;
15 
23  public static function getDefaultChain(ClientInterface $client)
24  {
25  $factories = array();
26  if ($description = $client->getDescription()) {
27  $factories[] = new ServiceDescriptionFactory($description);
28  }
29  $factories[] = new ConcreteClassFactory($client);
30 
31  return new self($factories);
32  }
33 
37  public function __construct(array $factories = array())
38  {
39  $this->factories = $factories;
40  }
41 
50  public function add(FactoryInterface $factory, $before = null)
51  {
52  $pos = null;
53 
54  if ($before) {
55  foreach ($this->factories as $i => $f) {
56  if ($before instanceof FactoryInterface) {
57  if ($f === $before) {
58  $pos = $i;
59  break;
60  }
61  } elseif (is_string($before)) {
62  if ($f instanceof $before) {
63  $pos = $i;
64  break;
65  }
66  }
67  }
68  }
69 
70  if ($pos === null) {
71  $this->factories[] = $factory;
72  } else {
73  array_splice($this->factories, $i, 0, array($factory));
74  }
75 
76  return $this;
77  }
78 
86  public function has($factory)
87  {
88  return (bool) $this->find($factory);
89  }
90 
98  public function remove($factory = null)
99  {
100  if (!($factory instanceof FactoryInterface)) {
101  $factory = $this->find($factory);
102  }
103 
104  $this->factories = array_values(array_filter($this->factories, function($f) use ($factory) {
105  return $f !== $factory;
106  }));
107 
108  return $this;
109  }
110 
118  public function find($factory)
119  {
120  foreach ($this->factories as $f) {
121  if ($factory === $f || (is_string($factory) && $f instanceof $factory)) {
122  return $f;
123  }
124  }
125  }
126 
135  public function factory($name, array $args = array())
136  {
137  foreach ($this->factories as $factory) {
138  $command = $factory->factory($name, $args);
139  if ($command) {
140  return $command;
141  }
142  }
143  }
144 
145  public function count()
146  {
147  return count($this->factories);
148  }
149 
150  public function getIterator()
151  {
152  return new \ArrayIterator($this->factories);
153  }
154 }
Guzzle\Service\Command\Factory\CompositeFactory\add
add(FactoryInterface $factory, $before=null)
Definition: CompositeFactory.php:53
Guzzle\Service\Command\Factory\CompositeFactory\$factories
$factories
Definition: CompositeFactory.php:17
Guzzle\Service\Command\Factory\CompositeFactory\find
find($factory)
Definition: CompositeFactory.php:121
Guzzle\Service\Command\Factory\CompositeFactory\getDefaultChain
static getDefaultChain(ClientInterface $client)
Definition: CompositeFactory.php:26
Guzzle\Service\Command\Factory\CompositeFactory\count
count()
Definition: CompositeFactory.php:148
Guzzle\Service\Command\Factory\CompositeFactory\factory
factory($name, array $args=array())
Definition: CompositeFactory.php:138
Guzzle\Service\Command\Factory\ConcreteClassFactory
Definition: ConcreteClassFactory.php:12
Guzzle\Service\Command\Factory
Definition: AliasFactory.php:3
Guzzle\Service\Command\CommandInterface
Definition: CommandInterface.php:17
Guzzle\Service\ClientInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Service/ClientInterface.php:16
Guzzle\Service\Command\Factory\CompositeFactory\has
has($factory)
Definition: CompositeFactory.php:89
Guzzle\Service\Command\Factory\CompositeFactory\getIterator
getIterator()
Definition: CompositeFactory.php:153
Guzzle\Service\Command\Factory\CompositeFactory\__construct
__construct(array $factories=array())
Definition: CompositeFactory.php:40
Guzzle\Service\Command\Factory\FactoryInterface
Definition: FactoryInterface.php:10
Guzzle\Service\Command\Factory\ServiceDescriptionFactory
Definition: ServiceDescriptionFactory.php:11
Guzzle\Service\Command\Factory\CompositeFactory
Definition: CompositeFactory.php:11