Development

Changeset 4074

You must first sign up to be able to contribute.

Changeset 4074

Show
Ignore:
Timestamp:
05/22/07 17:02:28 (6 years ago)
Author:
mahono
Message:

refactored almost everything, new driver concept, instead of providing a page base class there is an action mixin now: actAsSimplePage()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfSimplePageControllerPlugin/lib/helper/SimplePageHelper.php

    r4068 r4074  
    1010function include_simplepage_area($area) 
    1111{ 
    12   echo sfSimplePage::getInstance()->getArea($area)->render(); 
     12  if (sfSimplePage::hasInstance()) 
     13  { 
     14    echo sfSimplePage::getInstance()->getArea($area)->render(); 
     15  } 
    1316} 
    1417 
    1518function get_simplepage_area($area) 
    1619{ 
    17   return sfSimplePage::getInstance()->getArea($area)->render()
     20  return sfSimplePage::hasInstance() ? sfSimplePage::getInstance()->getArea($area)->render() : ''
    1821} 
  • plugins/sfSimplePageControllerPlugin/lib/sfSimplePage.php

    r4068 r4074  
    99 
    1010/** 
    11  * Base class for pages. Extend it instead of sfActions if you want to have a page
     11 * Manager class for pages
    1212 * 
    1313 * @author Matthias Nothhaft <php@mahono.com> 
    1414 */ 
    15 class sfSimplePage extends sfAction 
     15abstract class sfSimplePage 
    1616{ 
    17   protected static $instance = null; 
     17  protected static $defaultDriverClass = 'sfSimplePageYmlDriver'; 
     18  protected static $instances = array(); 
     19 
     20  /** 
     21   * Instance of the page action 
     22   */ 
     23  protected $actionInstance = null; 
     24  protected $isPageRequest = false; 
     25  protected $pageId; 
     26  protected $pageExtension; 
     27  protected $pageOffset; 
    1828 
    1929  /** 
     
    2232  protected $areas = array(); 
    2333 
    24   /** 
    25    * Returns the instance of the current page. 
    26    * 
    27    * NOTE: This could lead to unexpected results if you forward to another page! 
    28    *       Page forwarding is not tested. 
    29    * 
    30    * @TODO add support for forward by integrating a stack (possibly sfActionStack??) !? 
    31    * @return Instance of the current page. 
    32    */ 
     34  public static function getSignature($actionInstance) 
     35  { 
     36    return $actionInstance->getModuleName() . '/' . $actionInstance->getActionName(); 
     37  } 
     38 
    3339  public static function getInstance() 
    3440  { 
    35     return self::$instance; 
     41    $page = end(self::$instances); 
     42    return $page; 
    3643  } 
    3744 
    38   public function initialize($context
     45  public static function hasInstance(
    3946  { 
    40     parent::initialize($context); 
     47    return !empty(self::$instances); 
     48  } 
    4149 
    42     self::$instance = $this; 
     50  /** 
     51   * Mixin for actions. 
     52   */ 
     53  public static function actAsSimplePage($actionInstance, $driverClass = null) 
     54  { 
     55    $signature = self::getSignature($actionInstance); 
    4356 
    44     return true; 
     57    if (empty(self::$instances[$signature])) 
     58    { 
     59      if (is_null($driverClass)) 
     60      { 
     61        $driverClass = sfConfig::get('sf_plugin_simplepage_driver_class', self::$defaultDriverClass); 
     62      } 
     63 
     64      self::$instances[$signature] = new $driverClass($actionInstance); 
     65      self::$instances[$signature]->initialize(); 
     66    } 
     67 
     68    return self::$instances[$signature]; 
     69  } 
     70 
     71  /** 
     72   * Mixin for actions 
     73   */ 
     74  public static function getSimplePage($actionInstance) 
     75  { 
     76    $signature = self::getSignature($actionInstance); 
     77 
     78    if (!empty(self::$instances[$signature]) && self::$instances[$signature] instanceof sfSimplePage) 
     79    { 
     80      return self::$instances[$signature]; 
     81    } 
     82 
     83    throw sfException('You have to call $this->actAsSimplePage() first.'); 
     84  } 
     85 
     86  public function __construct($actionInstance) 
     87  { 
     88    $this->actionInstance = $actionInstance; 
     89  } 
     90 
     91  /** 
     92   * Initialize the page config driver. 
     93   * You have to implement this method in order to 
     94   * - get from request parameters: pageId, pageExtension, pageOffset 
     95   * - build the page areas 
     96   */ 
     97  abstract protected function initialize(); 
     98 
     99  public function setActionInstance($actionInstance) 
     100  { 
     101    $this->actionInstance = $actionInstance; 
     102  } 
     103 
     104  public function getActionInstance() 
     105  { 
     106    return $this->actionInstance; 
    45107  } 
    46108 
     
    62124 
    63125  /** 
    64    * Dispatches to the action defined by the 'action' parameter of the sfRequest object
     126   * Check whether a page with a given page id exists
    65127   * 
    66    * This method try to execute the executeXXX() method of the current object where XXX is the 
    67    * defined action name. 
     128   * @param string page identifier 
     129   * @return true if the given page exists, false if not 
     130   */ 
     131  public function pageExists($pageId) 
     132  { 
     133    return true; 
     134  } 
     135 
     136  /** 
     137   * Returns whether a page was requested or not. 
     138   * @return bool 
     139   */ 
     140  public function isPageRequest() 
     141  { 
     142    return $this->isPageRequest; 
     143  } 
     144 
     145  public function getPageId() 
     146  { 
     147    return $this->pageId; 
     148  } 
     149 
     150  /** 
     151   * Returns file extension of the requested page, e.g. 'html' 
     152   * @return string | false if no extension 
     153   */ 
     154  public function getPageExtension() 
     155  { 
     156    return $this->pageExtension; 
     157  } 
     158 
     159  /** 
     160   * For pagination 
     161   */ 
     162  public function getPageOffset() 
     163  { 
     164    return $this->pageOffset; 
     165  } 
     166 
     167  /** 
     168   * Set whether this request is a page request or not. 
     169   * Should not be used. 
    68170   * 
    69    * @return string A string containing the view name associated with this action 
     171   * @param boolean 
     172   * @see isPageRequest() 
     173   */ 
     174  protected function setPageRequest($status) 
     175  { 
     176    $this->isPageRequest = $status; 
     177  } 
     178 
     179  /** 
     180   * Calls methods callable in the page action. 
    70181   * 
    71    * @throws sfInitializationException 
    72    * 
    73    * @see sfAction 
     182   * @param string The method name 
     183   * @param array  The method arguments 
     184   * @return mixed The returned value of the called method 
    74185   */ 
    75   public function execute(
     186  public function __call($method, $arguments
    76187  { 
    77     sfLoader::loadHelpers(array('Partial', 'SimplePage')); 
     188    $callback = array($this->actionInstance, $method); 
    78189 
    79     $actionName = ucfirst($this->getActionName()); 
    80  
    81     require sfConfigCache::getInstance()->checkConfig( 
    82       sfConfig::get('sf_app_module_dir_name'). '/' . $this->getModuleName(). '/' . 
    83       sfConfig::get('sf_app_module_config_dir_name').'/page' . $actionName . '.yml', 
    84       true 
    85     ); 
    86  
    87     // dispatch action 
    88     $actionToRun = 'execute' . $actionName; 
    89     if (!is_callable(array($this, $actionToRun))) 
     190    if (is_callable($callback)) 
    90191    { 
    91       // action not found 
    92       $error = 'sfAction initialization failed for module "%s", action "%s". You must create a "%s" method.'; 
    93       $error = sprintf($error, $this->getModuleName(), $this->getActionName(), $actionToRun); 
    94       throw new sfInitializationException($error); 
     192      return call_user_func_array($callback, $arguments); 
    95193    } 
    96194 
    97     if (sfConfig::get('sf_logging_enabled')) 
    98     { 
    99       $this->getContext()->getLogger()->info('{sfAction} call "'.get_class($this).'->'.$actionToRun.'()'.'"'); 
    100     } 
    101  
    102     // run action 
    103     $ret = $this->$actionToRun(); 
    104  
    105     return $ret; 
     195    throw new sfException(sprintf('Call to undefined method ' . get_class($this) . '::%s', $method)); 
    106196  } 
    107197 
  • plugins/sfSimplePageControllerPlugin/lib/sfSimplePageArea.php

    r4068 r4074  
    2525  protected $contents = array(); 
    2626  protected $autoLabelCounter = 1; 
     27 
     28  protected $hasAction = false; 
     29  protected $hasComponent = false; 
     30  protected $hasPartial = false; 
    2731 
    2832  public function __construct($areaName) 
     
    5357    } 
    5458 
     59    $this->hasAction = true; 
    5560    return $label; 
    5661  } 
     
    7176    ); 
    7277 
     78    $this->hasComponent = true; 
    7379    return $label; 
    7480  } 
     
    8894    ); 
    8995 
     96    $this->hasPartial = true; 
    9097    return $label; 
    9198  } 
     
    96103  public function render() 
    97104  { 
    98     $context = sfContext::getInstance(); 
    99     $controller = $context->getController(); 
     105    if ($this->hasAction) 
     106    { 
     107      $context = sfContext::getInstance(); 
     108      $controller = $context->getController(); 
     109    } 
     110 
     111    if ($this->hasComponent || $this->hasPartial) 
     112    { 
     113      sfLoader::loadHelpers(array('Partial')); 
     114    } 
    100115 
    101116    $output = ''; 
  • plugins/sfSimplePageControllerPlugin/lib/sfSimplePageConfigHandler.php

    r4073 r4074  
    4141    $code[] = "// date: " . date('Y-m-d H:i:s'); 
    4242    $code[] = ''; 
    43     $code[] = "\$page = sfSimplePage::getInstance();"; 
    4443 
    45     if (!empty($pageConfig['driver'])) 
     44    if (empty($pageConfig['children'])) 
    4645    { 
    47       $this->compileForDriver($pageConfig, $code); 
    48     } 
    49     elseif (!empty($pageConfig['children'])) 
    50     { 
    51       $this->compileForYml($pageConfig, $code); 
    52     } 
    53     else 
    54     { 
    55       throw new sfConfigurationException('You have to either set driver or children in your page configuration.'); 
     46      throw new sfConfigurationException('You have to set children in your page configuration.'); 
    5647    } 
    5748 
    58     // compile data 
    59     $retval = implode("\n", $code); 
    60     return $retval; 
    61   } 
    62  
    63   /** 
    64    * Compiles the config for use with a custom driver. 
    65    */ 
    66   protected function compileForDriver($pageConfig, &$code) 
    67   { 
    68     $code[] = "\$context = sfContext::getInstance();"; 
    69     $code[] = "\$driverInstance = new {$pageConfig['driver']}(\$context, \$page);"; 
    70   } 
    71  
    72   /** 
    73    * Compiles the page config from yml settings. 
    74    */ 
    75   protected function compileForYml($pageConfig, &$code) 
    76   { 
    7749    if (!empty($pageConfig['layout']['template'])) 
    7850    { 
     
    8355    { 
    8456      $code[] = ''; 
    85       $code[] = "\$area = \$page->getArea('{$areaId}');"; 
     57      $code[] = "\$area = \$this->getArea('{$areaId}');"; 
    8658 
    8759      foreach ((array) $children as $childId => $conf) 
     
    12597      } 
    12698    } 
    127   } 
    12899 
    129     // for debugging only 
    130   public function debugVar($var) 
    131   { 
    132     echo '<pre style="font-family:serif; font-size: 12px">'; 
    133     $e = print_r($var, true); 
    134     echo $e; 
    135     echo "</pre>"; 
     100    // compile data 
     101    $retval = implode("\n", $code); 
     102    return $retval; 
    136103  } 
    137104 
  • plugins/sfSimplePageControllerPlugin/lib/sfSimplePageDemoDriver.php

    r4073 r4074  
    1616 * @author Matthias Nothhaft <php@mahono.com> 
    1717 */ 
    18 class sfSimplePageDemoDriver extends sfSimplePageConfigDriver 
     18class sfSimplePageDemoDriver extends sfSimplePage 
    1919{ 
    2020  protected function initialize()