1: <?php
2:
3: namespace Guzzle\Service\Command\Factory;
4:
5: use Guzzle\Service\Description\ServiceDescriptionInterface;
6: use Guzzle\Inflection\InflectorInterface;
7:
8: /**
9: * Command factory used to create commands based on service descriptions
10: */
11: class ServiceDescriptionFactory implements FactoryInterface
12: {
13: /**
14: * @var ServiceDescriptionInterface
15: */
16: protected $description;
17:
18: /**
19: * @var InflectorInterface
20: */
21: protected $inflector;
22:
23: /**
24: * @param ServiceDescriptionInterface $description Service description
25: * @param InflectorInterface $inflector Optional inflector to use if the command is not at first found
26: */
27: public function __construct(ServiceDescriptionInterface $description, InflectorInterface $inflector = null)
28: {
29: $this->setServiceDescription($description);
30: $this->inflector = $inflector;
31: }
32:
33: /**
34: * Change the service description used with the factory
35: *
36: * @param ServiceDescriptionInterface $description Service description to use
37: *
38: * @return FactoryInterface
39: */
40: public function setServiceDescription(ServiceDescriptionInterface $description)
41: {
42: $this->description = $description;
43:
44: return $this;
45: }
46:
47: /**
48: * Returns the service description
49: *
50: * @return ServiceDescriptionInterface
51: */
52: public function getServiceDescription()
53: {
54: return $this->description;
55: }
56:
57: /**
58: * {@inheritdoc}
59: */
60: public function factory($name, array $args = array())
61: {
62: $command = $this->description->getOperation($name);
63:
64: // If a command wasn't found, then try to uppercase the first letter and try again
65: if (!$command) {
66: $command = $this->description->getOperation(ucfirst($name));
67: // If an inflector was passed, then attempt to get the command using snake_case inflection
68: if (!$command && $this->inflector) {
69: $command = $this->description->getOperation($this->inflector->snake($name));
70: }
71: }
72:
73: if ($command) {
74: $class = $command->getClass();
75: return new $class($args, $command, $this->description);
76: }
77: }
78: }
79: