Hoa central
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Static Private Attributes | List of all members
Hoa\Core\Consistency\Consistency Class Reference

Public Member Functions

 import ($pattern, $load=false)
 
 foreachImport ($pattern, $callback)
 
 setRoot ($root, $from=null)
 
 getRoot ()
 
 getFroms ()
 
 getImportedClasses ()
 

Static Public Member Functions

static from ($namespace)
 
static autoload ($classname)
 
static dnew ($classname, Array $arguments=[])
 
static getAllImportedClasses ()
 
static getEntityShortestName ($entityName)
 
static entityExists ($entityName, $autoloader=false)
 
static flexEntity ($entityName)
 
static isKeyword ($word)
 
static isIdentifier ($id)
 

Protected Member Functions

 _import ($pattern, $load, $callback=null)
 

Protected Attributes

 $_from = null
 
 $_roots = []
 
 $__class = []
 

Static Protected Attributes

static $_cache = []
 
static $_class
 

Private Member Functions

 __construct ($from)
 

Static Private Attributes

static $_multiton = []
 

Detailed Description

Class Hoa.

This class manages all classes, importations etc.

Definition at line 57 of file Consistency.php.

Constructor & Destructor Documentation

Hoa\Core\Consistency\Consistency::__construct (   $from)
private

Singleton to manage a library family.

Parameters
string$fromLibrary family's name.
Returns
void

Definition at line 124 of file Consistency.php.

125  {
126  $this->_from = preg_split('#\s*(,|or)\s*#', trim($from, '()'));
127  $parameters = Core::getInstance()->getParameters();
128  $wildcard = $parameters->getFormattedParameter('namespace.prefix.*');
129 
130  foreach ($this->_from as $f) {
131  $this->setRoot(
132  $parameters->getFormattedParameter('namespace.prefix.' . $f)
133  ?: $wildcard,
134  $f
135  );
136  }
137 
138  return;
139  }
static getInstance()
Definition: Core.php:193

Here is the call graph for this function:

Member Function Documentation

Hoa\Core\Consistency\Consistency::_import (   $pattern,
  $load,
  $callback = null 
)
protected

Real import implementation.

Parameters
string$patternPattern.
bool$loadWhether loading directly or not.
callable$callbackCallback.
Returns
bool

Definition at line 196 of file Consistency.php.

197  {
198  $parts = explode('.', $pattern);
199 
200  if (!isset($parts[1])) {
201  return false;
202  }
203 
204  if (false !== strpos($pattern, '~')) {
205  $handle = null;
206 
207  foreach ($parts as &$part) {
208  if (null !== $handle && '*' !== $handle) {
209  $part = str_replace('~', $handle, $part);
210  }
211 
212  $handle = $part;
213  }
214  }
215 
216  if (false !== strpos($pattern, '*')) {
217  if ('Hoa' !== $parts[0] && 'Hoathis' !== $parts[0]) {
218  return false;
219  }
220 
221  $glob = new \AppendIterator();
222  $ds = preg_quote(DS);
223  $_pattern = '#' . $ds . $parts[0] . $ds . $parts[1] . $ds . '?$#i';
224 
225  foreach (resolve('hoa://Library/' . $parts[1], true, true) as $path) {
226  if (0 !== preg_match($_pattern, $path)) {
227  $glob->append(new \CallbackFilterIterator(
228  new \GlobIterator(
229  $path . DS . implode(DS, array_slice($parts, 2)) . '.php',
230  \GlobIterator::KEY_AS_PATHNAME
231  | \GlobIterator::CURRENT_AS_SELF
232  | \GlobIterator::SKIP_DOTS
233  ),
234  function ($current, $key) use ($path, $parts) {
235  $current->__hoa_pattern =
236  $parts[0] .
237  '.' .
238  $parts[1] .
239  '.' .
240  str_replace(
241  DS,
242  '.',
243  substr($key, strlen($path) + 1, -4)
244  );
245 
246  return true;
247  }
248  ));
249  }
250  }
251 
252  $out = true;
253 
254  foreach ($glob as $filesystem) {
255  $out &= $this->_import($filesystem->__hoa_pattern, $load, $callback);
256  }
257 
258  return (bool) $out;
259  }
260 
261  $classname = implode('\\', $parts);
262  $imported = array_key_exists($classname, static::$_class);
263 
264  if (false === $imported) {
265  static::$_class[$classname] = [
266  'path' => null,
267  'alias' => null
268  ];
269 
270  $count = count($parts);
271 
272  if ($parts[$count - 2] === $parts[$count - 1]) {
273  $alias = implode('\\', array_slice($parts, 0, -1));
274 
275  static::$_class[$classname]['alias'] = $alias;
276  static::$_class[$alias] = $classname;
277  $this->__class[$alias] = &static::$_class[$alias];
278  }
279  }
280 
281  $this->__class[$classname] = &static::$_class[$classname];
282 
283  if (true === $load &&
284  false === static::entityExists($classname, false)) {
285  spl_autoload_call($classname);
286 
287  if (null !== $callback &&
288  true === static::entityExists($classname, false)) {
289  $callback($classname);
290  }
291 
292  return true;
293  }
294 
295  if (null !== $callback) {
296  $callback($classname);
297  }
298 
299  return true;
300  }
_import($pattern, $load, $callback=null)

