FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Entity.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 /**
6  * Entity manager component.
7  */
8 class Entity extends Component{
9 
10  public $classNames = []; //!< Classnames for item types (key = type, value = class name).
11  public $defaultNamespace = ''; //!< Namespace to look for an item class (ucfirst(id)) when not found in classNames.
12 
13  protected $_entities = [];
14 
15  /**
16  * Retrieve an entity, or create a new if nonexistence.
17  * @param string $type Entity type.
18  * @param string $id,... Entity ID, and other parameters - passed when creating a new entity.
19  * @return object Entity.
20  */
21  public function item($type,$id){
22  \Rsi\Record::add($this->_entities,$type,[]);
23  if($id && array_key_exists($id,$this->_entities[$type])) return $this->_entities[$type][$id];
24  $this->component('log')->debug("Creating entity $type:$id",__FILE__,__LINE__);
25  $reflect = new \ReflectionClass($this->classNames[$type] ?? $this->defaultNamespace . '\\' . ucfirst($type));
26  $params = func_get_args();
27  $params[0] = $this->_fred;
28  return $this->_entities[$type][$id] = $reflect->newInstanceArgs($params);
29  }
30  /**
31  * Check if an entity already exists.
32  * @param string $type Entity type.
33  * @param string $id Entity ID.
34  * @return bool True if the entity exists.
35  */
36  public function has($type,$id){
37  return array_key_exists($type,$this->_entities) && array_key_exists($id,$this->_entities[$type]);
38  }
39  /**
40  * Flush entities.
41  * @param string $type Entity type (empty to flush al entities).
42  * @param string $id Entity ID (empty to flush all entities of this type).
43  */
44  public function flush($type = null,$id = null){
45  $this->component('log')->debug(__CLASS__ . "::flush($type" . ($id ? ",$id" : '') . ')',__FILE__,__LINE__);
46  if($id) unset($this->_entities[$type][$id]);
47  elseif($type) unset($this->_entities[$type]);
48  else $this->_entities = [];
49  }
50 
51  public function __call($func_name,$params){
52  return call_user_func_array([$this,'item'],array_merge([$func_name],$params));
53  }
54 
55  public function __invoke($type,$id){
56  return $this->item($type,$id);
57  }
58 
59 }
$defaultNamespace
Namespace to look for an item class (ucfirst(id)) when not found in classNames.
Definition: Entity.php:11
item($type, $id)
Retrieve an entity, or create a new if nonexistence.
Definition: Entity.php:21
flush($type=null, $id=null)
Flush entities.
Definition: Entity.php:44
Entity manager component.
Definition: Entity.php:8
Basic component class.
Definition: Component.php:8
__invoke($type, $id)
Definition: Entity.php:55
__call($func_name, $params)
Definition: Entity.php:51
has($type, $id)
Check if an entity already exists.
Definition: Entity.php:36
$classNames
Classnames for item types (key = type, value = class name).
Definition: Entity.php:10
component($name)
Get a component (local or default).
Definition: Component.php:80