PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/library/Xedin/Controller/Abstract.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 395 lines | 244 code | 83 blank | 68 comment | 34 complexity | 2dc8a2b7098c3c95ad3fef16c3bb86db MD5 | raw file
  1. <?php
  2. /**
  3. * @author Xedin Unknown <xedin.unknown+xdn@gmail.com>
  4. * @version 0.1.1
  5. * + Fixed errors related to retreiving config values
  6. */
  7. abstract class Xedin_Controller_Abstract extends Zend_Controller_Request_Abstract {
  8. /** @var Xedin_Controller_Request_Http */
  9. protected $_request;
  10. protected $_originalSegments;
  11. protected $_segments;
  12. protected $_sectionKeys = array();
  13. protected $_isExplicitKeyOverrides = false;
  14. protected $_originalModule;
  15. protected $_originalController;
  16. protected $_actualModule;
  17. protected $_isRewritten = false;
  18. protected $_isAdmin;
  19. /** @var Xedin_Controller_Abstract */
  20. protected $_currentController;
  21. protected $_urlMap;
  22. const XML_PATH_ROUTERS = 'global/routers';
  23. const XML_PATH_URI_SEGMENTS_DEFAULT = 'global/uri/segments/default';
  24. const XML_PATH_URI_SEGMENTS_MAP = 'global/uri/segments/map';
  25. const XML_PATH_URI_ADMIN_MODULE_NAME = 'global/uri/admin_module_name';
  26. const URL_SEGMENT_SEPARATOR = '/';
  27. const XML_SEGMENT_SEPARATOR = '_';
  28. const XML_NUMERIC_TAG_PREFIX = '_';
  29. public function __construct() {
  30. $this->_originalSegments = $this->parsePathInfo();
  31. $this->_segments = $this->getNormalizedSegments();
  32. $this->setSectionKeys( $this->getUrlMap() )
  33. ->setParams( $this->_getNormalizedParams() );
  34. $this->_originalModule = $this->getModuleName();
  35. $this->_originalController = $this->getControllerName();
  36. $this->rewrite();
  37. }
  38. public function getUrlMap($index = null, $default = null) {
  39. if( !$this->_urlMap ) {
  40. $this->_urlMap = $this->_getUrlMap();
  41. }
  42. if( is_null($index) ) {
  43. return $this->_urlMap;
  44. }
  45. if( !isset($this->_urlMap[$index]) ) {
  46. return $default;
  47. }
  48. return $this->_urlMap[$index];
  49. }
  50. protected function _getUrlMap() {
  51. $originalMap = (array)Hub::getConfig(self::XML_PATH_URI_SEGMENTS_MAP);
  52. $urlMap = array();
  53. foreach( $originalMap as $_name => $_info ) {
  54. $_info = (array)$_info;
  55. if( !preg_match($_info['rule'], $this->getRequest()->getPathInfo()) ) {
  56. continue;
  57. }
  58. foreach( $_info['segments'] as $_index => $_segmentName ) {
  59. $_index = trim($_index, ' _');
  60. $urlMap[$_index] = $_segmentName;
  61. }
  62. return $urlMap;
  63. }
  64. }
  65. public function getNormalizedSegments($segment = null, $default = null) {
  66. if( !$this->_segments ) {
  67. $this->_segments = $this->_getNormalizedSegments();
  68. }
  69. if( is_null($segment) ) {
  70. return $this->_segments;
  71. }
  72. if( !isset($this->_segments[$segment]) ) {
  73. return $default;
  74. }
  75. return $this->_segments[$segment];
  76. }
  77. public function parsePathInfo($pathInfo = null) {
  78. if( is_null($pathInfo) ) {
  79. $pathInfo = $this->getRequest()->getPathInfo();
  80. }
  81. $pathInfo = trim($pathInfo, ' /' . self::XML_NUMERIC_TAG_PREFIX);
  82. return explode(self::URL_SEGMENT_SEPARATOR, $pathInfo);
  83. }
  84. public function getSectionKeys($index = null, $default = null) {
  85. if( is_null($index) ) {
  86. return $this->_sectionKeys;
  87. }
  88. if( !isset($this->_sectionKeys[$index]) ) {
  89. return $default;
  90. }
  91. return $this->_sectionKeys[$index];
  92. }
  93. public function hasSectionKeys($index = null) {
  94. if( is_null($index) ) {
  95. return (bool)count($this->getSectionKeys());
  96. }
  97. if( is_numeric($index) ) {
  98. $index = intval($index);
  99. return isset($this->_sectionKeys[$index]);
  100. }
  101. return in_array($index, $this->_sectionKeys);
  102. }
  103. /**
  104. *
  105. * @param type $index
  106. * @param type $value
  107. * @return \Xedin_Controller_Request_Http
  108. */
  109. public function setSectionKeys($index, $value = null) {
  110. if( is_null($value) && is_array($index) ) {
  111. foreach( $index as $_index => $_value ) {
  112. $this->setSectionKeys($_index, $_value);
  113. }
  114. return $this;
  115. }
  116. $index = intval($index);
  117. $this->_sectionKeys[$index] = $value;
  118. return $this;
  119. }
  120. /**
  121. * Makes sure the URI segments are in the necessary order.
  122. * Applies defaults.
  123. *
  124. * @param type $segments
  125. * @return type
  126. */
  127. protected function _getNormalizedSegments($segments = null) {
  128. if( is_null($segments) ) {
  129. $segments = $this->getOriginalSegments();
  130. }
  131. return $segments;
  132. }
  133. /**
  134. * Parses a URL path (everything between base path and query) into an array
  135. * according to the map specified in section keys. Example:
  136. *
  137. * URL: "one/two/three/four/five/six/7"
  138. * Result:
  139. * array(5) {
  140. * ["module"]=>
  141. * string(3) "one"
  142. * ["controller"]=>
  143. * string(3) "two"
  144. * ["action"]=>
  145. * string(5) "three"
  146. * ["four"]=>
  147. * string(4) "five"
  148. * ["six"]=>
  149. * string(1) "7"
  150. * }
  151. *
  152. * @param type $pathInfo
  153. * @param type $default
  154. * @return type
  155. */
  156. protected function _getNormalizedParams($pathInfo = null, $default = null) {
  157. if( is_null($pathInfo) ) {
  158. $pathInfo = $this->getRequest()->getPathInfo();
  159. }
  160. if( is_string($pathInfo) ) {
  161. $pathInfo = $this->parsePathInfo($pathInfo);
  162. }
  163. /* @var array $sections The normalized segments in key => value pairs */
  164. $sections = array();
  165. foreach( $pathInfo as $_index => $_segment ) {
  166. if( !$this->hasSectionKeys($_index) ) {
  167. $this->setSectionKeys($_index+1, $_segment);
  168. $sections[$this->getSectionKeys($_index+1)] = $default;
  169. continue;
  170. }
  171. $sections[(string)$this->getSectionKeys($_index)] = $_segment;
  172. }
  173. $sections = array_merge($this->_getDefaultUriSegments(), $sections);
  174. return $sections;
  175. }
  176. protected function _getDefaultUriSegments() {
  177. // var_dump(Hub::getConfig('global/uri'));
  178. return $defaultSegments = (array)Hub::getConfig(self::XML_PATH_URI_SEGMENTS_DEFAULT);
  179. }
  180. public function isExplicitKeyOverrides($overrides = null) {
  181. if( is_null($overrides) ) {
  182. return $this->_isExplicitKeyOverrides;
  183. }
  184. $this->_isExplicitKeyOverrides = (bool)$overrides;
  185. }
  186. /**
  187. *
  188. * @return Xedin_Controller_Request_Http
  189. */
  190. public function getRequest() {
  191. if( !$this->_request ) {
  192. $this->_request = Hub::getLibrary('xedin_controller_request_http');
  193. }
  194. return $this->_request;
  195. }
  196. /**
  197. * @todo The Xedin vendor is supposed to be default, but configurable.
  198. * Right now, however, only controllers from the Xedin vendor modules will be dispatched.
  199. * @return \Xedin_Controller_Abstract
  200. */
  201. public function rewrite($frontName = null) {
  202. $routers = (array)Hub::getConfig( self::XML_PATH_ROUTERS );
  203. $moduleName = (is_null($frontName)) ? $this->getModuleName() : $frontName;
  204. foreach( $routers as $toModule => $_router ) {
  205. $_router = (array)$_router;
  206. if( (string)$_router['frontName'] == $moduleName ) {
  207. $this->setModuleName($toModule);
  208. $this->_actualModule = $_router['module'];
  209. $this->_isRewritten = true;
  210. return;
  211. }
  212. }
  213. $this->_actualModule = 'Xedin_' . ucfirst( $this->getModuleName() );
  214. return $this;
  215. }
  216. public function isRewritten() {
  217. return $this->_isRewritten;
  218. }
  219. public function getActualModuleName() {
  220. return $this->_actualModule;
  221. }
  222. public function getOriginalSegments($index=null) {
  223. if( is_null($index) ) {
  224. return $this->_originalSegments;
  225. }
  226. if( !isset($this->_originalSegments[$index]) ) {
  227. return null;
  228. }
  229. return $this->_originalSegments[$index];
  230. }
  231. /**
  232. * @return Xedin_Core_Controller_Abstract
  233. */
  234. public function getCurrentController() {
  235. return $this->_currentController;
  236. }
  237. protected function _beforeDispatch() {
  238. }
  239. protected function _afterDispatch() {
  240. }
  241. // public abstract function dispatch();
  242. public function redirect($url = '', $prepare = true, $params = array()) {
  243. header('Location: ' . $this->getRedirectUrl($url, $prepare, $params));
  244. exit();
  245. }
  246. public function getRedirectUrl($url = '', $prepare = true, $params = array()) {
  247. if( $prepare && !is_array($url) ) {
  248. $sections = explode('/', $url);
  249. }
  250. else {
  251. $sections = $url;
  252. }
  253. $redirectUrlSegments = array();
  254. $sectionKeys = $this->getSectionKeys();
  255. $i = 0;
  256. foreach( $sectionKeys as $_idx => $_key ) {
  257. if( isset($sections[$_idx]) ) {
  258. if( preg_match('!(\*)!', $sections[$_idx]) ) {
  259. $redirectUrlSegments[$i] = preg_replace('!(\*)!', $sections[$_idx], $this->getParam($sectionKeys[$_idx]));
  260. }
  261. else {
  262. $redirectUrlSegments[$i] = $sections[$_idx];
  263. }
  264. }
  265. $i++;
  266. }
  267. $additionalParams = array();
  268. foreach( $params as $_key => $_value ) {
  269. $additionalParams[] = $_key;
  270. $additionalParams[] = $_value;
  271. }
  272. $redirectUrlSegments = array_merge($redirectUrlSegments, $additionalParams);
  273. $url = implode('/', $redirectUrlSegments);
  274. if( $prepare ) {
  275. $url = $this->getUrl($url);
  276. }
  277. return $url;
  278. }
  279. public function setParams(array $array) {
  280. parent::setParams($array);
  281. $this->getRequest()->setParams($array);
  282. return $this;
  283. }
  284. public function getUrl($path = '') {
  285. return Hub::getUrl($path);
  286. }
  287. /**
  288. * If parameter specified and is not null, will set the request to be an admin request.
  289. * If no parameter specified, returns whether or not this is an admin request.
  290. * The first time this is called with no parameters, the controller will
  291. * autodetect whether or not it is handling an admin request.
  292. *
  293. * @see self::XML_PATH_URI_ADMIN_MODULE_NAME
  294. * @param null|bool $isAdmin Whether or not the request should be treated as an admin request.
  295. * @return \Xedin_Controller_Abstract|bool Either whether or not an admin controller has been invoked, or this instance.
  296. */
  297. public function isAdmin($isAdmin = null) {
  298. if( !is_null($isAdmin) ) {
  299. $this->_isAdmin = (bool)$isAdmin;
  300. return $this;
  301. }
  302. if( is_null($this->_isAdmin) ) {
  303. $isAdmin = $this->getModuleName() == $this->getAdminModuleName();
  304. $this->isAdmin($isAdmin);
  305. }
  306. return $this->_isAdmin;
  307. }
  308. public function getAdminModuleName() {
  309. return (string)Hub::getConfig(self::XML_PATH_URI_ADMIN_MODULE_NAME);
  310. }
  311. }
  312. ?>