Here is the caller graph for this function:

static Hoa\Core\Consistency\Consistency::autoload (   $classname)
static

Autoloader.

Parameters
string$classnameClassname.
Returns
bool

Definition at line 308 of file Consistency.php.

309  {
310  if (false === strpos($classname, '\\')) {
311  return false;
312  }
313 
314  $classname = ltrim($classname, '\\');
315 
316  // Hard-preload.
317  if ('Hoa\Core' === substr($classname, 0, 8) &&
318  false !== ($pos = strpos($classname, '\\', 10)) &&
319  'Bin\\' !== substr($classname, 9, 4)) {
320  require static::$_class[substr($classname, 0, $pos)]['path'];
321 
322  return true;
323  }
324 
325  $head = substr($classname, 0, strpos($classname, '\\'));
326 
327  if (false === array_key_exists($classname, static::$_class)) {
328  $_classname = str_replace('\\', '.', $classname);
329  $out = from($head)->_import($_classname, true);
330 
331  if (false === static::entityExists($classname)) {
332  $out = from($head)->_import($_classname . '.~', true);
333  }
334 
335  return $out;
336  } elseif (is_string($original = static::$_class[$classname])) {
337  spl_autoload_call($original);
338 
339  return true;
340  }
341 
342  $roots = from($head)->getRoot();
343  $classpath = str_replace('\\', DS, $classname) . '.php';
344  $classpathExtended = str_replace(
345  '\\',
346  DS,
347  $classname . substr($classname, strrpos('\\', $classname, 1))
348  ) . '.php';
349 
350  $gotcha = false;
351 
352  foreach ($roots as $vendor => $_roots) {
353  foreach ($_roots as $root) {
354  if (true === file_exists($path = $root . $classpath) ||
355  true === file_exists($path = $root . $classpathExtended)) {
356  $gotcha = true;
357  require $path;
358  static::$_class[$classname]['path'] = $path;
359 
360  break 2;
361  }
362  }
363  }
364 
365  return $gotcha;
366  }

Here is the call graph for this function:

static Hoa\Core\Consistency\Consistency::dnew (   $classname,
Array  $arguments = [] 
)
static

Dynamic new, i.e. a native factory (import + load + instance).

Parameters
string$classnameClassname.
array$argumentsConstructor's arguments.
Returns
object
Exceptions

Definition at line 376 of file Consistency.php.

