@@ 20-108 (lines=89) @@ | ||
17 | use Symfony\Component\Config\Definition\NodeInterface; |
|
18 | use Webcreate\Conveyor\Factory\TaskFactory; |
|
19 | ||
20 | class TaskNode extends ArrayNode |
|
21 | { |
|
22 | protected $prepared = false; |
|
23 | protected $originalChildren = array(); |
|
24 | ||
25 | /** |
|
26 | * Constructor. |
|
27 | * |
|
28 | * @param string $name |
|
29 | * @param NodeInterface $parent |
|
30 | */ |
|
31 | public function __construct($name, NodeInterface $parent = null) |
|
32 | { |
|
33 | parent::__construct($name, $parent); |
|
34 | ||
35 | //$this->setIgnoreExtraKeys(true); |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * Set task factory |
|
40 | * |
|
41 | * @param TaskFactory|null $factory |
|
42 | */ |
|
43 | public function setTaskFactory($factory) |
|
44 | { |
|
45 | $this->taskFactory = $factory; |
|
46 | } |
|
47 | ||
48 | /** |
|
49 | * Takes child nodes from a ConfigurtionInterface instance |
|
50 | * and adds these to this node |
|
51 | * |
|
52 | * @param string $type |
|
53 | * @throws InvalidConfigurationException |
|
54 | */ |
|
55 | protected function prepareChildren($type) |
|
56 | { |
|
57 | // when we hit this function for the first time, |
|
58 | // we store the original children. Each time we |
|
59 | // hit this function we reset the children to the |
|
60 | // original ones. |
|
61 | if (false === $this->prepared) { |
|
62 | $this->originalChildren = $this->children; |
|
63 | $this->prepared = true; |
|
64 | } |
|
65 | ||
66 | $tasks = $this->taskFactory->getTasks(); |
|
67 | ||
68 | $this->children = $this->originalChildren; |
|
69 | ||
70 | if (isset($tasks[$type])) { |
|
71 | $configuration = $this->taskFactory->configuration($type); |
|
72 | ||
73 | if ($configuration instanceof ConfigurationInterface) { |
|
74 | $tree = $configuration->getConfigTreeBuilder()->buildTree(); |
|
75 | foreach ($tree->getChildren() as $child) { |
|
76 | $this->addChild($child); |
|
77 | } |
|
78 | } |
|
79 | } else { |
|
80 | throw new InvalidConfigurationException(sprintf( |
|
81 | 'Task type "%s" does not exist at path "%s". Did you mean any of %s?', $type, |
|
82 | $this->getPath(), |
|
83 | implode(', ', array_keys($tasks)) |
|
84 | )); |
|
85 | } |
|
86 | } |
|
87 | ||
88 | /** |
|
89 | * We hook into the validateType method, this |
|
90 | * gets called form the normalize method. |
|
91 | * |
|
92 | * @param mixed $value |
|
93 | * |
|
94 | * @throws InvalidTypeException |
|
95 | */ |
|
96 | protected function validateType($value) |
|
97 | { |
|
98 | if (isset($value['type'])) { |
|
99 | $this->prepareChildren($value['type']); |
|
100 | } else { |
|
101 | // ignore extra keys so the error message will be |
|
102 | // focused on the missing type field |
|
103 | $this->setIgnoreExtraKeys(true); |
|
104 | } |
|
105 | ||
106 | parent::validateType($value); |
|
107 | } |
|
108 | } |
|
109 |
@@ 21-112 (lines=92) @@ | ||
18 | use Symfony\Component\Config\Definition\NodeInterface; |
|
19 | use Webcreate\Conveyor\Factory\TransporterFactory; |
|
20 | ||
21 | class TransporterNode extends ArrayNode |
|
22 | { |
|
23 | protected $prepared = false; |
|
24 | protected $originalChildren = array(); |
|
25 | ||
26 | /** |
|
27 | * @var TransporterFactory |
|
28 | */ |
|
29 | protected $transporterFactory; |
|
30 | ||
31 | /** |
|
32 | * Constructor. |
|
33 | * |
|
34 | * @param string $name |
|
35 | * @param NodeInterface $parent |
|
36 | */ |
|
37 | public function __construct($name, NodeInterface $parent = null) |
|
38 | { |
|
39 | parent::__construct($name, $parent); |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * Set transporter factory |
|
44 | * |
|
45 | * @param TransporterFactory|null $factory |
|
46 | */ |
|
47 | public function setTransporterFactory($factory) |
|
48 | { |
|
49 | $this->transporterFactory = $factory; |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * Takes child nodes from a ConfigurtionInterface instance |
|
54 | * and adds these to this node |
|
55 | * |
|
56 | * @param string $type |
|
57 | * @throws InvalidConfigurationException |
|
58 | */ |
|
59 | protected function prepareChildren($type) |
|
60 | { |
|
61 | // when we hit this function for the first time, |
|
62 | // we store the original children. Each time we |
|
63 | // hit this function we reset the children to the |
|
64 | // original ones. |
|
65 | if (false === $this->prepared) { |
|
66 | $this->originalChildren = $this->children; |
|
67 | $this->prepared = true; |
|
68 | } |
|
69 | ||
70 | $transporters = $this->transporterFactory->getTransporters(); |
|
71 | ||
72 | $this->children = $this->originalChildren; |
|
73 | ||
74 | if (isset($transporters[$type])) { |
|
75 | $configuration = $this->transporterFactory->configuration($type); |
|
76 | ||
77 | if ($configuration instanceof ConfigurationInterface) { |
|
78 | $tree = $configuration->getConfigTreeBuilder()->buildTree(); |
|
79 | foreach ($tree->getChildren() as $child) { |
|
80 | $this->addChild($child); |
|
81 | } |
|
82 | } |
|
83 | } else { |
|
84 | throw new InvalidConfigurationException(sprintf( |
|
85 | 'Transporter type "%s" does not exist at path "%s". Did you mean any of %s?', $type, |
|
86 | $this->getPath(), |
|
87 | implode(', ', array_keys($transporters)) |
|
88 | )); |
|
89 | } |
|
90 | } |
|
91 | ||
92 | /** |
|
93 | * We hook into the validateType method, this |
|
94 | * gets called form the normalize method. |
|
95 | * |
|
96 | * @param mixed $value |
|
97 | * |
|
98 | * @throws InvalidTypeException |
|
99 | */ |
|
100 | protected function validateType($value) |
|
101 | { |
|
102 | if (isset($value['type'])) { |
|
103 | $this->prepareChildren($value['type']); |
|
104 | } else { |
|
105 | // ignore extra keys so the error message will be |
|
106 | // focused on the missing type field |
|
107 | $this->setIgnoreExtraKeys(true); |
|
108 | } |
|
109 | ||
110 | parent::validateType($value); |
|
111 | } |
|
112 | } |
|
113 |