[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/action/ -> sfAction.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   * sfAction executes all the logic for the current request.
  14   *
  15   * @package    symfony
  16   * @subpackage action
  17   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  18   * @author     Sean Kerr <skerr@mojavi.org>
  19   * @version    SVN: $Id: sfAction.class.php 3209 2007-01-10 08:37:24Z fabien $
  20   */
  21  abstract class sfAction extends sfComponent
  22  {
  23    protected
  24      $security = array();
  25  
  26    /**
  27     * Initializes this action.
  28     *
  29     * @param sfContext The current application context.
  30     *
  31     * @return bool true, if initialization completes successfully, otherwise false
  32     */
  33    public function initialize($context)
  34    {
  35      parent::initialize($context);
  36  
  37      // include security configuration
  38      require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$this->getModuleName().'/'.sfConfig::get('sf_app_module_config_dir_name').'/security.yml', true));
  39  
  40      return true;
  41    }
  42  
  43    /**
  44     * Executes an application defined process prior to execution of this sfAction object.
  45     *
  46     * By default, this method is empty.
  47     */
  48    public function preExecute()
  49    {
  50    }
  51  
  52    /**
  53     * Execute an application defined process immediately after execution of this sfAction object.
  54     *
  55     * By default, this method is empty.
  56     */
  57    public function postExecute()
  58    {
  59    }
  60  
  61    /**
  62     * Forwards current action to the default 404 error action.
  63     *
  64     * @param  string Message of the generated exception
  65     *
  66     * @throws sfError404Exception
  67     *
  68     */
  69    public function forward404($message = '')
  70    {
  71      throw new sfError404Exception($message);
  72    }
  73  
  74    /**
  75     * Forwards current action to the default 404 error action unless the specified condition is true.
  76     *
  77     * @param bool A condition that evaluates to true or false
  78     * @param  string Message of the generated exception
  79     *
  80     * @throws sfError404Exception
  81     */
  82    public function forward404Unless($condition, $message = '')
  83    {
  84      if (!$condition)
  85      {
  86        throw new sfError404Exception($message);
  87      }
  88    }
  89  
  90    /**
  91     * Forwards current action to the default 404 error action if the specified condition is true.
  92     *
  93     * @param bool A condition that evaluates to true or false
  94     * @param  string Message of the generated exception
  95     *
  96     * @throws sfError404Exception
  97     */
  98    public function forward404If($condition, $message = '')
  99    {
 100      if ($condition)
 101      {
 102        throw new sfError404Exception($message);
 103      }
 104    }
 105  
 106    /**
 107     * Redirects current action to the default 404 error action (with browser redirection).
 108     *
 109     * This method stops the current code flow.
 110     *
 111     */
 112    public function redirect404()
 113    {
 114      return $this->redirect('/'.sfConfig::get('sf_error_404_module').'/'.sfConfig::get('sf_error_404_action'));
 115    }
 116  
 117    /**
 118     * Forwards current action to a new one (without browser redirection).
 119     *
 120     * This method stops the action. So, no code is executed after a call to this method.
 121     *
 122     * @param  string A module name
 123     * @param  string An action name
 124     *
 125     * @throws sfStopException
 126     */
 127    public function forward($module, $action)
 128    {
 129      if (sfConfig::get('sf_logging_enabled'))
 130      {
 131        $this->getContext()->getLogger()->info('{sfAction} forward to action "'.$module.'/'.$action.'"');
 132      }
 133  
 134      $this->getController()->forward($module, $action);
 135  
 136      throw new sfStopException();
 137    }
 138  
 139    /**
 140     * If the condition is true, forwards current action to a new one (without browser redirection).
 141     *
 142     * This method stops the action. So, no code is executed after a call to this method.
 143     *
 144     * @param  bool   A condition that evaluates to true or false
 145     * @param  string A module name
 146     * @param  string An action name
 147     *
 148     * @throws sfStopException
 149     */
 150    public function forwardIf($condition, $module, $action)
 151    {
 152      if ($condition)
 153      {
 154        $this->forward($module, $action);
 155      }
 156    }
 157  
 158    /**
 159     * Unless the condition is true, forwards current action to a new one (without browser redirection).
 160     *
 161     * This method stops the action. So, no code is executed after a call to this method.
 162     *
 163     * @param  bool   A condition that evaluates to true or false
 164     * @param  string A module name
 165     * @param  string An action name
 166     *
 167     * @throws sfStopException
 168     */
 169    public function forwardUnless($condition, $module, $action)
 170    {
 171      if (!$condition)
 172      {
 173        $this->forward($module, $action);
 174      }
 175    }
 176  
 177    /**
 178     * Redirects current request to a new URL.
 179     *
 180     * 2 URL formats are accepted :
 181     *  - a full URL: http://www.google.com/
 182     *  - an internal URL (url_for() format): module/action
 183     *
 184     * This method stops the action. So, no code is executed after a call to this method.
 185     *
 186     * @param  string Url
 187     * @param  string Status code (default to 302)
 188     *
 189     * @throws sfStopException
 190     */
 191    public function redirect($url, $statusCode = 302)
 192    {
 193      $url = $this->getController()->genUrl($url, true);
 194  
 195      if (sfConfig::get('sf_logging_enabled'))
 196      {
 197        $this->getContext()->getLogger()->info('{sfAction} redirect to "'.$url.'"');
 198      }
 199  
 200      $this->getController()->redirect($url, 0, $statusCode);
 201  
 202      throw new sfStopException();
 203    }
 204  
 205    /**
 206     * Redirects current request to a new URL, only if specified condition is true.
 207     *
 208     * This method stops the action. So, no code is executed after a call to this method.
 209     *
 210     * @param  bool   A condition that evaluates to true or false
 211     * @param  string url
 212     *
 213     * @throws sfStopException
 214     *
 215     * @see redirect
 216     */
 217    public function redirectIf($condition, $url)
 218    {
 219      if ($condition)
 220      {
 221        $this->redirect($url);
 222      }
 223    }
 224  
 225    /**
 226     * Redirects current request to a new URL, unless specified condition is true.
 227     *
 228     * This method stops the action. So, no code is executed after a call to this method.
 229     *
 230     * @param  bool   A condition that evaluates to true or false
 231     * @param  string Url
 232     *
 233     * @throws sfStopException
 234     *
 235     * @see redirect
 236     */
 237    public function redirectUnless($condition, $url)
 238    {
 239      if (!$condition)
 240      {
 241        $this->redirect($url);
 242      }
 243    }
 244  
 245    /**
 246     * Appends the given text to the response content and bypasses the built-in view system.
 247     *
 248     * This method must be called as with a return:
 249     *
 250     * <code>return $this->renderText('some text')</code>
 251     *
 252     * @param  string Text to append to the response
 253     *
 254     * @return sfView::NONE
 255     */
 256    public function renderText($text)
 257    {
 258      $this->getResponse()->setContent($this->getResponse()->getContent().$text);
 259  
 260      return sfView::NONE;
 261    }
 262  
 263    /**
 264     * Retrieves the default view to be executed when a given request is not served by this action.
 265     *
 266     * @return string A string containing the view name associated with this action
 267     */
 268    public function getDefaultView()
 269    {
 270      return sfView::INPUT;
 271    }
 272  
 273    /**
 274     * Retrieves the request methods on which this action will process validation and execution.
 275     *
 276     * @return int One of the following values:
 277     *
 278     * - sfRequest::GET
 279     * - sfRequest::POST
 280     * - sfRequest::PUT
 281     * - sfRequest::DELETE
 282     * - sfRequest::HEAD
 283     * - sfRequest::NONE
 284     *
 285     * @see sfRequest
 286     */
 287    public function getRequestMethods()
 288    {
 289      return sfRequest::GET
 290             | sfRequest::POST
 291             | sfRequest::PUT
 292             | sfRequest::DELETE
 293             | sfRequest::HEAD 
 294             | sfRequest::NONE;
 295    }
 296  
 297    /**
 298     * Executes any post-validation error application logic.
 299     *
 300     * @return string A string containing the view name associated with this action
 301     */
 302    public function handleError()
 303    {
 304      return sfView::ERROR;
 305    }
 306  
 307    /**
 308     * Validates manually files and parameters.
 309     *
 310     * @return bool true, if validation completes successfully, otherwise false.
 311     */
 312    public function validate()
 313    {
 314      return true;
 315    }
 316  
 317    /**
 318     * Returns the security configuration for this module.
 319     *
 320     * @return string Current security configuration as an array
 321     */
 322    public function getSecurityConfiguration()
 323    {
 324      return $this->security;
 325    }
 326  
 327    /**
 328     * Overrides the current security configuration for this module.
 329     *
 330     * @param array The new security configuration
 331     */
 332    public function setSecurityConfiguration($security)
 333    {
 334      $this->security = $security;
 335    }
 336  
 337    /**
 338     * Indicates that this action requires security.
 339     *
 340     * @return bool true, if this action requires security, otherwise false.
 341     */
 342    public function isSecure()
 343    {
 344      if (isset($this->security[$this->getActionName()]['is_secure']))
 345      {
 346        return $this->security[$this->getActionName()]['is_secure'];
 347      }
 348  
 349      if (isset($this->security['all']['is_secure']))
 350      {
 351        return $this->security['all']['is_secure'];
 352      }
 353  
 354      return false;
 355    }
 356  
 357    /**
 358     * Gets credentials the user must have to access this action.
 359     *
 360     * @return mixed An array or a string describing the credentials the user must have to access this action
 361     */
 362    public function getCredential()
 363    {
 364      if (isset($this->security[$this->getActionName()]['credentials']))
 365      {
 366        $credentials = $this->security[$this->getActionName()]['credentials'];
 367      }
 368      else if (isset($this->security['all']['credentials']))
 369      {
 370        $credentials = $this->security['all']['credentials'];
 371      }
 372      else
 373      {
 374        $credentials = null;
 375      }
 376  
 377      return $credentials;
 378    }
 379  
 380    /**
 381     * Sets an alternate template for this sfAction.
 382     *
 383     * See 'Naming Conventions' in the 'Symfony View' documentation.
 384     *
 385     * @param string Template name
 386     */
 387    public function setTemplate($name)
 388    {
 389      if (sfConfig::get('sf_logging_enabled'))
 390      {
 391        $this->getContext()->getLogger()->info('{sfAction} change template to "'.$name.'"');
 392      }
 393  
 394      $this->getResponse()->setParameter($this->getModuleName().'_'.$this->getActionName().'_template', $name, 'symfony/action/view');
 395    }
 396  
 397    /**
 398     * Gets the name of the alternate template for this sfAction.
 399     *
 400     * WARNING: It only returns the template you set with the setTemplate() method,
 401     *          and does not return the template that you configured in your view.yml.
 402     *
 403     * See 'Naming Conventions' in the 'Symfony View' documentation.
 404     *
 405     * @return string Template name. Returns null if no template has been set within the action
 406     */
 407    public function getTemplate()
 408    {
 409      return $this->getResponse()->getParameter($this->getModuleName().'_'.$this->getActionName().'_template', null, 'symfony/action/view');
 410    }
 411  
 412    /**
 413     * Sets an alternate layout for this sfAction.
 414     *
 415     * To de-activate the layout, set the layout name to false.
 416     *
 417     * To revert the layout to the one configured in the view.yml, set the template name to null.
 418     *
 419     * @param mixed Layout name or false to de-activate the layout
 420     */
 421    public function setLayout($name)
 422    {
 423      if (sfConfig::get('sf_logging_enabled'))
 424      {
 425        $this->getContext()->getLogger()->info('{sfAction} change layout to "'.$name.'"');
 426      }
 427  
 428      $this->getResponse()->setParameter($this->getModuleName().'_'.$this->getActionName().'_layout', $name, 'symfony/action/view');
 429    }
 430  
 431    /**
 432     * Gets the name of the alternate layout for this sfAction.
 433     *
 434     * WARNING: It only returns the layout you set with the setLayout() method,
 435     *          and does not return the layout that you configured in your view.yml.
 436     *
 437     * @return mixed Layout name. Returns null if no layout has been set within the action
 438     */
 439    public function getLayout()
 440    {
 441      return $this->getResponse()->getParameter($this->getModuleName().'_'.$this->getActionName().'_layout', null, 'symfony/action/view');
 442    }
 443  
 444    /**
 445     * Changes the default view class used for rendering the template associated with the current action.
 446     *
 447     * @param string View class name
 448     */
 449    public function setViewClass($class)
 450    {
 451      sfConfig::set('mod_'.strtolower($this->getModuleName()).'_view_class', $class);
 452    }
 453  }


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