377  {
378  $classname = ltrim($classname, '\\');
379 
380  if (!class_exists($classname, false)) {
381  $head = substr($classname, 0, $pos = strpos($classname, '\\'));
382  $tail = str_replace('\\', '.', substr($classname, $pos + 1));
383  $from = from($head);
384 
385  foreach ([$tail, $tail . '.~'] as $_tail) {
386  foreach ($from->getFroms() as $_from) {
387  $break = false;
388  $from->_import(
389  $_from . '.' . $_tail,
390  true,
391  function ($_classname) use (&$break, &$classname) {
392  $classname = $_classname;
393  $break = true;
394  }
395  );
396 
397  if (true === $break) {
398  break 2;
399  }
400  }
401  }
402  }
403 
404  $class = new \ReflectionClass($classname);
405 
406  if (empty($arguments) || false === $class->hasMethod('__construct')) {
407  return $class->newInstance();
408  }
409 
410  return $class->newInstanceArgs($arguments);
411  }

Here is the call graph for this function:

static Hoa\Core\Consistency\Consistency::entityExists (   $entityName,
  $autoloader = false 
)
static

Check if an entity exists (class, interface, trait…).

Parameters
string$entityNameEntity name.
bool$autoloaderRun autoloader if necessary.
Returns
bool

Definition at line 503 of file Consistency.php.

504  {
505  return
506  class_exists($entityName, $autoloader) ||
507  interface_exists($entityName, false) ||
508  trait_exists($entityName, false);
509  }
static Hoa\Core\Consistency\Consistency::flexEntity (   $entityName)
static

Declare a flex entity (for nested library).

Parameters
string$entityNameEntity name.
Returns
bool

Definition at line 517 of file Consistency.php.

518  {
519  return class_alias(
520  $entityName,
521  static::getEntityShortestName($entityName)
522  );
523  }
Hoa\Core\Consistency\Consistency::foreachImport (   $pattern,
  $callback 
)

Iterate over each solution found by an import.

Parameters
string$patternPattern.
callable$callbackCallback (also disable cache).
Returns

Definition at line 179 of file Consistency.php.

180  {
181  foreach ($this->_from as $from) {
182  $this->_import($from . '.' . $pattern, false, $callback);
183  }
184 
185  return $this;
186  }
_import($pattern, $load, $callback=null)

Here is the call graph for this function:

static Hoa\Core\Consistency\Consistency::from (   $namespace)
static

Get the library family's singleton.

Parameters
string$namespaceLibrary family's name.
Returns

Definition at line 147 of file Consistency.php.

148  {
149  if (!isset(static::$_multiton[$namespace])) {
150  static::$_multiton[$namespace] = new static($namespace);
151  }
152 
153  return static::$_multiton[$namespace];
154  }

Here is the caller graph for this function:

static Hoa\Core\Consistency\Consistency::getAllImportedClasses ( )
static

Get imported classes from all library families.

Returns
array

Definition at line 473 of file Consistency.php.

474  {
475  return static::$_class;
476  }
static Hoa\Core\Consistency\Consistency::getEntityShortestName (   $entityName)
static

Get the shortest name for an entity.

Parameters
string$entityNameEntity name.
Returns
string

Definition at line 484 of file Consistency.php.

485  {
486  $parts = explode('\\', $entityName);
487  $count = count($parts);
488 
489  if ($parts[$count - 2] === $parts[$count - 1]) {
490  return implode('\\', array_slice($parts, 0, -1));
491  }
492 
493  return $entityName;
494  }
Hoa\Core\Consistency\Consistency::getFroms ( )

Get froms.

Returns
array

Definition at line 453 of file Consistency.php.

454  {
455  return $this->_from;
456  }
Hoa\Core\Consistency\Consistency::getImportedClasses ( )

Get imported classes from the current library family.

Returns
array

Definition at line 463 of file Consistency.php.

464  {
465  return $this->__class;
466  }
Hoa\Core\Consistency\Consistency::getRoot ( )

Get roots of the current library family.

Returns
array

Definition at line 443 of file Consistency.php.

444  {
445  return $this->_roots;
446  }
Hoa\Core\Consistency\Consistency::import (   $pattern,
  $load = false 
)

Import a class, an interface or a trait.

