PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/plugins/doctrine/lib/Doctrine/Record/Generator.php

https://github.com/cawago/ci_campusync_auth
PHP | 374 lines | 174 code | 56 blank | 144 comment | 18 complexity | 33bfe6961bf5d882a14a0a9c867cece9 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Record_Generator
  23. *
  24. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  25. * @package Doctrine
  26. * @subpackage Plugin
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @version $Revision$
  29. * @link www.phpdoctrine.org
  30. * @since 1.0
  31. */
  32. abstract class Doctrine_Record_Generator extends Doctrine_Record_Abstract
  33. {
  34. /**
  35. * _options
  36. *
  37. * @var array $_options an array of plugin specific options
  38. */
  39. protected $_options = array('generateFiles' => false,
  40. 'generatePath' => false,
  41. 'builderOptions' => array(),
  42. 'identifier' => false,
  43. 'table' => false,
  44. 'pluginTable' => false,
  45. 'children' => array());
  46. /**
  47. * _initialized
  48. *
  49. * @var bool $_initialized
  50. */
  51. protected $_initialized = false;
  52. /**
  53. * __get
  54. * an alias for getOption
  55. *
  56. * @param string $option
  57. */
  58. public function __get($option)
  59. {
  60. if (isset($this->_options[$option])) {
  61. return $this->_options[$option];
  62. }
  63. return null;
  64. }
  65. /**
  66. * __isset
  67. *
  68. * @param string $option
  69. */
  70. public function __isset($option)
  71. {
  72. return isset($this->_options[$option]);
  73. }
  74. /**
  75. * returns the value of an option
  76. *
  77. * @param $option the name of the option to retrieve
  78. * @return mixed the value of the option
  79. */
  80. public function getOption($name)
  81. {
  82. if ( ! isset($this->_options[$name])) {
  83. throw new Doctrine_Exception('Unknown option ' . $name);
  84. }
  85. return $this->_options[$name];
  86. }
  87. /**
  88. * sets given value to an option
  89. *
  90. * @param $option the name of the option to be changed
  91. * @param $value the value of the option
  92. * @return Doctrine_Plugin this object
  93. */
  94. public function setOption($name, $value)
  95. {
  96. $this->_options[$name] = $value;
  97. return $this;
  98. }
  99. /**
  100. * addChild
  101. *
  102. * Add child record generator
  103. *
  104. * @param Doctrine_Record_Generator $generator
  105. * @return void
  106. */
  107. public function addChild($generator)
  108. {
  109. $this->_options['children'][] = $generator;
  110. }
  111. /**
  112. * getOptions
  113. *
  114. * returns all options and their associated values
  115. *
  116. * @return array all options as an associative array
  117. */
  118. public function getOptions()
  119. {
  120. return $this->_options;
  121. }
  122. /**
  123. * initialize
  124. *
  125. * Initialize the plugin. Call in Doctrine_Template setTableDefinition() in order to initiate a generator in a template
  126. * SEE: Doctrine_Template_I18n for an example
  127. *
  128. * @param Doctrine_Table $table
  129. * @return void
  130. */
  131. public function initialize(Doctrine_Table $table)
  132. {
  133. if ($this->_initialized) {
  134. return false;
  135. }
  136. $this->_initialized = true;
  137. $this->initOptions();
  138. $table->addGenerator($this, get_class($this));
  139. $this->_options['table'] = $table;
  140. $this->_options['className'] = str_replace('%CLASS%',
  141. $this->_options['table']->getComponentName(),
  142. $this->_options['className']);
  143. // check that class doesn't exist (otherwise we cannot create it)
  144. if ($this->_options['generateFiles'] === false && class_exists($this->_options['className'], false)) {
  145. return false;
  146. }
  147. $this->buildTable();
  148. $fk = $this->buildForeignKeys($this->_options['table']);
  149. $this->_table->setColumns($fk);
  150. $this->buildRelation();
  151. $this->setTableDefinition();
  152. $this->setUp();
  153. $definition = array();
  154. $definition['columns'] = $this->_table->getColumns();
  155. $definition['tableName'] = $this->_table->getTableName();
  156. $definition['actAs'] = $this->_table->getTemplates();
  157. $this->generateClass($definition);
  158. $this->buildChildDefinitions();
  159. $this->_table->initIdentifier();
  160. }
  161. public function buildTable()
  162. {
  163. // Bind model
  164. $conn = $this->_options['table']->getConnection();
  165. $conn->getManager()->bindComponent($this->_options['className'], $conn->getName());
  166. // Create table
  167. $this->_table = new Doctrine_Table($this->_options['className'], $conn);
  168. // If custom table name set then lets use it
  169. if (isset($this->_options['tableName']) && $this->_options['tableName']) {
  170. $this->_table->setTableName($this->_options['tableName']);
  171. }
  172. // Maintain some options from the parent table
  173. $options = $this->_options['table']->getOptions();
  174. $newOptions = array();
  175. $maintain = array('type', 'collate', 'charset'); // This list may need updating
  176. foreach ($maintain as $key) {
  177. if (isset($options[$key])) {
  178. $newOptions[$key] = $options[$key];
  179. }
  180. }
  181. $this->_table->setOptions($newOptions);
  182. $conn->addTable($this->_table);
  183. }
  184. /**
  185. * empty template method for providing the concrete plugins the ability
  186. * to initialize options before the actual definition is being built
  187. *
  188. * @return void
  189. */
  190. public function initOptions()
  191. {
  192. }
  193. /**
  194. * buildChildDefinitions
  195. *
  196. * @return void
  197. */
  198. public function buildChildDefinitions()
  199. {
  200. if ( ! isset($this->_options['children'])) {
  201. throw new Doctrine_Record_Exception("Unknown option 'children'.");
  202. }
  203. foreach ($this->_options['children'] as $child) {
  204. if ($child instanceof Doctrine_Template) {
  205. if ($child->getPlugin() !== null) {
  206. $this->_table->addGenerator($child->getPlugin(), get_class($child->getPlugin()));
  207. }
  208. $this->_table->addTemplate(get_class($child), $child);
  209. $child->setInvoker($this);
  210. $child->setTable($this->_table);
  211. $child->setTableDefinition();
  212. $child->setUp();
  213. } else {
  214. $this->_table->addGenerator($child, get_class($child));
  215. $child->initialize($this->_table);
  216. }
  217. }
  218. }
  219. /**
  220. * buildForeignKeys
  221. *
  222. * generates foreign keys for the plugin table based on the owner table
  223. *
  224. * the foreign keys generated by this method can be used for
  225. * setting the relations between the owner and the plugin classes
  226. *
  227. * @param Doctrine_Table $table the table object that owns the plugin
  228. * @return array an array of foreign key definitions
  229. */
  230. public function buildForeignKeys(Doctrine_Table $table)
  231. {
  232. $fk = array();
  233. foreach ((array) $table->getIdentifier() as $column) {
  234. $def = $table->getDefinitionOf($column);
  235. unset($def['autoincrement']);
  236. unset($def['sequence']);
  237. unset($def['primary']);
  238. $col = $column;
  239. $def['primary'] = true;
  240. $fk[$col] = $def;
  241. }
  242. return $fk;
  243. }
  244. /**
  245. * buildLocalRelation
  246. *
  247. * @return void
  248. */
  249. public function buildLocalRelation()
  250. {
  251. $options = array('local' => $this->_options['table']->getIdentifier(),
  252. 'foreign' => $this->_options['table']->getIdentifier(),
  253. 'type' => Doctrine_Relation::ONE,
  254. 'owningSide' => true);
  255. $options['onDelete'] = 'CASCADE';
  256. $options['onUpdate'] = 'CASCADE';
  257. $this->_table->getRelationParser()->bind($this->_options['table']->getComponentName(), $options);
  258. }
  259. /**
  260. * buildForeignRelation
  261. *
  262. * @param string $alias Alias of the foreign relation
  263. * @return void
  264. */
  265. public function buildForeignRelation($alias = null)
  266. {
  267. $options = array('local' => $this->_options['table']->getIdentifier(),
  268. 'foreign' => $this->_options['table']->getIdentifier(),
  269. 'type' => Doctrine_Relation::MANY);
  270. $aliasStr = '';
  271. if ($alias !== null) {
  272. $aliasStr = ' as ' . $alias;
  273. }
  274. $this->_options['table']->getRelationParser()->bind($this->_table->getComponentName() . $aliasStr,
  275. $options);
  276. }
  277. /**
  278. * buildRelation
  279. *
  280. * this method can be used for generating the relation from the plugin
  281. * table to the owner table. By default buildForeignRelation() and buildLocalRelation() are called
  282. * Those methods can be overridden or this entire method can be overridden
  283. *
  284. * @return void
  285. */
  286. public function buildRelation()
  287. {
  288. $this->buildForeignRelation();
  289. $this->buildLocalRelation();
  290. }
  291. /**
  292. * generateClass
  293. *
  294. * generates the class definition for plugin class
  295. *
  296. * @param array $definition Definition array defining columns, relations and options
  297. * for the model
  298. * @return void
  299. */
  300. public function generateClass(array $definition = array())
  301. {
  302. $definition['className'] = $this->_options['className'];
  303. $builder = new Doctrine_Import_Builder();
  304. $builderOptions = isset($this->_options['builderOptions']) ? (array) $this->_options['builderOptions']:array();
  305. $builder->setOptions($builderOptions);
  306. if ($this->_options['generateFiles']) {
  307. if (isset($this->_options['generatePath']) && $this->_options['generatePath']) {
  308. $builder->setTargetPath($this->_options['generatePath']);
  309. $builder->buildRecord($definition);
  310. } else {
  311. throw new Doctrine_Record_Exception('If you wish to generate files then you must specify the path to generate the files in.');
  312. }
  313. } else {
  314. $def = $builder->buildDefinition($definition);
  315. eval($def);
  316. }
  317. }
  318. }