Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Attributes | Static Private Attributes | List of all members
Hoa\Core\Protocol\Protocol Class Reference
Inheritance diagram for Hoa\Core\Protocol\Protocol:

Public Member Functions

 __construct ($name=null, $reach=null)
 
 offsetSet ($name, $component)
 
 offsetGet ($name)
 
 offsetExists ($name)
 
 offsetUnset ($name)
 
 resolve ($path, $exists=true, $unfold=false)
 
 reach ($queue=null)
 
 reachId ($id)
 
 setReach ($reach)
 
 getName ()
 
 getIterator ()
 
 __toString ()
 

Static Public Member Functions

static clearCache ()
 

Public Attributes

const NO_RESOLUTION = '/hoa/flatland'
 

Protected Member Functions

 _resolve ($path, &$accumulator, $id=null)
 
 _resolveChoice ($reach, Array &$accumulator)
 
 getReach ()
 

Protected Attributes

 $_name = null
 
 $_reach = null
 

Private Attributes

 $_components = []
 

Static Private Attributes

static $_cache = []
 

Detailed Description

Definition at line 50 of file Protocol.php.

Constructor & Destructor Documentation

Hoa\Core\Protocol\Protocol::__construct (   $name = null,
  $reach = null 
)

Construct a protocol's component. If it is not a data object (i.e. if it is not extend this class to overload the $this->_name property), we can set the $this->_name property dynamically. So usefull to create components on the fly…

Parameters
string$nameComponent's name.
string$reachPath for the reach() method.
Returns
void

Definition at line 99 of file Protocol.php.

100  {
101  if (null !== $name) {
102  $this->_name = $name;
103  }
104 
105  if (null !== $reach) {
106  $this->_reach = $reach;
107  }
108 
109  return;
110  }

Member Function Documentation

Hoa\Core\Protocol\Protocol::__toString ( )

Print a tree of component.

Returns
string

Definition at line 460 of file Protocol.php.

461  {
462  static $i = 0;
463 
464  $out = str_repeat(' ', $i) . $this->getName() . "\n";
465 
466  foreach ($this as $foo => $component) {
467  ++$i;
468  $out .= $component;
469  --$i;
470  }
471 
472  return $out;
473  }

Here is the call graph for this function:

Hoa\Core\Protocol\Protocol::_resolve (   $path,
$accumulator,
  $id = null 
)
protected

Resolve a path, i.e. iterate the components tree and reach the queue of the path.

Parameters
string$pathPath to resolve.
array&$accumulatorCombination of all possibles paths.
string$idID.
Returns
mixed

Definition at line 260 of file Protocol.php.

261  {
262  if (substr($path, 0, 6) == 'hoa://') {
263  $path = substr($path, 6);
264  }
265 
266  if (empty($path)) {
267  return null;
268  }
269 
270  if (null === $accumulator) {
271  $accumulator = [];
272  $posId = strpos($path, '#');
273 
274  if (false !== $posId) {
275  $id = substr($path, $posId + 1);
276  $path = substr($path, 0, $posId);
277  } else {
278  $id = null;
279  }
280  }
281 
282  $path = trim($path, '/');
283  $pos = strpos($path, '/');
284 
285  if (false !== $pos) {
286  $next = substr($path, 0, $pos);
287  } else {
288  $next = $path;
289  }
290 
291  if (isset($this[$next])) {
292  if (false === $pos) {
293  if (null === $id) {
294  $this->_resolveChoice($this[$next]->reach(), $accumulator);
295 
296  return true;
297  }
298 
299  $accumulator = null;
300 
301  return $this[$next]->reachId($id);
302  }
303 
304  $tnext = $this[$next];
305  $this->_resolveChoice($tnext->reach(), $accumulator);
306 
307  return $tnext->_resolve(substr($path, $pos + 1), $accumulator, $id);
308  }
309 
310  $this->_resolveChoice($this->reach($path), $accumulator);
311 
312  return true;
313  }
_resolveChoice($reach, Array &$accumulator)
Definition: Protocol.php:322

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Core\Protocol\Protocol::_resolveChoice (   $reach,
Array &  $accumulator 
)
protected

Resolve choices, i.e. a reach value has a “;”.

Parameters
string$reachReach value.
array&$accumulatorCombination of all possibles paths.
Returns
void

Definition at line 322 of file Protocol.php.

323  {
324  if (empty($accumulator)) {
325  $accumulator = explode(';', $reach);
326 
327  return;
328  }
329 
330  if (false === strpos($reach, ';')) {
331  if (false !== $pos = strrpos($reach, "\r")) {
332  $reach = substr($reach, $pos + 1);
333 
334  foreach ($accumulator as &$entry) {
335  $entry = null;
336  }
337  }
338 
339  foreach ($accumulator as &$entry) {
340  $entry .= $reach;
341  }
342 
343  return;
344  }
345 
346  $choices = explode(';', $reach);
347  $ref = $accumulator;
348  $accumulator = [];
349 
350  foreach ($choices as $choice) {
351  if (false !== $pos = strrpos($choice, "\r")) {
352  $choice = substr($choice, $pos + 1);
353 
354  foreach ($ref as $entry) {
355  $accumulator[] = $choice;
356  }
357  } else {
358  foreach ($ref as $entry) {
359  $accumulator[] = $entry . $choice;
360  }
361  }
362  }
363 
364  unset($ref);
365 
366  return;
367  }

Here is the caller graph for this function:

static Hoa\Core\Protocol\Protocol::clearCache ( )
static

Clear cache.

Returns
void

Definition at line 374 of file Protocol.php.

375  {
376  self::$_cache = [];
377 
378  return;
379  }
Hoa\Core\Protocol\Protocol::getIterator ( )

