Changeset 4074
- Timestamp:
- 05/22/07 17:02:28 (6 years ago)
- Files:
-
- plugins/sfSimplePageControllerPlugin/lib/helper/SimplePageHelper.php (modified) (1 diff)
- plugins/sfSimplePageControllerPlugin/lib/sfSimplePage.php (modified) (3 diffs)
- plugins/sfSimplePageControllerPlugin/lib/sfSimplePageArea.php (modified) (5 diffs)
- plugins/sfSimplePageControllerPlugin/lib/sfSimplePageConfigDriver.php (deleted)
- plugins/sfSimplePageControllerPlugin/lib/sfSimplePageConfigHandler.php (modified) (3 diffs)
- plugins/sfSimplePageControllerPlugin/lib/sfSimplePageDemoDriver.php (modified) (1 diff)
- plugins/sfSimplePageControllerPlugin/lib/sfSimplePageYmlDriver.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfSimplePageControllerPlugin/lib/helper/SimplePageHelper.php
r4068 r4074 10 10 function include_simplepage_area($area) 11 11 { 12 echo sfSimplePage::getInstance()->getArea($area)->render(); 12 if (sfSimplePage::hasInstance()) 13 { 14 echo sfSimplePage::getInstance()->getArea($area)->render(); 15 } 13 16 } 14 17 15 18 function get_simplepage_area($area) 16 19 { 17 return sfSimplePage:: getInstance()->getArea($area)->render();20 return sfSimplePage::hasInstance() ? sfSimplePage::getInstance()->getArea($area)->render() : ''; 18 21 } plugins/sfSimplePageControllerPlugin/lib/sfSimplePage.php
r4068 r4074 9 9 10 10 /** 11 * Base class for pages. Extend it instead of sfActions if you want to have a page.11 * Manager class for pages. 12 12 * 13 13 * @author Matthias Nothhaft <php@mahono.com> 14 14 */ 15 class sfSimplePage extends sfAction 15 abstract class sfSimplePage 16 16 { 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; 18 28 19 29 /** … … 22 32 protected $areas = array(); 23 33 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 33 39 public static function getInstance() 34 40 { 35 return self::$instance; 41 $page = end(self::$instances); 42 return $page; 36 43 } 37 44 38 public function initialize($context)45 public static function hasInstance() 39 46 { 40 parent::initialize($context); 47 return !empty(self::$instances); 48 } 41 49 42 self::$instance = $this; 50 /** 51 * Mixin for actions. 52 */ 53 public static function actAsSimplePage($actionInstance, $driverClass = null) 54 { 55 $signature = self::getSignature($actionInstance); 43 56 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; 45 107 } 46 108 … … 62 124 63 125 /** 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. 65 127 * 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. 68 170 * 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. 70 181 * 71 * @ throws sfInitializationException72 * 73 * @ see sfAction182 * @param string The method name 183 * @param array The method arguments 184 * @return mixed The returned value of the called method 74 185 */ 75 public function execute()186 public function __call($method, $arguments) 76 187 { 77 sfLoader::loadHelpers(array('Partial', 'SimplePage'));188 $callback = array($this->actionInstance, $method); 78 189 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)) 90 191 { 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); 95 193 } 96 194 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)); 106 196 } 107 197 plugins/sfSimplePageControllerPlugin/lib/sfSimplePageArea.php
r4068 r4074 25 25 protected $contents = array(); 26 26 protected $autoLabelCounter = 1; 27 28 protected $hasAction = false; 29 protected $hasComponent = false; 30 protected $hasPartial = false; 27 31 28 32 public function __construct($areaName) … … 53 57 } 54 58 59 $this->hasAction = true; 55 60 return $label; 56 61 } … … 71 76 ); 72 77 78 $this->hasComponent = true; 73 79 return $label; 74 80 } … … 88 94 ); 89 95 96 $this->hasPartial = true; 90 97 return $label; 91 98 } … … 96 103 public function render() 97 104 { 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 } 100 115 101 116 $output = ''; plugins/sfSimplePageControllerPlugin/lib/sfSimplePageConfigHandler.php
r4073 r4074 41 41 $code[] = "// date: " . date('Y-m-d H:i:s'); 42 42 $code[] = ''; 43 $code[] = "\$page = sfSimplePage::getInstance();";44 43 45 if ( !empty($pageConfig['driver']))44 if (empty($pageConfig['children'])) 46 45 { 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.'); 56 47 } 57 48 58 // compile data59 $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 {77 49 if (!empty($pageConfig['layout']['template'])) 78 50 { … … 83 55 { 84 56 $code[] = ''; 85 $code[] = "\$area = \$ page->getArea('{$areaId}');";57 $code[] = "\$area = \$this->getArea('{$areaId}');"; 86 58 87 59 foreach ((array) $children as $childId => $conf) … … 125 97 } 126 98 } 127 }128 99 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; 136 103 } 137 104 plugins/sfSimplePageControllerPlugin/lib/sfSimplePageDemoDriver.php
r4073 r4074 16 16 * @author Matthias Nothhaft <php@mahono.com> 17 17 */ 18 class sfSimplePageDemoDriver extends sfSimplePage ConfigDriver18 class sfSimplePageDemoDriver extends sfSimplePage 19 19 { 20 20 protected function initialize()