Apache Zeta Components Manual :: File Source for controller.php

Source for file controller.php

Documentation is available at controller.php

  1. <?php
  2. /**
  3.  * File containing the ezcMvcController class
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @version //autogentag//
  24.  * @filesource
  25.  * @package MvcTools
  26.  */
  27.  
  28. /**
  29.  * Interface defining controller classes.
  30.  *
  31.  * Controllers process the client's request and returns variables usable by the
  32.  * view-manager in an instance of an ezcMvcResult.  Controllers should not
  33.  * access request variables directly but should use the passed ezcMvcRequest.
  34.  * The process is done through the createResult() method, but is not limited to
  35.  * use protected nor private methods. The result of running a controller is an
  36.  * instance of ezcMvcResult.
  37.  *
  38.  * @package MvcTools
  39.  * @version //autogentag//
  40.  * @mainclass
  41.  */
  42. abstract class ezcMvcController
  43. {
  44.     /**
  45.      * Contains the action to run
  46.      * @var string 
  47.      */
  48.     protected $action;
  49.  
  50.     /**
  51.      * Contains the original request
  52.      * @var ezcMvcRequest 
  53.      */
  54.     protected $request;
  55.  
  56.     /**
  57.      * Holds the properties of this class.
  58.      *
  59.      * @var array(string=>mixed) 
  60.      */
  61.     private $properties array();
  62.  
  63.     /**
  64.      * Holds the router associated with the action
  65.      *
  66.      * @var ezcMvcRouter 
  67.      */
  68.     private $router;
  69.  
  70.     /**
  71.      * Creates a new controller object and sets all the request variables as class variables.
  72.      *
  73.      * @throws ezcMvcControllerException if the action method is empty
  74.      * @param string        $action 
  75.      * @param ezcMvcRequest $request 
  76.      */
  77.     public function __construct$actionezcMvcRequest $request )
  78.     {
  79.         if ezcBase::inDevMode(&& !is_string$action || strlen$action == ) )
  80.         {
  81.             throw new ezcMvcControllerException"The '" get_class$this "' controller requires an action." );
  82.         }
  83.         $this->action = $action;
  84.         $this->setRequestVariables$request );
  85.     }
  86.  
  87.     /**
  88.      * Sets the property $name to $value, all properties are readonly though so
  89.      * an exception is thrown for every set.
  90.      *
  91.      * @throws ezcBasePropertyPermissionException if a read-only property is
  92.      *          tried to be modified.
  93.      *
  94.      * @param string $name 
  95.      * @param mixed $value 
  96.      * @ignore
  97.      */
  98.     public function __set$name$value )
  99.     {
  100.         throw new ezcBasePropertyPermissionException$nameezcBasePropertyPermissionException::READ );
  101.     }
  102.  
  103.     /**
  104.      * Returns the value of the property $name.
  105.      *
  106.      * @throws ezcBasePropertyNotFoundException if the property does not exist.
  107.      * @param string $name 
  108.      * @ignore
  109.      */
  110.     public function __get$name )
  111.     {
  112.         if isset$this->properties[$name) )
  113.         {
  114.             return $this->properties[$name];
  115.         }
  116.  
  117.         throw new ezcBasePropertyNotFoundException$name );
  118.     }
  119.  
  120.     /**
  121.      * Returns true if the property $name is set, otherwise false.
  122.      *
  123.      * @param string $name 
  124.      * @return bool 
  125.      * @ignore
  126.      */
  127.     public function __isset$name )
  128.     {
  129.         return array_key_exists$name$this->properties );
  130.     }
  131.  
  132.     /**
  133.      * Sets the router associated with this request.
  134.      *
  135.      * @param ezcMvcRouter $router 
  136.      */
  137.     public function setRouterezcMvcRouter $router )
  138.     {
  139.         $this->router $router;
  140.     }
  141.  
  142.     /**
  143.      * Returns the router associated with this request.
  144.      *
  145.      * @return ezcMvcRouter 
  146.      */
  147.     public function getRouter()
  148.     {
  149.         return $this->router;
  150.     }
  151.  
  152.     /**
  153.      * Loops over all the variables in the request, and sets them as object properties.
  154.      *
  155.      * @param ezcMvcRequest $request 
  156.      */
  157.     protected function setRequestVariablesezcMvcRequest $request )
  158.     {
  159.         foreach $request->variables as $key => $value )
  160.         {
  161.             $this->properties[$key$value;
  162.         }
  163.         $this->request = $request;
  164.     }
  165.  
  166.     /**
  167.      * Creates a method name to call from an $action name.
  168.      *
  169.      * @param string $action 
  170.      * @return string 
  171.      */
  172.     public static function createActionMethodName$action )
  173.     {
  174.         $actionMethod 'do' preg_replace'@[^A-Za-z]@'''preg_replace'@[A-Za-z]+@e''ucfirst( "\\0" )'$action ) );
  175.         return $actionMethod;
  176.     }
  177.  
  178.     /**
  179.      * Runs the controller to process the query and return variables usable
  180.      * to render the view.
  181.      *
  182.      * @throws ezcMvcActionNotFoundException if the action method could not be found
  183.      * @return ezcMvcResult|ezcMvcInternalRedirect
  184.      */
  185.     public function createResult()
  186.     {
  187.         $actionMethod self::createActionMethodName$this->action );
  188.  
  189.         if method_exists$this$actionMethod ) )
  190.         {
  191.             return $this->$actionMethod();
  192.         }
  193.         throw new ezcMvcActionNotFoundException$this->action );
  194.     }
  195. }
  196. ?>
Documentation generated by phpDocumentor 1.4.3