Overview

Namespaces

  • Deimos
    • Builder

Classes

  • Deimos\Builder\Builder
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\Builder;
  4: 
  5: class Builder
  6: {
  7: 
  8:     /**
  9:      * @var array
 10:      */
 11:     protected $instances = [];
 12: 
 13:     /**
 14:      * @param $name
 15:      *
 16:      * @return string
 17:      */
 18:     protected function methodName($name)
 19:     {
 20:         return 'build' . ucfirst($name);
 21:     }
 22: 
 23:     /**
 24:      * @param string $name
 25:      *
 26:      * @return bool
 27:      */
 28:     protected function hasInstance($name)
 29:     {
 30:         return isset($this->instances[$name]);
 31:     }
 32: 
 33:     /**
 34:      * @param $method
 35:      *
 36:      * @return mixed
 37:      */
 38:     private function init($method)
 39:     {
 40:         if (is_string($method) && method_exists($this, $method))
 41:         {
 42:             return $this->$method();
 43:         }
 44: 
 45:         return $method();
 46:     }
 47: 
 48:     /**
 49:      * init method
 50:      *
 51:      * @param string          $name
 52:      * @param string|callable $method
 53:      *
 54:      * @return mixed
 55:      */
 56:     private function objectBuild($name, $method)
 57:     {
 58:         if (!$this->hasInstance($name))
 59:         {
 60:             $this->instances[$name] = $this->init($method);
 61:         }
 62: 
 63:         return $this->instances[$name];
 64:     }
 65: 
 66:     /**
 67:      * @param $name
 68:      *
 69:      * @return mixed
 70:      */
 71:     protected function instance($name)
 72:     {
 73:         $methodName = $this->methodName($name);
 74: 
 75:         return $this->objectBuild($name, $methodName);
 76:     }
 77: 
 78:     /**
 79:      * @param callable $callable
 80:      *
 81:      * @return string
 82:      */
 83:     private function hash(callable $callable)
 84:     {
 85:         $reflection = new \ReflectionFunction($callable);
 86: 
 87:         return crc32($reflection);
 88:     }
 89: 
 90:     /**
 91:      * @param callable $callable
 92:      * @param string   $udId
 93:      *
 94:      * @return mixed
 95:      */
 96:     protected function once(callable $callable, $udId = null)
 97:     {
 98:         if (!$udId)
 99:         {
100:             $udId = $this->hash($callable);
101:         }
102: 
103:         return $this->objectBuild($udId, $callable);
104:     }
105: 
106: }
API documentation generated by ApiGen