PageRenderTime 32ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/data/symfony/helper/PartialHelper.php

http://openirudi.googlecode.com/
PHP | 389 lines | 162 code | 52 blank | 175 comment | 21 complexity | 53c869b5c5f52780d315ce3ea5d26c99 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * PartialHelper.
  11. *
  12. * @package symfony
  13. * @subpackage helper
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: PartialHelper.php 10227 2008-07-11 19:36:32Z fabien $
  16. */
  17. /**
  18. * Evaluates and echoes a component slot.
  19. * The component name is deduced from the definition of the view.yml
  20. * For a variable to be accessible to the component and its partial,
  21. * it has to be passed in the second argument.
  22. *
  23. * <b>Example:</b>
  24. * <code>
  25. * include_component_slot('sidebar', array('myvar' => 12345));
  26. * </code>
  27. *
  28. * @param string slot name
  29. * @param array variables to be made accessible to the component
  30. *
  31. * @see get_component_slot, include_partial, include_component
  32. */
  33. function include_component_slot($name, $vars = array())
  34. {
  35. echo get_component_slot($name, $vars);
  36. }
  37. /**
  38. * Evaluates and returns a component slot.
  39. * The syntax is similar to the one of include_component_slot.
  40. *
  41. * <b>Example:</b>
  42. * <code>
  43. * echo get_component_slot('sidebar', array('myvar' => 12345));
  44. * </code>
  45. *
  46. * @param string $name slot name
  47. * @param array $vars variables to be made accessible to the component
  48. *
  49. * @return string result of the component execution
  50. * @see get_component_slot, include_partial, include_component
  51. */
  52. function get_component_slot($name, $vars = array())
  53. {
  54. $viewInstance = sfContext::getInstance()->get('view_instance');
  55. if (!$viewInstance->hasComponentSlot($name))
  56. {
  57. // cannot find component slot
  58. throw new sfConfigurationException(sprintf('The component slot "%s" is not set.', $name));
  59. }
  60. if ($componentSlot = $viewInstance->getComponentSlot($name))
  61. {
  62. return get_component($componentSlot[0], $componentSlot[1], $vars);
  63. }
  64. }
  65. /**
  66. * Returns true if component slot exists.
  67. *
  68. * @param string slot name
  69. * @return bool true if component slot exists, false otherwise
  70. */
  71. function has_component_slot($name)
  72. {
  73. $viewInstance = sfContext::getInstance()->get('view_instance');
  74. // check to see if one is defined
  75. if (!$viewInstance->hasComponentSlot($name))
  76. {
  77. return false;
  78. }
  79. // check to see if component slot is empty (null)
  80. if ($viewInstance->getComponentSlot($name))
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * Evaluates and echoes a component.
  88. * For a variable to be accessible to the component and its partial,
  89. * it has to be passed in the third argument.
  90. *
  91. * <b>Example:</b>
  92. * <code>
  93. * include_component('mymodule', 'mypartial', array('myvar' => 12345));
  94. * </code>
  95. *
  96. * @param string $moduleName module name
  97. * @param string $componentName component name
  98. * @param array $vars variables to be made accessible to the component
  99. *
  100. * @see get_component, include_partial, include_component_slot
  101. */
  102. function include_component($moduleName, $componentName, $vars = array())
  103. {
  104. echo get_component($moduleName, $componentName, $vars);
  105. }
  106. /**
  107. * Evaluates and returns a component.
  108. * The syntax is similar to the one of include_component.
  109. *
  110. * <b>Example:</b>
  111. * <code>
  112. * echo get_component('mymodule', 'mypartial', array('myvar' => 12345));
  113. * </code>
  114. *
  115. * @param string $moduleName module name
  116. * @param string $componentName component name
  117. * @param array $vars variables to be made accessible to the component
  118. *
  119. * @return string result of the component execution
  120. * @see include_component
  121. */
  122. function get_component($moduleName, $componentName, $vars = array())
  123. {
  124. $context = sfContext::getInstance();
  125. $actionName = '_'.$componentName;
  126. $view = new sfPartialView($context, $moduleName, $actionName, '');
  127. $view->setPartialVars($vars);
  128. if ($retval = $view->getCache())
  129. {
  130. return $retval;
  131. }
  132. $allVars = _call_component($moduleName, $componentName, $vars);
  133. if (!is_null($allVars))
  134. {
  135. // render
  136. $view->getAttributeHolder()->add($allVars);
  137. return $view->render();
  138. }
  139. }
  140. /**
  141. * Evaluates and echoes a partial.
  142. * The partial name is composed as follows: 'mymodule/mypartial'.
  143. * The partial file name is _mypartial.php and is looked for in modules/mymodule/templates/.
  144. * If the partial name doesn't include a module name,
  145. * then the partial file is searched for in the caller's template/ directory.
  146. * If the module name is 'global', then the partial file is looked for in myapp/templates/.
  147. * For a variable to be accessible to the partial, it has to be passed in the second argument.
  148. *
  149. * <b>Example:</b>
  150. * <code>
  151. * include_partial('mypartial', array('myvar' => 12345));
  152. * </code>
  153. *
  154. * @param string $templateName partial name
  155. * @param array $vars variables to be made accessible to the partial
  156. *
  157. * @see get_partial, include_component
  158. */
  159. function include_partial($templateName, $vars = array())
  160. {
  161. echo get_partial($templateName, $vars);
  162. }
  163. /**
  164. * Evaluates and returns a partial.
  165. * The syntax is similar to the one of include_partial
  166. *
  167. * <b>Example:</b>
  168. * <code>
  169. * echo get_partial('mypartial', array('myvar' => 12345));
  170. * </code>
  171. *
  172. * @param string $templateName partial name
  173. * @param array $vars variables to be made accessible to the partial
  174. *
  175. * @return string result of the partial execution
  176. * @see include_partial
  177. */
  178. function get_partial($templateName, $vars = array())
  179. {
  180. $context = sfContext::getInstance();
  181. // partial is in another module?
  182. if (false !== $sep = strpos($templateName, '/'))
  183. {
  184. $moduleName = substr($templateName, 0, $sep);
  185. $templateName = substr($templateName, $sep + 1);
  186. }
  187. else
  188. {
  189. $moduleName = $context->getActionStack()->getLastEntry()->getModuleName();
  190. }
  191. $actionName = '_'.$templateName;
  192. $view = new sfPartialView($context, $moduleName, $actionName, '');
  193. $view->setPartialVars($vars);
  194. return $view->render();
  195. }
  196. /**
  197. * Begins the capturing of the slot.
  198. *
  199. * @param string $name slot name
  200. * @param string $value The slot content
  201. *
  202. * @see end_slot
  203. */
  204. function slot($name, $value = null)
  205. {
  206. $context = sfContext::getInstance();
  207. $response = $context->getResponse();
  208. $slot_names = sfConfig::get('symfony.view.slot_names', array());
  209. if (in_array($name, $slot_names))
  210. {
  211. throw new sfCacheException(sprintf('A slot named "%s" is already started.', $name));
  212. }
  213. if (sfConfig::get('sf_logging_enabled'))
  214. {
  215. $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Set slot "%s"', $name))));
  216. }
  217. if (!is_null($value))
  218. {
  219. $response->setSlot($name, $value);
  220. return;
  221. }
  222. $slot_names[] = $name;
  223. $response->setSlot($name, '');
  224. sfConfig::set('symfony.view.slot_names', $slot_names);
  225. ob_start();
  226. ob_implicit_flush(0);
  227. }
  228. /**
  229. * Stops the content capture and save the content in the slot.
  230. *
  231. * @see slot
  232. */
  233. function end_slot()
  234. {
  235. $content = ob_get_clean();
  236. $response = sfContext::getInstance()->getResponse();
  237. $slot_names = sfConfig::get('symfony.view.slot_names', array());
  238. if (!$slot_names)
  239. {
  240. throw new sfCacheException('No slot started.');
  241. }
  242. $name = array_pop($slot_names);
  243. $response->setSlot($name, $content);
  244. sfConfig::set('symfony.view.slot_names', $slot_names);
  245. }
  246. /**
  247. * Returns true if the slot exists.
  248. *
  249. * @param string $name slot name
  250. *
  251. * @return bool true, if the slot exists
  252. * @see get_slot, include_slot
  253. */
  254. function has_slot($name)
  255. {
  256. return array_key_exists($name, sfContext::getInstance()->getResponse()->getSlots());
  257. }
  258. /**
  259. * Evaluates and echoes a slot.
  260. *
  261. * <b>Example:</b>
  262. * <code>
  263. * include_slot('navigation');
  264. * </code>
  265. *
  266. * @param string $name slot name
  267. *
  268. * @see has_slot, get_slot
  269. */
  270. function include_slot($name)
  271. {
  272. return ($v = get_slot($name)) ? print $v : false;
  273. }
  274. /**
  275. * Evaluates and returns a slot.
  276. *
  277. * <b>Example:</b>
  278. * <code>
  279. * echo get_slot('navigation');
  280. * </code>
  281. *
  282. * @param string $name slot name
  283. *
  284. * @return string content of the slot
  285. * @see has_slot, include_slot
  286. */
  287. function get_slot($name)
  288. {
  289. $context = sfContext::getInstance();
  290. $slots = $context->getResponse()->getSlots();
  291. if (sfConfig::get('sf_logging_enabled'))
  292. {
  293. $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Get slot "%s"', $name))));
  294. }
  295. return isset($slots[$name]) ? $slots[$name] : '';
  296. }
  297. function _call_component($moduleName, $componentName, $vars)
  298. {
  299. $context = sfContext::getInstance();
  300. $controller = $context->getController();
  301. if (!$controller->componentExists($moduleName, $componentName))
  302. {
  303. // cannot find component
  304. throw new sfConfigurationException(sprintf('The component does not exist: "%s", "%s".', $moduleName, $componentName));
  305. }
  306. // create an instance of the action
  307. $componentInstance = $controller->getComponent($moduleName, $componentName);
  308. // load component's module config file
  309. require($context->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml'));
  310. $componentInstance->getVarHolder()->add($vars);
  311. // dispatch component
  312. $componentToRun = 'execute'.ucfirst($componentName);
  313. if (!method_exists($componentInstance, $componentToRun))
  314. {
  315. if (!method_exists($componentInstance, 'execute'))
  316. {
  317. // component not found
  318. throw new sfInitializationException(sprintf('sfComponent initialization failed for module "%s", component "%s".', $moduleName, $componentName));
  319. }
  320. $componentToRun = 'execute';
  321. }
  322. if (sfConfig::get('sf_logging_enabled'))
  323. {
  324. $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Call "%s->%s()'.'"', $moduleName, $componentToRun))));
  325. }
  326. // run component
  327. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  328. {
  329. $timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName));
  330. }
  331. $retval = $componentInstance->$componentToRun($context->getRequest());
  332. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  333. {
  334. $timer->addTime();
  335. }
  336. return sfView::NONE == $retval ? null : $componentInstance->getVarHolder()->getAll();
  337. }