Parameters
string$patternPattern.
bool$loadWhether loading directly or not.
Returns

Definition at line 163 of file Consistency.php.

164  {
165  foreach ($this->_from as $from) {
166  $this->_import($from . '.' . $pattern, $load);
167  }
168 
169  return $this;
170  }
_import($pattern, $load, $callback=null)

Here is the call graph for this function:

static Hoa\Core\Consistency\Consistency::isIdentifier (   $id)
static

Whether an ID is a valid PHP identifier.

Parameters
string$idID.
Returns
bool

Definition at line 633 of file Consistency.php.

634  {
635  return 0 !== preg_match(
636  '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#',
637  $id
638  );
639  }
static Hoa\Core\Consistency\Consistency::isKeyword (   $word)
static

Whether a word is reserved or not.

Parameters
string$wordWord.
Returns
void

Definition at line 531 of file Consistency.php.

532  {
533  static $_list = [
534  // PHP keywords.
535  '__halt_compiler',
536  'abstract',
537  'and',
538  'array',
539  'as',
540  'bool',
541  'break',
542  'callable',
543  'case',
544  'catch',
545  'class',
546  'clone',
547  'const',
548  'continue',
549  'declare',
550  'default',
551  'die',
552  'do',
553  'echo',
554  'else',
555  'elseif',
556  'empty',
557  'enddeclare',
558  'endfor',
559  'endforeach',
560  'endif',
561  'endswitch',
562  'endwhile',
563  'eval',
564  'exit',
565  'extends',
566  'false',
567  'final',
568  'float',
569  'for',
570  'foreach',
571  'function',
572  'global',
573  'goto',
574  'if',
575  'implements',
576  'include',
577  'include_once',
578  'instanceof',
579  'insteadof',
580  'int',
581  'interface',
582  'isset',
583  'list',
584  'mixed',
585  'namespace',
586  'new',
587  'null',
588  'numeric',
589  'object',
590  'or',
591  'print',
592  'private',
593  'protected',
594  'public',
595  'require',
596  'require_once',
597  'resource',
598  'return',
599  'static',
600  'string',
601  'switch',
602  'throw',
603  'trait',
604  'true',
605  'try',
606  'unset',
607  'use',
608  'var',
609  'while',
610  'xor',
611  'yield',
612 
613  // Compile-time constants.
614  '__class__',
615  '__dir__',
616  '__file__',
617  '__function__',
618  '__line__',
619  '__method__',
620  '__namespace__',
621  '__trait__'
622  ];
623 
624  return in_array(strtolower($word), $_list);
625  }
Hoa\Core\Consistency\Consistency::setRoot (   $root,
  $from = null 
)

Set the root of the current library family.

Parameters
bool$rootRoot.
string$fromLibrary family's name (if null, first family will be choosen).
Returns

Definition at line 421 of file Consistency.php.

422  {
423  if (null === $from) {
424  $from = $this->_from[0];
425  }
426 
427  if (!isset($this->_roots[$from])) {
428  $this->_roots[$from] = [];
429  }
430 
431  foreach (explode(';', $root) as $r) {
432  $this->_roots[$from][] = rtrim($r, '/\\') . DS;
433  }
434 
435  return $this;
436  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Core\Consistency\Consistency::$__class = []
protected

Definition at line 114 of file Consistency.php.

Hoa\Core\Consistency\Consistency::$_cache = []
staticprotected

Definition at line 85 of file Consistency.php.

Hoa\Core\Consistency\Consistency::$_class
staticprotected
Initial value:
= [
'Hoa\Core\Event' => [
'path' => PATH_EVENT

Definition at line 92 of file Consistency.php.

Hoa\Core\Consistency\Consistency::$_from = null
protected

Definition at line 71 of file Consistency.php.

Hoa\Core\Consistency\Consistency::$_multiton = []
staticprivate

Definition at line 64 of file Consistency.php.

Hoa\Core\Consistency\Consistency::$_roots = []
protected

Definition at line 78 of file Consistency.php.


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