PageRenderTime 83ms CodeModel.GetById 35ms RepoModel.GetById 3ms app.codeStats 1ms

/library/Adapto/MetaEntity.php

http://github.com/egeniq/adapto
PHP | 197 lines | 71 code | 24 blank | 102 comment | 16 complexity | 806579105650bf5077ebbcf238875248 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Adapto Toolkit.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package adapto
  9. *
  10. * @copyright (c) 2004-2007 Peter C. Verhage
  11. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  12. *
  13. */
  14. /**
  15. * The ATK Meta Entity class.
  16. *
  17. * Makes it possible to create entitys in 1 line of code
  18. * using metadata from the database.
  19. *
  20. * @author petercv
  21. *
  22. * @package adapto
  23. */
  24. class Adapto_MetaEntity extends Adapto_Entity
  25. {
  26. /**
  27. * Meta options.
  28. *
  29. * @var array
  30. */
  31. private $m_metaOptions;
  32. /**
  33. * Constructor.
  34. *
  35. * This constructor accepts a variety of parameters in different order.
  36. * To make this possible (and for supporting passing parameters by reference)
  37. * the constructor accepts an array which may contain the following fields:
  38. *
  39. * - type entity type
  40. * - table table name
  41. * - sequence sequence name to use (if not specified, it'll use autoincrement for mysql)
  42. * - db/database database name or instance
  43. * - policy meta policy, the meta policy to use ((sub-)class atkMetaPolicy instance)
  44. * - grammar meta grammar, the meta grammar to use ((sub-)class atkMetaGrammar instance)
  45. * - compiler meta compiler, the meta compiler to use ((sub-)class atkMetaCompiler instance)
  46. * - handler meta handler, handler which needs to be called instead of the default meta method
  47. * - flags entity flags
  48. * - descriptor descriptor template for this entity
  49. * - order (default) order to sort fields
  50. * - index create indexed navigation on a attribute/fieldname
  51. * - filter filter
  52. * - securityAlias security alias for this entity
  53. * - securityMap security map for this entity (will be added to the existing security map!)
  54. * - cacheable control whatever this metan entity is cacheable (by default a metaentity is
  55. * cachable if the meta method is defined static or if there is no meta method
  56. * defined)
  57. *
  58. * All of these variables can also be specified by creating a class variable with
  59. * the same name. If you do so for flags, and have to use multiple flags, use
  60. * an array of flags.
  61. *
  62. * @param array $options metan entity options
  63. *
  64. * @return Adapto_MetaEntity
  65. */
  66. public function __construct($options = array())
  67. {
  68. $this->m_metaOptions = $options;
  69. $type = $this->getMetaOption('type', strtolower(get_class($this)));
  70. parent::__construct($type);
  71. if (!$this->_constructFromCache())
  72. $this->_constructFromPolicy();
  73. $this->postMeta();
  74. }
  75. /**
  76. * Old-style constructor for backwards-compatibility.
  77. *
  78. * @return Adapto_MetaEntity
  79. */
  80. protected function Adapto_MetaEntity()
  81. {
  82. $args = func_get_args();
  83. call_user_func_array(array($this, '__construct'), $args);
  84. }
  85. /**
  86. * Returns the value for the meta option with the given name.
  87. *
  88. * @param string $name meta option name
  89. * @param mixed $default fallback value
  90. *
  91. * @return mixed meta option value
  92. */
  93. public function getMetaOption($name, $default = null)
  94. {
  95. if (isset($this->$name)) {
  96. return $this->$name;
  97. } else if (isset($this->m_metaOptions[$name])) {
  98. return $this->m_metaOptions[$name];
  99. } else {
  100. return $default;
  101. }
  102. }
  103. /**
  104. * Is the entity structure cacheable?
  105. *
  106. * @return boolean cacheable?
  107. */
  108. public function isCacheable()
  109. {
  110. $cacheable = $this->getMetaOption('cacheable');
  111. if ($cacheable !== null)
  112. return $cacheable;
  113. if (!Adapto_Config::getGlobal('meta_caching', true))
  114. return false;
  115. if (strtolower(get_class($this)) == 'atkmetaentity')
  116. return false;
  117. if (!method_exists($this, 'meta'))
  118. return true;
  119. $method = new ReflectionMethod(get_class($this), 'meta');
  120. return $method->isStatic();
  121. }
  122. /**
  123. * Constructs the metan entity from the cache if this entity is cachable and the entity
  124. * structure has been cached.
  125. *
  126. * @return boolean is the entity constructed from the cache?
  127. */
  128. private function _constructFromCache()
  129. {
  130. if (!$this->isCacheable())
  131. return false;
  132. $file = new Adapto_TmpFile("meta/" . $this->getModule() . "/" . $this->getType() . ".php");
  133. $module = getModule($this->getModule());
  134. if ($file->exists()
  135. && ($module == null || !file_exists($module->getEntityFile(parent::__constructType()))
  136. || filemtime($file->getPath()) > filemtime($module->getEntityFile(parent::__constructType())))) {
  137. $entity = $this;
  138. include($file->getPath());
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * Create policy.
  145. *
  146. * @return atkMetaPolicy policy
  147. */
  148. protected function _createPolicy()
  149. {
  150. $policy = $this->getMetaOption('policy');
  151. return atkMetaPolicy::create($this, $policy);
  152. }
  153. /**
  154. * Constructs the metan entity using the meta policy.
  155. */
  156. protected function _constructFromPolicy()
  157. {
  158. $policy = $this->_createPolicy();
  159. $policy->apply();
  160. }
  161. /**
  162. * Post meta.
  163. *
  164. * This method is called just after the entity is constructed from the cache
  165. * or using the meta policy and allows you to do some entity initialization
  166. * which cannot be done by the meta policy.
  167. */
  168. public function postMeta()
  169. {
  170. // do nothing
  171. }
  172. }