1 : <?php
2 : /**
3 : * This file is part of NoiseLabs-PHP-ToolKit
4 : *
5 : * NoiseLabs-PHP-ToolKit is free software; you can redistribute it
6 : * and/or modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 3 of the License, or (at your option) any later version.
9 : *
10 : * NoiseLabs-PHP-ToolKit is distributed in the hope that it will be
11 : * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 : * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with NoiseLabs-PHP-ToolKit; if not, see
17 : * <http://www.gnu.org/licenses/>.
18 : *
19 : * Copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
20 : *
21 : *
22 : * @category NoiseLabs
23 : * @package Process
24 : * @version 0.1.1
25 : * @author Vítor Brandão <noisebleed@noiselabs.org>
26 : * @copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
27 : */
28 :
29 : namespace NoiseLabs\ToolKit\Runner;
30 :
31 : use NoiseLabs\ToolKit\Runner\ParameterBag;
32 : use NoiseLabs\ToolKit\Runner\ProcessInterface;
33 : use NoiseLabs\ToolKit\Runner\ProcessManagerInterface;
34 :
35 : class ProcessManager implements ProcessManagerInterface
36 : {
37 : protected $processes = array();
38 : public $settings;
39 :
40 : public function __construct(array $settings = array())
41 : {
42 4 : $this->settings = new ParameterBag($settings);
43 4 : }
44 :
45 : public function has($id)
46 : {
47 4 : return isset($this->processes[$id]);
48 : }
49 :
50 : public function add($id, ProcessInterface $process)
51 : {
52 3 : if ($this->has($id)) {
53 0 : throw new \InvalidArgumentException(sprintf('A process with ID "%s" already exists.', $id));
54 : }
55 :
56 3 : return $this->set($id, $process);
57 : }
58 :
59 : /**
60 : * Add a new Process to the container.
61 : * A previous process with the same ID will be overridden.
62 : *
63 : * @param string $id Process ID
64 : * @param NoiseLabs\ToolKit\Runner\ProcessInterface The Process object to
65 : * add
66 : *
67 : * @return self (ProcessManager)
68 : */
69 : public function set($id, ProcessInterface $process)
70 : {
71 4 : if (!is_string($id)) {
72 1 : throw new \InvalidArgumentException('$id must be a string');
73 : }
74 :
75 3 : $this->processes[$id] = $process;
76 : // Copy our own set of settings over Process current settings
77 3 : $this->processes[$id]->settings->add($this->settings->all());
78 :
79 3 : return $this;
80 : }
81 :
82 : /**
83 : * Gets a Process registered in this object.
84 : *
85 : * @param $id Process ID
86 : *
87 : * @throws InvalidArgumentException if no process is registered with $id
88 : */
89 : public function get($id)
90 : {
91 1 : if ($this->has($id)) {
92 1 : return $this->processes[$id];
93 : }
94 : else {
95 0 : throw new \InvalidArgumentException(sprintf('Process ID "%s" does not exist.', $id));
96 : }
97 : }
98 :
99 : public function remove($id)
100 : {
101 2 : if ($this->has($id)) {
102 1 : unset($this->processes[$id]);
103 1 : }
104 : else {
105 1 : throw new \InvalidArgumentException(sprintf('Process ID "%s" does not exist.', $id));
106 : }
107 1 : }
108 :
109 : /**
110 : * Returns true if the section exists (implements the \ArrayAccess
111 : * interface).
112 : *
113 : * @param string $offset The name of the section
114 : *
115 : * @return Boolean true if the section exists, false otherwise
116 : */
117 : public function offsetExists($offset)
118 : {
119 0 : return $this->has($offset);
120 : }
121 :
122 : /**
123 : * Returns the array of options associated with the section (implements
124 : * the \ArrayAccess interface).
125 : *
126 : * @param string $offset The offset of the value to get
127 : *
128 : * @return mixed The array of options associated with the section
129 : */
130 : public function offsetGet($offset)
131 : {
132 0 : return $this->has($offset) ? $this->get($offset) : null;
133 : }
134 :
135 : /**
136 : * Adds an array of options to the given section (implements the
137 : * \ArrayAccess interface).
138 : *
139 : * @param string $offset Process ID
140 : * @param array $values Process to register
141 : */
142 : public function offsetSet($offset, $value)
143 : {
144 0 : $this->set($offset, $value);
145 0 : }
146 :
147 : /**
148 : * Removes the Process with the given ID (implements the
149 : * \ArrayAccess interface).
150 : *
151 : * @param string $name The name of the Process to be removed
152 : */
153 : public function offsetUnset($name)
154 : {
155 0 : $this->has($name) && $this->remove($name);
156 0 : }
157 :
158 : /**
159 : * Returns the number of registered processes (implements the \Countable
160 : * interface).
161 : *
162 : * @return integer The number of sections
163 : */
164 : public function count()
165 : {
166 1 : return count($this->processes);
167 : }
168 :
169 : /**
170 : * Returns the iterator for this group.
171 : *
172 : * @return \ArrayIterator
173 : */
174 : public function getIterator()
175 : {
176 0 : return new \ArrayIterator($this->processes);
177 : }
178 : }
179 :
|