PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/generator/sfThemeGenerator.class.php

https://github.com/pborreli/sfThemeGeneratorPlugin
PHP | 331 lines | 284 code | 33 blank | 14 comment | 18 complexity | 0efc934c396a7bd2dbbcd2a104c3f316 MD5 | raw file
  1. <?php
  2. class sfThemeGenerator extends sfDoctrineGenerator
  3. {
  4. protected
  5. $options = array(),
  6. $themeDir = null,
  7. $availableConfigs = array();
  8. public function getClassLabel()
  9. {
  10. return $this->get('class_label', $this->getModelClass());
  11. }
  12. public function getFormClass()
  13. {
  14. return $this->get('form_class', $this->getModelClass().'Form');
  15. }
  16. public function getThemeDirectory()
  17. {
  18. return $this->themeDir;
  19. }
  20. public function hasI18nEnabled()
  21. {
  22. return $this->get('i18n', false);
  23. }
  24. public function get($config, $default = null)
  25. {
  26. if (isset($this->options[$config])) {
  27. return $this->options[$config];
  28. }
  29. return $default;
  30. }
  31. // Render text in HTML
  32. public function renderHtmlText($text)
  33. {
  34. if ($this->hasI18nEnabled()) {
  35. $text = $this->renderPhpText($text);
  36. return sprintf('<?php echo %s ?>', $text);
  37. }
  38. return $this->parser->renderHtmlText($text);
  39. }
  40. // Render text in a PHP block
  41. public function renderPhpText($text)
  42. {
  43. $text = $this->parser->renderPhpText($text);
  44. if ($this->hasI18nEnabled()) {
  45. $text = sprintf('__(%s, array(), \''. $this->getI18nCatalogue().'\')', $text);
  46. }
  47. return $text;
  48. }
  49. // Render text that will appear in a php array
  50. public function renderPhpArrayText($text)
  51. {
  52. if ($this->hasI18nEnabled()) {
  53. $text = $this->parser->wrapPhpToken(sprintf('__(\'%s\', array(), \''. $this->getI18nCatalogue().'\')', $text));
  54. }
  55. return $text;
  56. }
  57. public function startCredentialCondition($params = array())
  58. {
  59. if (isset($params['credentials']))
  60. {
  61. return sprintf("[?php if (\$sf_user->hasCredential(%s)): ?]
  62. ", $this->renderCredentials($params['credentials']));
  63. }
  64. }
  65. public function endCredentialCondition($params)
  66. {
  67. if (isset($params['credentials']))
  68. {
  69. return "[?php endif; ?]\n";
  70. }
  71. }
  72. public function addCredentialCondition($content, $params = array())
  73. {
  74. if (isset($params['credentials']))
  75. {
  76. $content = sprintf("
  77. [?php if (\$sf_user->hasCredential(%s)): ?]
  78. %s
  79. [?php endif; ?]
  80. ", $this->renderCredentials($params['credentials']), $content);
  81. }
  82. return $content;
  83. }
  84. public function configToOptions($configs, $prefix = '')
  85. {
  86. foreach ($configs as $name => $config)
  87. {
  88. $name = $prefix ? $prefix.'_'.$name : $name;
  89. if (is_array($config))
  90. {
  91. $this->configToOptions($config, $name);
  92. }
  93. $this->options[$name] = $config;
  94. }
  95. }
  96. public function linkTo($action, $params)
  97. {
  98. $forObject = isset($params['object_link']) && $params['object_link'];
  99. $params = array_merge(array('class' => $action), $params);
  100. $method = sprintf('linkTo%s', ucwords(sfInflector::camelize($action)));
  101. $link = method_exists($this, $method) ? $this->$method($params) : $this->getLinkToAction($action, $params, $forObject);
  102. return $this->addCredentialCondition($link, $params);
  103. }
  104. /**
  105. * Returns HTML code for an action link.
  106. *
  107. * @param string $actionName The action name
  108. * @param array $params The parameters
  109. * @param boolean $pk_link Whether to add a primary key link or not
  110. *
  111. * @return string HTML code
  112. */
  113. public function getLinkToAction($actionName, $params, $object_link = false)
  114. {
  115. $route = isset($params['route']) ? $params['route'] : null;
  116. $action = isset($params['action']) ? $params['action'] : 'List'.sfInflector::camelize($actionName);
  117. $label = isset($params['label']) ? $params['label'] : sfInflector::humanize($actionName);
  118. if (isset($params['confirm'])) {
  119. $params['confirm'] = $this->renderPhpArrayText($params['confirm']);
  120. }
  121. if (isset($params['title'])) {
  122. $params['title'] = $this->renderPhpArrayText($params['title']);
  123. }
  124. // Not a "link_to" attribute
  125. unset($params['action'], $params['label'], $params['route'], $params['object_link'], $params['credentials']);
  126. $sf_subject = $object_link ? '$'.$this->getSingularName() : null;
  127. $urlOptions = array();
  128. $linkOptions = $params;
  129. if ($route) {
  130. $route = $this->asPhp($route);
  131. }
  132. else {
  133. $route = $this->urlFor($object_link ? 'object' : 'collection', false);
  134. $urlOptions['action'] = $action;
  135. }
  136. if ($sf_subject) {
  137. $urlOptions['sf_subject'] = $this->parser->wrapPhpToken($sf_subject);
  138. }
  139. // Old style URL
  140. if (strpos($route, "'@") === 0) {
  141. $options = $urlOptions ? array_merge($urlOptions, $linkOptions) : $linkOptions;
  142. return $this->_renderOldStyleRoute($this->renderPhpText($label), $route, $this->parser->renderArray($options));
  143. }
  144. $urlOptions = count($urlOptions) == 1 && isset($urlOptions['sf_subject']) ? $sf_subject : $this->parser->renderArray($urlOptions);
  145. return $this->_renderNewStyleRoute($this->renderPhpText($label), $route, $urlOptions, $this->parser->renderArray($linkOptions));
  146. }
  147. protected function _renderOldStyleRoute($label, $route, $options)
  148. {
  149. return $this->parser->replaceTokens(sprintf('[?php echo link_to(%s, %s, %s) ?]', $label, $route, $options), null);
  150. }
  151. protected function _renderNewStyleRoute($label, $route, $urlOptions, $linkOptions)
  152. {
  153. return $this->parser->replaceTokens(sprintf('[?php echo link_to(%s, %s, %s, %s) ?]', $label, $route, $urlOptions, $linkOptions), null);
  154. }
  155. public function urlFor($action, $routeName = true)
  156. {
  157. if (isset($this->params['route_prefix']))
  158. {
  159. $route = 'list' == $action ? $this->params['route_prefix'] : $this->params['route_prefix'].'_'.$action;
  160. return $this->asPhp(($routeName ? '@' : '').$route);
  161. }
  162. return $this->asPhp($this->getModuleName().'/'.$action);
  163. }
  164. public function checkConfigIsValid($configs, $available)
  165. {
  166. if ($available !== array()) // all options pass for "array()"
  167. {
  168. foreach ($configs as $key => $config)
  169. {
  170. if (!isset($available[$key]))
  171. {
  172. throw new InvalidArgumentException(sprintf('Configuration key "%s" is invalid.', $key));
  173. }
  174. if (is_array($config) && is_array($available[$key]))
  175. {
  176. $this->checkConfigIsValid($config, $available[$key]);
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * Generates classes and templates in cache.
  183. *
  184. * @param array $params The parameters
  185. *
  186. * @return string The data to put in configuration cache
  187. */
  188. public function generate($params = array())
  189. {
  190. $params = $this->validateParameters($params);
  191. $this->saveParams($params);
  192. // theme exists?
  193. if (!is_dir($themeDir = $this->getThemeDirectory())) {
  194. throw new sfConfigurationException(sprintf('The theme "%s" does not exist.', $this->getTheme()));
  195. }
  196. // configure the model
  197. $this->configure();
  198. $this->configuration = $this->loadConfiguration();
  199. $this->parser = $this->getTokenParser();
  200. $files = $this->getPhpFilesToGenerate($themeDir);
  201. // generate files
  202. $this->generatePhpFiles($this->generatedModuleName, $files);
  203. return sprintf("require_once('%s/%s/actions/actions.class.php');", sfConfig::get('sf_module_cache_dir'), $this->generatedModuleName);
  204. }
  205. protected function saveParams($params)
  206. {
  207. $this->params = $params;
  208. $this->configToOptions($params);
  209. $this->setModuleName($params['moduleName']);
  210. $this->modelClass = $params['model_class'];
  211. $this->setGeneratedModuleName('auto'.ucfirst($this->getModuleName()));
  212. $this->options['singular_name'] = $this->getSingularName();
  213. $this->options['class_label'] = $this->getClassLabel();
  214. $this->setTheme(isset($params['theme']) ? $params['theme'] : 'default');
  215. $this->themeDir = $this->generatorManager->getConfiguration()->getGeneratorTemplate($this->getGeneratorClass(), $this->getTheme(), '');
  216. }
  217. /**
  218. * Loads the configuration for this generated module.
  219. */
  220. protected function loadConfiguration()
  221. {
  222. $class = $this->getConfigurationClass();
  223. $configuration = new $class($this->config, $this->params);
  224. $this->configToOptions($configuration->getConfiguration());
  225. return $configuration;
  226. }
  227. protected function validateParameters($params)
  228. {
  229. foreach (array('model_class', 'moduleName') as $key)
  230. {
  231. if (!isset($params[$key]))
  232. {
  233. throw new sfParseException(sprintf('sfModelGenerator must have a "%s" parameter.', $key));
  234. }
  235. }
  236. if (!class_exists($params['model_class']))
  237. {
  238. throw new sfInitializationException(sprintf('Unable to generate a module for non-existent model "%s".', $params['model_class']));
  239. }
  240. if (isset($params['config'])) {
  241. $this->checkConfigIsValid($params['config'], $this->availableConfigs);
  242. $this->config = $params['config'];
  243. }
  244. else {
  245. $this->config = array();
  246. }
  247. unset($params['config']);
  248. return $params;
  249. }
  250. protected function renderCredentials($credentials)
  251. {
  252. if (is_array($credentials) && count($credentials) == 1
  253. && isset($credentials[0]) && is_string($credentials[0])) {
  254. $credentials = $credentials[0];
  255. }
  256. return is_array($credentials) ? $this->parser->renderArray($credentials) : $this->asPhp($credentials);
  257. }
  258. // Provides a hook to change generated files
  259. protected function getPhpFilesToGenerate($themeDir)
  260. {
  261. return sfFinder::type('file')->relative()->in($themeDir);
  262. }
  263. protected function getTokenParser()
  264. {
  265. return new sfThemeTokenParser($this->options, $this->getSingularName(), $this->getI18nCatalogue());
  266. }
  267. protected function getConfigurationClass()
  268. {
  269. return 'sfThemeGeneratorConfiguration';
  270. }
  271. }