Show
Ignore:
Timestamp:
02/23/06 20:09:13 (6 years ago)
Author:
john
Message:

added in phpDoc commenting tests docs - Walt

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/action_controller.php

    r128 r138  
    11<?php 
    2 # $Id$ 
    3 # 
    4 # Copyright (c) 2005 John Peterson 
    5 # 
    6 # Permission is hereby granted, free of charge, to any person obtaining 
    7 # a copy of this software and associated documentation files (the 
    8 # "Software"), to deal in the Software without restriction, including 
    9 # without limitation the rights to use, copy, modify, merge, publish, 
    10 # distribute, sublicense, and/or sell copies of the Software, and to 
    11 # permit persons to whom the Software is furnished to do so, subject to 
    12 # the following conditions: 
    13 # 
    14 # The above copyright notice and this permission notice shall be 
    15 # included in all copies or substantial portions of the Software. 
    16 # 
    17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
    18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
    19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
    20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
    21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
    22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
    23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
    24  
     2/** 
     3 *  File containing ActionController class 
     4 * 
     5 *  (PHP 5) 
     6 * 
     7 *  @package PHPonTrax 
     8 *  @version $Id$ 
     9 *  @copyright (c) 2005 John Peterson 
     10 * 
     11 *  Permission is hereby granted, free of charge, to any person obtaining 
     12 *  a copy of this software and associated documentation files (the 
     13 *  "Software"), to deal in the Software without restriction, including 
     14 *  without limitation the rights to use, copy, modify, merge, publish, 
     15 *  distribute, sublicense, and/or sell copies of the Software, and to 
     16 *  permit persons to whom the Software is furnished to do so, subject to 
     17 *  the following conditions: 
     18 * 
     19 *  The above copyright notice and this permission notice shall be 
     20 *  included in all copies or substantial portions of the Software. 
     21 * 
     22 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
     23 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
     24 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
     25 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
     26 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
     27 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
     28 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
     29 */ 
     30 
     31/** 
     32 *  Action controller 
     33 * 
     34 *  <p><b>Filters</b></p> 
     35 * 
     36 *  <p>Filters enable controllers to run shared pre and post 
     37 *  processing code for its actions. These filters can be used to do 
     38 *  authentication, caching, or auditing before the intended action is 
     39 *  performed. Or to do localization or output compression after the 
     40 *  action has been performed.</p> 
     41 * 
     42 *  <p>Filters have access to the request, response, and all the 
     43 *  instance variables set by other filters in the chain or by the 
     44 *  action (in the case of after filters). Additionally, it's possible 
     45 *  for a pre-processing <samp>before_filter</samp> to halt the processing 
     46 *  before the intended action is processed by returning false or 
     47 *  performing a redirect or render.  This is especially useful for 
     48 *  filters like authentication where you're not interested in 
     49 *  allowing the action to be  performed if the proper credentials are 
     50 *  not in order.</p> 
     51 * 
     52 *  <p><b>Filter inheritance</b></p> 
     53 * 
     54 *  <p>Controller inheritance hierarchies share filters downwards, but 
     55 *  subclasses can also add new filters without affecting the 
     56 *  superclass. For example:</p> 
     57 * 
     58 *  <pre> 
     59 *   class BankController extends ActionController 
     60 *   { 
     61 *     $this->before_filter = audit(); 
     62 * 
     63 *     private function audit() { 
     64 *       <i>record the action and parameters in an audit log</i> 
     65 *     } 
     66 *   } 
     67 * 
     68 *   class VaultController extends BankController 
     69 *   { 
     70 *     $this->before_filter = verify_credentials(); 
     71 * 
     72 *     private function verify_credentials() { 
     73 *       <i>make sure the user is allowed into the vault</i> 
     74 *     } 
     75 *   } 
     76 *  </pre> 
     77 * 
     78 *  <p>Now any actions performed on the BankController will have the 
     79 *  audit method called before. On the VaultController, first the 
     80 *  audit method is called, then the verify_credentials method. If the 
     81 *  audit method returns false, then verify_credentials and the 
     82 *  intended action are never called.</p> 
     83 * 
     84 *  <p><b>Filter types</b></p> 
     85 * 
     86 *  <p>A filter can take one of three forms: method reference 
     87 *  (symbol), external class, or inline method (proc). The first is the 
     88 *  most common and works by referencing a protected or private method 
     89 *  somewhere in the inheritance hierarchy of the controller by use of 
     90 *  a symbol. In the bank example above, both BankController and 
     91 *  VaultController use this form.</p> 
     92 * 
     93 *  <p>Using an external class makes for more easily reused generic 
     94 *  filters, such as output compression. External filter classes are 
     95 *  implemented by having a static +filter+ method on any class and 
     96 *  then passing this class to the filter method. Example:</p> 
     97 * 
     98 *  <pre> 
     99 *   class OutputCompressionFilter 
     100 *   { 
     101 *     static functionfilter(controller) { 
     102 *       controller.response.body = compress(controller.response.body) 
     103 *     } 
     104 *   } 
     105 * 
     106 *   class NewspaperController extends ActionController 
     107 *   { 
     108 *     $this->after_filter = OutputCompressionFilter; 
     109 *   } 
     110 *  </pre> 
     111 * 
     112 *  <p>The filter method is passed the controller instance and is 
     113 *  hence granted access to all aspects of the controller and can 
     114 *  manipulate them as it sees fit.</p> 
     115 * 
     116 *  <p>The inline method (using a proc) can be used to quickly do 
     117 *  something small that doesn't require a lot of explanation.  Or 
     118 *  just as a quick test. It works like this:</p> 
     119 * 
     120 *  <pre> 
     121 *   class WeblogController extends ActionController 
     122 *   { 
     123 *     before_filter { |controller| false if controller.params["stop_action"] } 
     124 *   } 
     125 *  </pre> 
     126 * 
     127 *  <p>As you can see, the block expects to be passed the controller 
     128 *  after it has assigned the request to the internal variables.  This 
     129 *  means that the block has access to both the request and response 
     130 *  objects complete with convenience methods for params, session, 
     131 *  template, and assigns. Note: The inline method doesn't strictly 
     132 *  have to be a block; any object that responds to call and returns 1 
     133 *  or -1 on arity will do (such as a Proc or an Method object).</p> 
     134 * 
     135 *  <p><b>Filter chain skipping</b></p> 
     136 * 
     137 *  <p>Some times its convenient to specify a filter chain in a superclass  
     138 *  that'll hold true for the majority of the subclasses, but not necessarily 
     139 *  all of them. The subclasses that behave in exception can then specify 
     140 *  which filters they would like to be relieved of. Examples</p> 
     141 * 
     142 *  <pre> 
     143 *   class ApplicationController extends ActionController 
     144 *   { 
     145 *     $this->before_filter = authenticate(); 
     146 *   } 
     147 * 
     148 *   class WeblogController extends ApplicationController 
     149 *   { 
     150 *      // will run the authenticate() filter 
     151 *   } 
     152 *  </pre> 
     153 * 
     154 *  <p><b>Filter conditions</b></p> 
     155 * 
     156 *  <p>Filters can be limited to run for only specific actions. This 
     157 *  can be expressed either by listing the actions to exclude or 
     158 *  the actions to include when executing the filter. Available 
     159 *  conditions are +:only+ or +:except+, both of which accept an 
     160 *  arbitrary number of method references. For example:</p> 
     161 * 
     162 *  <pre> 
     163 *   class Journal extends ActionController 
     164 *   { 
     165 *     // only require authentication if the current action is edit or delete 
     166 *     before_filter :authorize, :only => [ :edit, :delete ] 
     167 *     
     168 *     private function authorize() { 
     169 *      // redirect to login unless authenticated 
     170 *     } 
     171 *   } 
     172 *  </pre> 
     173 *  
     174 *  <p>When setting conditions on inline method (proc) filters the 
     175 *  condition must come first and be placed in parentheses.</p> 
     176 * 
     177 *  <pre> 
     178 *   class UserPreferences extends ActionController 
     179 *   { 
     180 *     before_filter(:except => :new) { ? some proc ... } 
     181 *  * ... 
     182 *   } 
     183 *  </pre> 
     184 * 
     185 *  @package PHPonTrax 
     186 */ 
    25187class ActionController { 
    26188 
     
    340502    } 
    341503 
     504    /** 
     505     *  Execute the before filters 
     506     */ 
    342507    function execute_before_filters() { 
    343508        if(count($this->controller_object->before_filters) > 0) {  
     
    350515    } 
    351516 
     517    /** 
     518     *  Append a before filter to the filter chain 
     519     * 
     520     *  @param mixed $filter_function_name  String with the name of 
     521     *  one filter function, or array of strings with the names of 
     522     *  several filter functions. 
     523     */ 
    352524    function add_before_filter($filter_function_name) { 
    353525        if(is_string($filter_function_name) && !empty($filter_function_name)) { 
     
    374546    } 
    375547 
     548 
     549    /** 
     550     *  Append an after filter to the filter chain 
     551     * 
     552     *  @param mixed $filter_function_name  String with the name of 
     553     *  one filter function, or array of strings with the names of 
     554     *  several filter functions. 
     555     */ 
    376556    function add_after_filter($filter_function_name) { 
    377557        if(is_string($filter_function_name) && !empty($filter_function_name)) { 
     
    394574    } 
    395575 
     576    /** 
     577     *  Rendering partials 
     578     *  
     579     *  <p>Partial rendering is most commonly used together with Ajax 
     580     *  calls that only update one or a few elements on a page without 
     581     *  reloading. Rendering of partials from the controller makes it 
     582     *  possible to use the same partial template in both the 
     583     *  full-page rendering (by calling it from within the template) 
     584     *  and when sub-page updates happen (from the controller action 
     585     *  responding to Ajax calls). By default, the current layout is 
     586     *  not used.</p> 
     587     * 
     588     *  <ul> 
     589     *    <li><samp>render_partial("win");</samp><br> 
     590     *      Renders the partial 
     591     *      located at app/views/controller/_win.r(html|xml)</li> 
     592     * 
     593     *    <li><samp>render_partial("win", 
     594     *       array("locals" => array("name" => "david")));</samp><br> 
     595     *      Renders the same partial but also makes a local variable 
     596     *      available to it</li> 
     597     *      
     598     *    <li><samp>render_partial("win", 
     599     *      array("collection" => array(...)));</samp><br> 
     600     *      Renders a collection of the same partial by making each 
     601     *      element of the collection available through the local variable 
     602     *      "win" as it builds the complete response </li> 
     603     * 
     604     *    <li><samp>render_partial("win", array("collection" => @wins, 
     605     *      "spacer_template" => "win_divider"));</samp><br> 
     606     *      Renders the same collection of partials, but also renders 
     607     *      the win_divider partial in between each win partial.</li> 
     608     *  </ul> 
     609     *  @param string $path Path to file containing partial view 
     610     *  @param string[] $options Options array 
     611     */ 
    396612    function render_partial($path, $options = array()) { 
    397613        if(strstr($path, "/")) { 
     
    479695    } 
    480696 
     697    /** 
     698     *  Raise an ActionControllerError exception 
     699     * 
     700     *  @param string $error_message Error message 
     701     *  @param string $error_heading Error heading 
     702     *  @param string $error_code    Error code 
     703     *  @throws ActionControllerError 
     704     */ 
    481705    function raise($error_message, $error_heading, $error_code = "404") { 
    482706        throw new ActionControllerError("Error Message: ".$error_message, $error_heading, $error_code); 
    483707    } 
    484708 
     709    /** 
     710     *  Generate an HTML page describing an error 
     711     */ 
    485712    function process_with_exception(&$exception) { 
    486713        $error_code = $exception->error_code;