[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/filter/ -> sfExecutionFilter.class.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   * (c) 2004-2006 Sean Kerr.
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * sfExecutionFilter is the last filter registered for each filter chain. This
  14   * filter does all action and view execution.
  15   *
  16   * @package    symfony
  17   * @subpackage filter
  18   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  19   * @author     Sean Kerr <skerr@mojavi.org>
  20   * @version    SVN: $Id: sfExecutionFilter.class.php 3244 2007-01-12 14:46:11Z fabien $
  21   */
  22  class sfExecutionFilter extends sfFilter
  23  {
  24    /**
  25     * Executes this filter.
  26     *
  27     * @param sfFilterChain The filter chain
  28     *
  29     * @throws <b>sfInitializeException</b> If an error occurs during view initialization.
  30     * @throws <b>sfViewException</b>       If an error occurs while executing the view.
  31     */
  32    public function execute($filterChain)
  33    {
  34      // get the context and controller
  35      $context    = $this->getContext();
  36      $controller = $context->getController();
  37  
  38      // get the current action instance
  39      $actionEntry    = $controller->getActionStack()->getLastEntry();
  40      $actionInstance = $actionEntry->getActionInstance();
  41  
  42      // get the current action information
  43      $moduleName = $context->getModuleName();
  44      $actionName = $context->getActionName();
  45  
  46      // get the request method
  47      $method = $context->getRequest()->getMethod();
  48  
  49      $viewName = null;
  50  
  51      if (sfConfig::get('sf_cache'))
  52      {
  53        $uri = sfRouting::getInstance()->getCurrentInternalUri();
  54        if (null !== $context->getResponse()->getParameter($uri.'_action', null, 'symfony/cache'))
  55        {
  56          // action in cache, so go to the view
  57          $viewName = sfView::SUCCESS;
  58        }
  59      }
  60  
  61      if (!$viewName)
  62      {
  63        if (($actionInstance->getRequestMethods() & $method) != $method)
  64        {
  65          // this action will skip validation/execution for this method
  66          // get the default view
  67          $viewName = $actionInstance->getDefaultView();
  68        }
  69        else
  70        {
  71          // set default validated status
  72          $validated = true;
  73  
  74          // get the current action validation configuration
  75          $validationConfig = $moduleName.'/'.sfConfig::get('sf_app_module_validate_dir_name').'/'.$actionName.'.yml';
  76  
  77          // load validation configuration
  78          // do NOT use require_once
  79          if (null !== $validateFile = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$validationConfig, true))
  80          {
  81            // create validator manager
  82            $validatorManager = new sfValidatorManager();
  83            $validatorManager->initialize($context);
  84  
  85            require($validateFile);
  86  
  87            // process validators
  88            $validated = $validatorManager->execute();
  89          }
  90  
  91          // process manual validation
  92          $validateToRun = 'validate'.ucfirst($actionName);
  93          $manualValidated = method_exists($actionInstance, $validateToRun) ? $actionInstance->$validateToRun() : $actionInstance->validate();
  94  
  95          // action is validated if:
  96          // - all validation methods (manual and automatic) return true
  97          // - or automatic validation returns false but errors have been 'removed' by manual validation
  98          $validated = ($manualValidated && $validated) || ($manualValidated && !$validated && !$context->getRequest()->hasErrors());
  99  
 100          // register fill-in filter
 101          if (null !== ($parameters = $context->getRequest()->getAttribute('fillin', null, 'symfony/filter')))
 102          {
 103            $this->registerFillInFilter($filterChain, $parameters);
 104          }
 105  
 106          if ($validated)
 107          {
 108            if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
 109            {
 110              $timer = sfTimerManager::getTimer(sprintf('Action "%s/%s"', $moduleName, $actionName));
 111            }
 112  
 113            // execute the action
 114            $actionInstance->preExecute();
 115            $viewName = $actionInstance->execute();
 116            if ($viewName == '')
 117            {
 118              $viewName = sfView::SUCCESS;
 119            }
 120            $actionInstance->postExecute();
 121  
 122            if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
 123            {
 124              $timer->addTime();
 125            }
 126          }
 127          else
 128          {
 129            if (sfConfig::get('sf_logging_enabled'))
 130            {
 131              $this->context->getLogger()->info('{sfFilter} action validation failed');
 132            }
 133  
 134            // validation failed
 135            $handleErrorToRun = 'handleError'.ucfirst($actionName);
 136            $viewName = method_exists($actionInstance, $handleErrorToRun) ? $actionInstance->$handleErrorToRun() : $actionInstance->handleError();
 137            if ($viewName == '')
 138            {
 139              $viewName = sfView::ERROR;
 140            }
 141          }
 142        }
 143      }
 144  
 145      if ($viewName == sfView::HEADER_ONLY)
 146      {
 147        $context->getResponse()->setHeaderOnly(true);
 148  
 149        // execute next filter
 150        $filterChain->execute();
 151      }
 152      else if ($viewName != sfView::NONE)
 153      {
 154        if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
 155        {
 156          $timer = sfTimerManager::getTimer(sprintf('View "%s" for "%s/%s"', $viewName, $moduleName, $actionName));
 157        }
 158  
 159        // get the view instance
 160        $viewInstance = $controller->getView($moduleName, $actionName, $viewName);
 161  
 162        $viewInstance->initialize($context, $moduleName, $actionName, $viewName);
 163  
 164        $viewInstance->execute();
 165  
 166        // render the view and if data is returned, stick it in the
 167        // action entry which was retrieved from the execution chain
 168        $viewData = $viewInstance->render();
 169  
 170        if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
 171        {
 172          $timer->addTime();
 173        }
 174  
 175        if ($controller->getRenderMode() == sfView::RENDER_VAR)
 176        {
 177          $actionEntry->setPresentation($viewData);
 178        }
 179        else
 180        {
 181          // execute next filter
 182          $filterChain->execute();
 183        }
 184      }
 185    }
 186  
 187    /**
 188     * Registers the fill in filter in the filter chain.
 189     *
 190     * @param sfFilterChain A sfFilterChain implementation instance
 191     * @param array An array of parameters to pass to the fill in filter.
 192     */
 193    protected function registerFillInFilter($filterChain, $parameters)
 194    {
 195      // automatically register the fill in filter if it is not already loaded in the chain
 196      if (isset($parameters['enabled']) && $parameters['enabled'] && !$filterChain->hasFilter('sfFillInFormFilter'))
 197      {
 198        // register the fill in form filter
 199        $fillInFormFilter = new sfFillInFormFilter();
 200        $fillInFormFilter->initialize($this->context, isset($parameters['param']) ? $parameters['param'] : array());
 201        $filterChain->register($fillInFormFilter);
 202      }
 203    }
 204  }


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