1: <?php
2:
3: namespace Guzzle\Service\Resource;
4:
5: use Guzzle\Inflection\InflectorInterface;
6: use Guzzle\Inflection\Inflector;
7: use Guzzle\Service\Command\CommandInterface;
8:
9: /**
10: * Factory for creating {@see ResourceIteratorInterface} objects using a convention of storing iterator classes under a
11: * root namespace using the name of a {@see CommandInterface} object as a convention for determining the name of an
12: * iterator class. The command name is converted to CamelCase and Iterator is appended (e.g. abc_foo => AbcFoo).
13: */
14: class ResourceIteratorClassFactory extends AbstractResourceIteratorFactory
15: {
16: /**
17: * @var array List of namespaces used to look for classes
18: */
19: protected $namespaces;
20:
21: /**
22: * @var InflectorInterface Inflector used to determine class names
23: */
24: protected $inflector;
25:
26: /**
27: * @param string|array $namespaces List of namespaces for iterator objects
28: * @param InflectorInterface $inflector Inflector used to resolve class names
29: */
30: public function __construct($namespaces = array(), InflectorInterface $inflector = null)
31: {
32: $this->namespaces = (array) $namespaces;
33: $this->inflector = $inflector ?: Inflector::getDefault();
34: }
35:
36: /**
37: * Registers a namespace to check for Iterators
38: *
39: * @param string $namespace Namespace which contains Iterator classes
40: *
41: * @return self
42: */
43: public function registerNamespace($namespace)
44: {
45: array_unshift($this->namespaces, $namespace);
46:
47: return $this;
48: }
49:
50: /**
51: * {@inheritdoc}
52: */
53: protected function getClassName(CommandInterface $command)
54: {
55: $iteratorName = $this->inflector->camel($command->getName()) . 'Iterator';
56:
57: // Determine the name of the class to load
58: foreach ($this->namespaces as $namespace) {
59: $potentialClassName = $namespace . '\\' . $iteratorName;
60: if (class_exists($potentialClassName)) {
61: return $potentialClassName;
62: }
63: }
64:
65: return false;
66: }
67: }
68: