From c07c304fab3f6080deef2920d82916c8405dadf3 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 22 Jun 2013 16:22:07 +0300 Subject: [PATCH] more autoloading --- ipf/admin/app.php | 2 +- ipf/admin/model.php | 6 +- ipf/admin/models/AdminLog.php | 18 +++--- ipf/admin/views.php | 4 +- ipf/http/url.php | 105 +++++++++++++++++----------------- ipf/template/tag/url.php | 5 +- 6 files changed, 72 insertions(+), 68 deletions(-) diff --git a/ipf/admin/app.php b/ipf/admin/app.php index 4e4d786..eaa4769 100644 --- a/ipf/admin/app.php +++ b/ipf/admin/app.php @@ -37,7 +37,7 @@ class IPF_Admin_App extends IPF_Application if ($ok) return true; else - return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL_urlForView('IPF_Admin_Views_Login')); + return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL::urlForView('IPF_Admin_Views_Login')); } static function GetAppModelFromSlugs($lapp, $lmodel) diff --git a/ipf/admin/model.php b/ipf/admin/model.php index e063a31..3e353bc 100644 --- a/ipf/admin/model.php +++ b/ipf/admin/model.php @@ -544,7 +544,7 @@ class IPF_Admin_Model $this->_afterAdd($item); $url = @$request->POST['ipf_referrer']; if ($url=='') - $url = IPF_HTTP_URL_urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel)); + $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel)); return new IPF_HTTP_Response_Redirect($url); } } @@ -585,7 +585,7 @@ class IPF_Admin_Model $o->delete(); $url = @$request->POST['ipf_referrer']; if ($url=='') - $url = IPF_HTTP_URL_urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel)); + $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel)); return new IPF_HTTP_Response_Redirect($url); } $context = array( @@ -627,7 +627,7 @@ class IPF_Admin_Model $this->_afterEdit($item); $url = @$request->POST['ipf_referrer']; if ($url=='') - $url = IPF_HTTP_URL_urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel)); + $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel)); return new IPF_HTTP_Response_Redirect($url); } } diff --git a/ipf/admin/models/AdminLog.php b/ipf/admin/models/AdminLog.php index 85d9acc..04aa978 100644 --- a/ipf/admin/models/AdminLog.php +++ b/ipf/admin/models/AdminLog.php @@ -6,7 +6,8 @@ class AdminLog extends BaseAdminLog const CHANGE = 2; const DELETION = 3; - public static function logAction($request, $object, $action_flag, $message=''){ + public static function logAction($request, $object, $action_flag, $message='') + { $log = new AdminLog(); $log->username = $request->user->username; $log->user_id = $request->user->id; @@ -18,27 +19,30 @@ class AdminLog extends BaseAdminLog $log->save(); } - public function is_addition(){ + public function is_addition() + { if ($this->action_flag==AdminLog::ADDITION) return true; return false; } - public function is_change(){ + public function is_change() + { if ($this->action_flag==AdminLog::CHANGE) return true; return false; } - public function is_deletion(){ + public function is_deletion() + { if ($this->action_flag==AdminLog::DELETION) return true; return false; } - public function GetAdminUrl(){ - return IPF_HTTP_URL_urlForView('IPF_Admin_Views_Index').IPF_Utils::appLabelByModel($this->object_class).'/'.strtolower($this->object_class).'/'.$this->object_id.'/'; + public function GetAdminUrl() + { + return IPF_HTTP_URL::urlForView('IPF_Admin_Views_Index').IPF_Utils::appLabelByModel($this->object_class).'/'.strtolower($this->object_class).'/'.$this->object_id.'/'; } - } diff --git a/ipf/admin/views.php b/ipf/admin/views.php index 85a0e1a..b451d06 100644 --- a/ipf/admin/views.php +++ b/ipf/admin/views.php @@ -251,7 +251,7 @@ function IPF_Admin_Views_ChangePassword($request, $match) $user->setPassword($form->cleaned_data['password1']); $user->save(); - return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL_urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel))); + return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel))); } } else $form = new IPF_Auth_Forms_ChangePassword(); @@ -282,7 +282,7 @@ function IPF_Admin_Views_Login($request, $match) if (!empty($request->REQUEST['next'])) $success_url = $request->REQUEST['next']; if (trim($success_url)=='') - $success_url = IPF_HTTP_URL_urlForView('IPF_Admin_Views_Index'); + $success_url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_Index'); if ($request->method == 'POST') { $form = new IPF_Auth_Forms_Login($request->POST); diff --git a/ipf/http/url.php b/ipf/http/url.php index d92c442..ea85026 100644 --- a/ipf/http/url.php +++ b/ipf/http/url.php @@ -32,65 +32,66 @@ class IPF_HTTP_URL } return '/'; } -} -function IPF_HTTP_URL_urlForView($view, $params=array(), $by_name=false, - $get_params=array(), $encoded=true) -{ - $action = IPF_HTTP_URL_reverse($view, $params, $by_name); - return IPF_HTTP_URL::generate($action, $get_params, $encoded); -} + public static function urlForView($view, $params=array(), $by_name=false, + $get_params=array(), $encoded=true) + { + $action = self::reverse($view, $params, $by_name); + return self::generate($action, $get_params, $encoded); + } -function IPF_HTTP_URL_reverse($view, $params=array(), $by_name=false) -{ - $regex = null; - - foreach (IPF::get('urls') as $url) { - $prefix = $url['prefix']; - foreach ($url['urls'] as $suburl){ - if ($suburl['func']==$view){ - $regex = $prefix.$suburl['regex']; - break; + public static function reverse($view, $params=array(), $by_name=false) + { + $regex = null; + + foreach (IPF::get('urls') as $url) { + $prefix = $url['prefix']; + foreach ($url['urls'] as $suburl){ + if ($suburl['func']==$view){ + $regex = $prefix.$suburl['regex']; + break; + } } + if ($regex!==null) + break; } - if ($regex!==null) - break; - } - if ($regex === null) { - throw new IPF_Exception('Error, the view: '.$view.' has not been found.'); + if ($regex === null) { + throw new IPF_Exception('Error, the view: '.$view.' has not been found.'); + } + $url = self::buildReverseUrl($regex, $params); + return IPF::get('app_base').$url; } - $url = IPF_HTTP_URL_buildReverseUrl($regex, $params); - return IPF::get('app_base').$url; -} -function IPF_HTTP_URL_buildReverseUrl($url_regex, $params=array()) -{ - $url_regex = str_replace('\\.', '.', $url_regex); - $url_regex = str_replace('\\-', '-', $url_regex); - $url = $url_regex; - $groups = '#\(([^)]+)\)#'; - $matches = array(); - preg_match_all($groups, $url_regex, $matches); - reset($params); - if (count($matches[0]) && count($matches[0]) == count($params)) { - // Test the params against the pattern - foreach ($matches[0] as $pattern) { - $in = current($params); - if (0 === preg_match('#'.$pattern.'#', $in)) { - throw new IPF_Exception('Error, param: '.$in.' is not matching the pattern: '.$pattern); + public static function buildReverseUrl($url_regex, $params=array()) + { + $url_regex = str_replace('\\.', '.', $url_regex); + $url_regex = str_replace('\\-', '-', $url_regex); + $url = $url_regex; + $groups = '#\(([^)]+)\)#'; + $matches = array(); + preg_match_all($groups, $url_regex, $matches); + reset($params); + if (count($matches[0]) && count($matches[0]) == count($params)) { + // Test the params against the pattern + foreach ($matches[0] as $pattern) { + $in = current($params); + if (0 === preg_match('#'.$pattern.'#', $in)) { + throw new IPF_Exception('Error, param: '.$in.' is not matching the pattern: '.$pattern); + } + next($params); } - next($params); + $func = create_function('$matches', + 'static $p = '.var_export($params, true).'; '. + '$a = current($p); '. + 'next($p); '. + 'return $a;'); + $url = preg_replace_callback($groups, $func, $url_regex); } - $func = create_function('$matches', - 'static $p = '.var_export($params, true).'; '. - '$a = current($p); '. - 'next($p); '. - 'return $a;'); - $url = preg_replace_callback($groups, $func, $url_regex); - } - $url = substr(substr($url, 2), 0, -2); - if (substr($url, -1) !== '$') { - return $url; + $url = substr(substr($url, 2), 0, -2); + if (substr($url, -1) !== '$') { + return $url; + } + return substr($url, 0, -1); } - return substr($url, 0, -1); } + diff --git a/ipf/template/tag/url.php b/ipf/template/tag/url.php index 1f75995..2a3bb55 100644 --- a/ipf/template/tag/url.php +++ b/ipf/template/tag/url.php @@ -1,11 +1,10 @@