Get an iterator.

Returns

Definition at line 450 of file Protocol.php.

451  {
452  return new \ArrayIterator($this->_components);
453  }
Hoa\Core\Protocol\Protocol::getName ( )

Get component's name.

Returns
string

Definition at line 430 of file Protocol.php.

431  {
432  return $this->_name;
433  }

Here is the caller graph for this function:

Hoa\Core\Protocol\Protocol::getReach ( )
protected

Get reach's root.

Returns
string

Definition at line 440 of file Protocol.php.

441  {
442  return $this->_reach;
443  }
Hoa\Core\Protocol\Protocol::offsetExists (   $name)

Check if a component exists.

Parameters
string$nameComponent name.
Returns
bool

Definition at line 173 of file Protocol.php.

174  {
175  return array_key_exists($name, $this->_components);
176  }
Hoa\Core\Protocol\Protocol::offsetGet (   $name)

Get a specific component.

Parameters
string$nameComponent name.
Returns
Exceptions

Definition at line 154 of file Protocol.php.

155  {
156  if (!isset($this[$name])) {
157  throw new Core\Exception(
158  'Component %s does not exist.',
159  2,
160  $name
161  );
162  }
163 
164  return $this->_components[$name];
165  }
Hoa\Core\Protocol\Protocol::offsetSet (   $name,
  $component 
)

Add a component.

Parameters
string$nameComponent name. If null, will be set to name of $component.
\Hoa\Core\Protocol$componentComponent to add.
Returns
Exceptions

Definition at line 121 of file Protocol.php.

122  {
123  if (!($component instanceof self)) {
124  throw new Core\Exception(
125  'Component must extend %s.',
126  0,
127  __CLASS__
128  );
129  }
130 
131  if (empty($name)) {
132  $name = $component->getName();
133  }
134 
135  if (empty($name)) {
136  throw new Core\Exception(
137  'Cannot add a component to the protocol hoa:// without a name.',
138  1
139  );
140  }
141 
142  $this->_components[$name] = $component;
143 
144  return;
145  }
Hoa\Core\Protocol\Protocol::offsetUnset (   $name)

Remove a component.

Parameters
string$nameComponent name to remove.
Returns

Definition at line 184 of file Protocol.php.

185  {
186  unset($this->_components[$name]);
187 
188  return;
189  }
Hoa\Core\Protocol\Protocol::reach (   $queue = null)

Queue of the component. Generic one. Should be overload in children classes.

Parameters
string$queueQueue of the component (generally, a filename, with probably a query).
Returns
mixed

Definition at line 389 of file Protocol.php.

390  {
391  return empty($queue) ? $this->_reach : $queue;
392  }

Here is the caller graph for this function:

Hoa\Core\Protocol\Protocol::reachId (   $id)

ID of the component. Generic one. Should be overload in children classes.

Parameters
string$idID of the component.
Returns
mixed
Exceptions

Definition at line 402 of file Protocol.php.

403  {
404  throw new Core\Exception(
405  'The component %s has no ID support (tried to reach #%s).',
406  4,
407  [$this->getName(), $id]
408  );
409  }

Here is the call graph for this function:

Hoa\Core\Protocol\Protocol::resolve (   $path,
  $exists = true,
  $unfold = false 
)

Front method for resolving a path. Please, look the $this->_resolve() method.

Parameters
string$pathPath to resolve.
bool$existsIf true, try to find the first that exists, else return the first solution.
bool$unfoldReturn all solutions instead of one.
Returns
mixed

Definition at line 201 of file Protocol.php.

202  {
203  if (substr($path, 0, 6) !== 'hoa://') {
204  return $path;
205  }
206 
207  if (isset(self::$_cache[$path])) {
208  $handle = self::$_cache[$path];
209  } else {
210  $out = $this->_resolve($path, $handle);
211 
212  // Not a path but a resource.
213  if (!is_array($handle)) {
214  return $out;
215  }
216 
217  $handle = array_values(array_unique($handle, SORT_REGULAR));
218 
219  self::$_cache[$path] = $handle;
220  }
221 
222  if (true === $unfold) {
223  if (true !== $exists) {
224  return $handle;
225  }
226 
227  $out = [];
228 
229  foreach ($handle as $solution) {
230  if (file_exists($solution)) {
231  $out[] = $solution;
232  }
233  }
234 
235  return $out;
236  }
237 
238  if (true !== $exists) {
239  return $handle[0];
240  }
241 
242  foreach ($handle as $solution) {
243  if (file_exists($solution)) {
244  return $solution;
245  }
246  }
247 
248  return static::NO_RESOLUTION;
249  }
_resolve($path, &$accumulator, $id=null)
Definition: Protocol.php:260

Here is the call graph for this function:

Hoa\Core\Protocol\Protocol::setReach (   $reach)

Set a new reach value.

Parameters
string$reachReach value.
Returns
string

Definition at line 417 of file Protocol.php.

418  {
419  $old = $this->_reach;
420  $this->_reach = $reach;
421 
422  return $old;
423  }

Member Data Documentation

Hoa\Core\Protocol\Protocol::$_cache = []
staticprivate

Definition at line 85 of file Protocol.php.

Hoa\Core\Protocol\Protocol::$_components = []
private

Definition at line 78 of file Protocol.php.

Hoa\Core\Protocol\Protocol::$_name = null
protected

Definition at line 64 of file Protocol.php.

Hoa\Core\Protocol\Protocol::$_reach = null
protected

Definition at line 71 of file Protocol.php.

const Hoa\Core\Protocol\Protocol::NO_RESOLUTION = '/hoa/flatland'

No resolution value.

string

Definition at line 57 of file Protocol.php.


The documentation for this class was generated from the following file: