/src/Propel/Generator/Model/Behavior.php

https://github.com/hhamon/Propel2 · PHP · 457 lines · 166 code · 48 blank · 243 comment · 7 complexity · 1c370482ef81392fb555daeea57c0144 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace Propel\Generator\Model;
  10. use Propel\Generator\Builder\Util\PropelTemplate;
  11. use Propel\Generator\Exception\LogicException;
  12. /**
  13. * Information about behaviors of a table.
  14. *
  15. * @author François Zaninotto
  16. * @author Hugo Hamon <webmaster@apprendre-php.com> (Propel)
  17. */
  18. class Behavior extends MappingModel
  19. {
  20. /**
  21. * The table object on which the behavior is applied.
  22. *
  23. * @var Table
  24. */
  25. protected $table;
  26. /**
  27. * The database object.
  28. *
  29. * @var Database
  30. */
  31. protected $database;
  32. /**
  33. * The behavior id.
  34. *
  35. * @var string
  36. */
  37. protected $id;
  38. /**
  39. * The behavior name.
  40. *
  41. * @var string
  42. */
  43. protected $name;
  44. /**
  45. * A collection of parameters.
  46. *
  47. * @var array
  48. */
  49. protected $parameters = [ ];
  50. /**
  51. * Wether or not the table has been
  52. * modified by the behavior.
  53. *
  54. * @var boolean
  55. */
  56. protected $isTableModified = false;
  57. /**
  58. * The absolute path to the directory
  59. * that contains the behavior's templates
  60. * files.
  61. *
  62. * @var string
  63. */
  64. protected $dirname;
  65. /**
  66. * A collection of additional builders.
  67. *
  68. * @var array
  69. */
  70. protected $additionalBuilders = [];
  71. /**
  72. * The order in which the behavior must
  73. * be applied.
  74. *
  75. * @var int
  76. */
  77. protected $tableModificationOrder = 50;
  78. /**
  79. * Sets the name of the Behavior
  80. *
  81. * @param string $name the name of the behavior
  82. */
  83. public function setName($name)
  84. {
  85. $this->name = $name;
  86. if ($this->id === null) {
  87. $this->id = $name;
  88. }
  89. }
  90. /**
  91. * Sets the id of the Behavior
  92. *
  93. * @param string $id The id of the behavior
  94. */
  95. public function setId($id)
  96. {
  97. $this->id = $id;
  98. }
  99. /**
  100. * Indicates whether the behavior can be applied several times on the same
  101. * table or not.
  102. *
  103. * @return bool
  104. */
  105. public function allowMultiple()
  106. {
  107. return false;
  108. }
  109. /**
  110. * Returns the id of the Behavior
  111. *
  112. * @return string
  113. */
  114. public function getId()
  115. {
  116. return $this->id;
  117. }
  118. /**
  119. * Returns the name of the Behavior
  120. *
  121. * @return string
  122. */
  123. public function getName()
  124. {
  125. return $this->name;
  126. }
  127. /**
  128. * Sets the table this behavior is applied to
  129. *
  130. * @param Table $table
  131. */
  132. public function setTable(Table $table)
  133. {
  134. $this->table = $table;
  135. }
  136. /**
  137. * Returns the table this behavior is applied to
  138. *
  139. * @return Table
  140. */
  141. public function getTable()
  142. {
  143. return $this->table;
  144. }
  145. /**
  146. * Sets the database this behavior is applied to
  147. *
  148. * @param Database $database
  149. */
  150. public function setDatabase(Database $database)
  151. {
  152. $this->database = $database;
  153. }
  154. /**
  155. * Returns the table this behavior is applied to if behavior is applied to
  156. * a database element.
  157. *
  158. * @return Database
  159. */
  160. public function getDatabase()
  161. {
  162. return $this->database;
  163. }
  164. /**
  165. * Adds a single parameter.
  166. *
  167. * Expects an associative array looking like
  168. * [ 'name' => 'foo', 'value' => bar ]
  169. *
  170. * @param array $parameter
  171. */
  172. public function addParameter(array $parameter)
  173. {
  174. $parameter = array_change_key_case($parameter, CASE_LOWER);
  175. $this->parameters[$parameter['name']] = $parameter['value'];
  176. }
  177. /**
  178. * Overrides the behavior parameters.
  179. *
  180. * Expects an associative array looking like [ 'foo' => 'bar' ].
  181. *
  182. * @param array $parameters
  183. */
  184. public function setParameters(array $parameters)
  185. {
  186. $this->parameters = $parameters;
  187. }
  188. /**
  189. * Returns the associative array of parameters.
  190. *
  191. * @return array
  192. */
  193. public function getParameters()
  194. {
  195. return $this->parameters;
  196. }
  197. /**
  198. * Returns a single parameter by its name.
  199. *
  200. * @param string $name
  201. * @return array
  202. */
  203. public function getParameter($name)
  204. {
  205. return $this->parameters[$name];
  206. }
  207. /**
  208. * Defines when this behavior must execute its modifyTable() method
  209. * relative to other behaviors. The bigger the value is, the later the
  210. * behavior is executed.
  211. *
  212. * Default is 50.
  213. *
  214. * @param integer $tableModificationOrder
  215. */
  216. public function setTableModificationOrder($tableModificationOrder)
  217. {
  218. $this->tableModificationOrder = (int) $tableModificationOrder;
  219. }
  220. /**
  221. * Returns when this behavior must execute its modifyTable() method relative
  222. * to other behaviors. The bigger the value is, the later the behavior is
  223. * executed.
  224. *
  225. * Default is 50.
  226. *
  227. * @return integer
  228. */
  229. public function getTableModificationOrder()
  230. {
  231. return $this->tableModificationOrder;
  232. }
  233. /**
  234. * This method is automatically called on database behaviors when the
  235. * database model is finished.
  236. *
  237. * Propagates the behavior to the tables of the database and override this
  238. * method to have a database behavior do something special.
  239. */
  240. public function modifyDatabase()
  241. {
  242. foreach ($this->getTables() as $table) {
  243. if ($table->hasBehavior($this->getId())) {
  244. // don't add the same behavior twice
  245. continue;
  246. }
  247. $behavior = clone $this;
  248. $table->addBehavior($behavior);
  249. }
  250. }
  251. /**
  252. * Returns the list of all tables in the same database.
  253. *
  254. * @return Table[] A collection of Table instance
  255. */
  256. protected function getTables()
  257. {
  258. return $this->database->getTables();
  259. }
  260. /**
  261. * This method is automatically called on table behaviors when the database
  262. * model is finished. It also override it to add columns to the current
  263. * table.
  264. */
  265. public function modifyTable()
  266. {
  267. }
  268. /**
  269. * Sets whether or not the table has been modified.
  270. *
  271. * @param boolean $modified
  272. */
  273. public function setTableModified($modified)
  274. {
  275. $this->isTableModified = $modified;
  276. }
  277. /**
  278. * Returns whether or not the table has been modified.
  279. *
  280. * @return boolean
  281. */
  282. public function isTableModified()
  283. {
  284. return $this->isTableModified;
  285. }
  286. /**
  287. * Use Propel simple templating system to render a PHP file using variables
  288. * passed as arguments. The template file name is relative to the behavior's
  289. * directory name.
  290. *
  291. * @param string $filename
  292. * @param array $vars
  293. * @param string $templateDir
  294. * @return string
  295. */
  296. public function renderTemplate($filename, $vars = [], $templateDir = '/templates/')
  297. {
  298. $filePath = $this->getDirname() . $templateDir . $filename;
  299. if (!file_exists($filePath)) {
  300. // try with '.php' at the end
  301. $filePath = $filePath . '.php';
  302. if (!file_exists($filePath)) {
  303. throw new \InvalidArgumentException(sprintf('Template "%s" not found in "%s" directory',
  304. $filename,
  305. $this->getDirname() . $templateDir
  306. ));
  307. }
  308. }
  309. $template = new PropelTemplate();
  310. $template->setTemplateFile($filePath);
  311. $vars = array_merge($vars, [ 'behavior' => $this ]);
  312. return $template->render($vars);
  313. }
  314. /**
  315. * Returns the current absolute directory name of this behavior. It also
  316. * works for descendants.
  317. *
  318. * @return string
  319. */
  320. protected function getDirname()
  321. {
  322. if (null === $this->dirname) {
  323. $r = new \ReflectionObject($this);
  324. $this->dirname = dirname($r->getFileName());
  325. }
  326. return $this->dirname;
  327. }
  328. /**
  329. * Returns a column object using a name stored in the behavior parameters.
  330. * Useful for table behaviors.
  331. *
  332. * @param string $name
  333. * @return Column
  334. */
  335. public function getColumnForParameter($name)
  336. {
  337. return $this->table->getColumn($this->getParameter($name));
  338. }
  339. protected function setupObject()
  340. {
  341. $this->setName($this->getAttribute('name'));
  342. if (!$this->allowMultiple() && $id = $this->getAttribute('id')) {
  343. throw new LogicException(sprintf('Defining an ID (%s) on a behavior which does not allow multiple instances makes no sense', $id));
  344. }
  345. $this->id = $this->getAttribute('id', $this->name);
  346. }
  347. /**
  348. * Returns the table modifier object.
  349. *
  350. * The current object is returned by default.
  351. *
  352. * @return $this|Behavior
  353. */
  354. public function getTableModifier()
  355. {
  356. return $this;
  357. }
  358. /**
  359. * Returns the object builder modifier object.
  360. *
  361. * The current object is returned by default.
  362. *
  363. * @return $this|Behavior
  364. */
  365. public function getObjectBuilderModifier()
  366. {
  367. return $this;
  368. }
  369. /**
  370. * Returns the query builder modifier object.
  371. *
  372. * The current object is returned by default.
  373. *
  374. * @return $this|Behavior
  375. */
  376. public function getQueryBuilderModifier()
  377. {
  378. return $this;
  379. }
  380. /**
  381. * Returns the table map builder modifier object.
  382. *
  383. * The current object is returned by default.
  384. *
  385. * @return $this|Behavior
  386. */
  387. public function getTableMapBuilderModifier()
  388. {
  389. return $this;
  390. }
  391. /**
  392. * Returns whether or not this behavior has additional builders.
  393. *
  394. * @return boolean
  395. */
  396. public function hasAdditionalBuilders()
  397. {
  398. return !empty($this->additionalBuilders);
  399. }
  400. /**
  401. * Returns the list of additional builder objects.
  402. *
  403. * @return array
  404. */
  405. public function getAdditionalBuilders()
  406. {
  407. return $this->additionalBuilders;
  408. }
  409. }