[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/helper/ -> PartialHelper.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * PartialHelper.
  13   *
  14   * @package    symfony
  15   * @subpackage helper
  16   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  17   * @version    SVN: $Id: PartialHelper.php 3265 2007-01-13 17:06:40Z fabien $
  18   */
  19  
  20  /**
  21   * Evaluates and echoes a component slot.
  22   * The component name is deduced from the definition of the view.yml
  23   * For a variable to be accessible to the component and its partial, 
  24   * it has to be passed in the second argument.
  25   *
  26   * <b>Example:</b>
  27   * <code>
  28   *  include_component_slot('sidebar', array('myvar' => 12345));
  29   * </code>
  30   *
  31   * @param  string slot name
  32   * @param  array variables to be made accessible to the component
  33   * @return void
  34   * @see    get_component_slot, include_partial, include_component
  35   */
  36  function include_component_slot($name, $vars = array())
  37  {
  38    echo get_component_slot($name, $vars);
  39  }
  40  
  41  /**
  42   * Evaluates and returns a component slot.
  43   * The syntax is similar to the one of include_component_slot.
  44   *
  45   * <b>Example:</b>
  46   * <code>
  47   *  echo get_component_slot('sidebar', array('myvar' => 12345));
  48   * </code>
  49   *
  50   * @param  string slot name
  51   * @param  array variables to be made accessible to the component
  52   * @return string result of the component execution
  53   * @see    get_component_slot, include_partial, include_component
  54   */
  55  function get_component_slot($name, $vars = array())
  56  {
  57    $context = sfContext::getInstance();
  58  
  59    $actionStackEntry = $context->getController()->getActionStack()->getLastEntry();
  60    $viewInstance     = $actionStackEntry->getViewInstance();
  61  
  62    if (!$viewInstance->hasComponentSlot($name))
  63    {
  64      // cannot find component slot
  65      $error = 'The component slot "%s" is not set';
  66      $error = sprintf($error, $name);
  67  
  68      throw new sfConfigurationException($error);
  69    }
  70  
  71    if ($componentSlot = $viewInstance->getComponentSlot($name))
  72    {
  73      return get_component($componentSlot[0], $componentSlot[1], $vars);
  74    }
  75  }
  76  
  77  /**
  78   * Evaluates and echoes a component.
  79   * For a variable to be accessible to the component and its partial, 
  80   * it has to be passed in the third argument.
  81   *
  82   * <b>Example:</b>
  83   * <code>
  84   *  include_component('mymodule', 'mypartial', array('myvar' => 12345));
  85   * </code>
  86   *
  87   * @param  string module name
  88   * @param  string component name
  89   * @param  array variables to be made accessible to the component
  90   * @return void
  91   * @see    get_component, include_partial, include_component_slot
  92   */
  93  function include_component($moduleName, $componentName, $vars = array())
  94  {
  95    echo get_component($moduleName, $componentName, $vars);
  96  }
  97  
  98  /**
  99   * Evaluates and returns a component.
 100   * The syntax is similar to the one of include_component.
 101   *
 102   * <b>Example:</b>
 103   * <code>
 104   *  echo get_component('mymodule', 'mypartial', array('myvar' => 12345));
 105   * </code>
 106   *
 107   * @param  string module name
 108   * @param  string component name
 109   * @param  array variables to be made accessible to the component
 110   * @return string result of the component execution
 111   * @see    include_component
 112   */
 113  function get_component($moduleName, $componentName, $vars = array())
 114  {
 115    $context = sfContext::getInstance();
 116    $actionName = '_'.$componentName;
 117  
 118    // check cache
 119    if ($cacheManager = $context->getViewCacheManager())
 120    {
 121      $cacheManager->registerConfiguration($moduleName);
 122      $uri = '@sf_cache_partial?module='.$moduleName.'&action='.$actionName.'&sf_cache_key='.(isset($vars['sf_cache_key']) ? $vars['sf_cache_key'] : md5(serialize($vars)));
 123      if ($retval = _get_cache($cacheManager, $uri))
 124      {
 125        return $retval;
 126      }
 127    }
 128  
 129    $controller = $context->getController();
 130  
 131    if (!$controller->componentExists($moduleName, $componentName))
 132    {
 133      // cannot find component
 134      $error = 'The component does not exist: "%s", "%s"';
 135      $error = sprintf($error, $moduleName, $componentName);
 136  
 137      throw new sfConfigurationException($error);
 138    }
 139  
 140    // create an instance of the action
 141    $componentInstance = $controller->getComponent($moduleName, $componentName);
 142  
 143    // initialize the action
 144    if (!$componentInstance->initialize($context))
 145    {
 146      // component failed to initialize
 147      $error = 'Component initialization failed for module "%s", component "%s"';
 148      $error = sprintf($error, $moduleName, $componentName);
 149  
 150      throw new sfInitializationException($error);
 151    }
 152  
 153    // load component's module config file
 154    require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$moduleName.'/'.sfConfig::get('sf_app_module_config_dir_name').'/module.yml'));
 155  
 156    $componentInstance->getVarHolder()->add($vars);
 157  
 158    // dispatch component
 159    $componentToRun = 'execute'.ucfirst($componentName);
 160    if (!method_exists($componentInstance, $componentToRun))
 161    {
 162      if (!method_exists($componentInstance, 'execute'))
 163      {
 164        // component not found
 165        $error = 'sfComponent initialization failed for module "%s", component "%s"';
 166        $error = sprintf($error, $moduleName, $componentName);
 167        throw new sfInitializationException($error);
 168      }
 169  
 170      $componentToRun = 'execute';
 171    }
 172  
 173    if (sfConfig::get('sf_logging_enabled'))
 174    {
 175      $context->getLogger()->info('{PartialHelper} call "'.$moduleName.'->'.$componentToRun.'()'.'"');
 176    }
 177  
 178    // run component
 179    if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
 180    {
 181      $timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName));
 182    }
 183  
 184    $retval = $componentInstance->$componentToRun();
 185  
 186    if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
 187    {
 188      $timer->addTime();
 189    }
 190  
 191    if ($retval != sfView::NONE)
 192    {
 193      // render
 194      $view = new sfPartialView();
 195      $view->initialize($context, $moduleName, $actionName, '');
 196  
 197      $retval = $view->render($componentInstance->getVarHolder()->getAll());
 198  
 199      if ($cacheManager)
 200      {
 201        $retval = _set_cache($cacheManager, $uri, $retval);
 202      }
 203  
 204      return $retval;
 205    }
 206  }
 207  
 208  /**
 209   * Evaluates and echoes a partial.
 210   * The partial name is composed as follows: 'mymodule/mypartial'.
 211   * The partial file name is _mypartial.php and is looked for in modules/mymodule/templates/.
 212   * If the partial name doesn't include a module name,
 213   * then the partial file is searched for in the caller's template/ directory.
 214   * If the module name is 'global', then the partial file is looked for in myapp/templates/.
 215   * For a variable to be accessible to the partial, it has to be passed in the second argument.
 216   *
 217   * <b>Example:</b>
 218   * <code>
 219   *  include_partial('mypartial', array('myvar' => 12345));
 220   * </code>
 221   *
 222   * @param  string partial name
 223   * @param  array variables to be made accessible to the partial
 224   * @return void
 225   * @see    get_partial, include_component
 226   */
 227  function include_partial($templateName, $vars = array())
 228  {
 229    echo get_partial($templateName, $vars);
 230  }
 231  
 232  /**
 233   * Evaluates and returns a partial.
 234   * The syntax is similar to the one of include_partial
 235   *
 236   * <b>Example:</b>
 237   * <code>
 238   *  echo get_partial('mypartial', array('myvar' => 12345));
 239   * </code>
 240   *
 241   * @param  string partial name
 242   * @param  array variables to be made accessible to the partial
 243   * @return string result of the partial execution
 244   * @see    include_partial
 245   */
 246  function get_partial($templateName, $vars = array())
 247  {
 248    $context = sfContext::getInstance();
 249  
 250    // partial is in another module?
 251    if (false !== $sep = strpos($templateName, '/'))
 252    {
 253      $moduleName   = substr($templateName, 0, $sep);
 254      $templateName = substr($templateName, $sep + 1);
 255    }
 256    else
 257    {
 258      $moduleName = $context->getActionStack()->getLastEntry()->getModuleName();
 259    }
 260    $actionName = '_'.$templateName;
 261  
 262    if ($cacheManager = $context->getViewCacheManager())
 263    {
 264      $cacheManager->registerConfiguration($moduleName);
 265      $uri = '@sf_cache_partial?module='.$moduleName.'&action='.$actionName.'&sf_cache_key='.(isset($vars['sf_cache_key']) ? $vars['sf_cache_key'] : md5(serialize($vars)));
 266      if ($retval = _get_cache($cacheManager, $uri))
 267      {
 268        return $retval;
 269      }
 270    }
 271  
 272    $view = new sfPartialView();
 273    $view->initialize($context, $moduleName, $actionName, '');
 274    $retval = $view->render($vars);
 275  
 276    if ($cacheManager)
 277    {
 278      $retval = _set_cache($cacheManager, $uri, $retval);
 279    }
 280  
 281    return $retval;
 282  }
 283  
 284  function _get_cache($cacheManager, $uri)
 285  {
 286    $retval = $cacheManager->get($uri);
 287  
 288    if (sfConfig::get('sf_web_debug'))
 289    {
 290      $retval = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $retval, false);
 291    }
 292  
 293    return $retval;
 294  }
 295  
 296  function _set_cache($cacheManager, $uri, $retval)
 297  {
 298    $saved = $cacheManager->set($retval, $uri);
 299  
 300    if ($saved && sfConfig::get('sf_web_debug'))
 301    {
 302      $retval = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $retval, true);
 303    }
 304  
 305    return $retval;
 306  }
 307  
 308  /**
 309   * Begins the capturing of the slot.
 310   *
 311   * @param  string slot name
 312   * @return void
 313   * @see    end_slot
 314   */
 315  function slot($name)
 316  {
 317    $context = sfContext::getInstance();
 318    $response = $context->getResponse();
 319  
 320    $slots = $response->getParameter('slots', array(), 'symfony/view/sfView/slot');
 321    $slot_names = $response->getParameter('slot_names', array(), 'symfony/view/sfView/slot');
 322    if (in_array($name, $slot_names))
 323    {
 324      throw new sfCacheException(sprintf('A slot named "%s" is already started.', $name));
 325    }
 326  
 327    $slot_names[] = $name;
 328    $slots[$name] = '';
 329  
 330    $response->setParameter('slots', $slots, 'symfony/view/sfView/slot');
 331    $response->setParameter('slot_names', $slot_names, 'symfony/view/sfView/slot');
 332  
 333    if (sfConfig::get('sf_logging_enabled'))
 334    {
 335      $context->getLogger()->info(sprintf('{PartialHelper} set slot "%s"', $name));
 336    }
 337  
 338    ob_start();
 339    ob_implicit_flush(0);
 340  }
 341  
 342  /**
 343   * Stops the content capture and save the content in the slot.
 344   *
 345   * @return void
 346   * @see    slot
 347   */
 348  function end_slot()
 349  {
 350    $content = ob_get_clean();
 351  
 352    $response = sfContext::getInstance()->getResponse();
 353    $slots = $response->getParameter('slots', array(), 'symfony/view/sfView/slot');
 354    $slot_names = $response->getParameter('slot_names', array(), 'symfony/view/sfView/slot');
 355    if (!$slot_names)
 356    {
 357      throw new sfCacheException('No slot started.');
 358    }
 359  
 360    $name = array_pop($slot_names);
 361    $slots[$name] = $content;
 362  
 363    $response->setParameter('slots', $slots, 'symfony/view/sfView/slot');
 364    $response->setParameter('slot_names', $slot_names, 'symfony/view/sfView/slot');
 365  }
 366  
 367  /**
 368   * Returns true if the slot exists.
 369   *
 370   * @param  string slot name
 371   * @return boolean true, if the slot exists
 372   * @see    get_slot, include_slot
 373   */
 374  function has_slot($name)
 375  {
 376    $response = sfContext::getInstance()->getResponse();
 377    $slots = $response->getParameter('slots', array(), 'symfony/view/sfView/slot');
 378  
 379    return array_key_exists($name, $slots);
 380  }
 381  
 382  /**
 383   * Evaluates and echoes a slot.
 384   *
 385   * <b>Example:</b>
 386   * <code>
 387   *  include_slot('navigation');
 388   * </code>
 389   *
 390   * @param  string slot name
 391   * @return void
 392   * @see    has_slot, get_slot
 393   */
 394  function include_slot($name)
 395  {
 396    $context = sfContext::getInstance();
 397    $slots = $context->getResponse()->getParameter('slots', array(), 'symfony/view/sfView/slot');
 398  
 399    if (sfConfig::get('sf_logging_enabled'))
 400    {
 401      $context->getLogger()->info(sprintf('{PartialHelper} get slot "%s"', $name));
 402    }
 403  
 404    if (isset($slots[$name]))
 405    {
 406      echo $slots[$name];
 407  
 408      return true;
 409    }
 410    else
 411    {
 412      return false;
 413    }
 414  }
 415  
 416  /**
 417   * Evaluates and returns a slot.
 418   *
 419   * <b>Example:</b>
 420   * <code>
 421   *  echo get_slot('navigation');
 422   * </code>
 423   *
 424   * @param  string slot name
 425   * @return string content of the slot
 426   * @see    has_slot, include_slot
 427   */
 428  function get_slot($name)
 429  {
 430    $context = sfContext::getInstance();
 431    $slots = $context->getResponse()->getParameter('slots', array(), 'symfony/view/sfView/slot');
 432  
 433    if (sfConfig::get('sf_logging_enabled'))
 434    {
 435      $context->getLogger()->info(sprintf('{PartialHelper} get slot "%s"', $name));
 436    }
 437  
 438    return isset($slots[$name]) ? $slots[$name] : '';
 439  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7