Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ldy
/
phwoolcon
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 2761996c
authored
Nov 21, 2020
by
yeran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove license
1 parent
320e8126
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
170 deletions
composer.json
src/Exception/Http/ApiException.php
src/Router.php
composer.json
View file @
2761996
...
...
@@ -3,8 +3,8 @@
"description"
:
"Phalcon"
,
"keywords"
:
[
"phwoolcon"
],
"type"
:
"library"
,
"version"
:
"1.0.
1
"
,
"license"
:
"
Apache-2.0
"
,
"version"
:
"1.0.
2
"
,
"license"
:
""
,
"authors"
:
[
{
"name"
:
"ldy"
,
...
...
@@ -23,8 +23,7 @@
"ext-phalcon"
:
"~3.0"
,
"symfony/console"
:
"~3.0"
,
"pda/pheanstalk"
:
"~3.1"
,
"swiftmailer/swiftmailer"
:
"~5.4|~6.0"
,
"opis/closure"
:
"3.0.1"
"swiftmailer/swiftmailer"
:
"~5.4|~6.0"
},
"require-dev"
:
{
},
...
...
src/Exception/Http/ApiException.php
0 → 100755
View file @
2761996
<?php
/**
* API异常处理
*/
namespace
Phwoolcon\Exception\Http
;
use
Phwoolcon\ErrorCodes
;
use
Throwable
;
class
ApiException
extends
\Exception
{
public
function
__construct
(
$message
=
''
,
int
$code
=
0
,
Throwable
$previous
=
null
)
{
if
(
empty
(
$message
))
{
$errorCode
=
ErrorCodes
::
getCodeMsg
(
$code
);
$message
=
(
$errorCode
&&
count
(
$errorCode
)
==
2
)
?
$errorCode
[
1
]
:
''
;
}
parent
::
__construct
(
$message
,
$code
,
$previous
);
}
public
function
getHeaders
()
{
return
[
'content-type: application/vnd.api+json'
,
'exception-type: ApiException'
,
];
}
public
function
getBody
()
{
return
json_encode
([
'jsonapi'
=>
[
'version'
=>
'1.0'
],
'error'
=>
$this
->
getCode
(),
'error_reason'
=>
$this
->
getMessage
()
]);
}
}
src/Router.php
View file @
2761996
...
...
@@ -3,18 +3,16 @@
namespace
Phwoolcon
;
use
Closure
;
use
core\base\Exception\ApiException
;
use
Opis\Closure\SerializableClosure
;
use
Phalcon\Di
;
use
Phalcon\Http\Request
;
use
Phalcon\Http\Response
;
use
Phalcon\Mvc\Router
as
PhalconRouter
;
use
Phalcon\Mvc\Router\Route
;
use
Phwoolcon\Daemon\ServiceAwareInterface
;
use
Phwoolcon\Exception\Http\ApiException
;
use
Phwoolcon\Exception\Http\CsrfException
;
use
Phwoolcon\Exception\Http\NotFoundException
;
use
Phwoolcon\Exception\HttpException
;
use
Phwoolcon\Fsm\Exception
;
/**
* Class Router
...
...
@@ -93,36 +91,6 @@ class Router extends PhalconRouter implements ServiceAwareInterface
$this
->
response
->
setStatusCode
(
200
);
}
/* public function addRoutes(array $routes, $prefix = null, $filter = null)
{
$prefix and $prefix = rtrim($prefix, '/');
foreach ($routes as $method => $methodRoutes) {
foreach ($methodRoutes as $uri => $handler) {
$uri{0} == '/' or $uri = '/' . $uri;
$prefix and $uri = $prefix . $uri;
$uri == '/' or $uri = rtrim($uri, '/');
$this->quickAdd($method, $uri, $handler, $filter);
}
}
}*/
public
function
addRoutes
(
array
$routes
)
{
foreach
(
$routes
as
$route
)
{
if
(
!
$route
[
2
]
instanceof
Closure
)
{
$pattern
=
$route
[
1
];
$paths
=
$route
[
2
];
$method
=
$route
[
0
];
$filter
=
empty
(
$route
[
3
])
?
null
:
$route
[
3
];
$this
->
quickAdd
(
$pattern
,
$paths
,
$method
,
$filter
);
}
else
{
$pattern
=
$route
[
1
];
$function
=
$route
[
2
];
$method
=
$route
[
0
];
$filter
=
empty
(
$route
[
3
])
?
null
:
$route
[
3
];
$this
->
CquickAdd
(
$pattern
,
$function
,
$method
,
$filter
);
}
}
}
public
static
function
checkCsrfToken
()
{
...
...
@@ -147,59 +115,6 @@ class Router extends PhalconRouter implements ServiceAwareInterface
{
static
::
$disableSession
=
true
;
}
/* public static function dispatch($uri = null)
{
try {
static::$router === null and static::$router = static::$di->getShared('router');
$router = static::$router;
// @codeCoverageIgnoreStart
if (!$uri && $router->_sitePathLength && $router->_uriSource == self::URI_SOURCE_GET_URL) {
list($uri) = explode('?', $_SERVER['REQUEST_URI']);
$uri = str_replace(basename($_SERVER['SCRIPT_FILENAME']), '', $uri);
if (substr($uri, 0, $router->_sitePathLength) == $router->_sitePathPrefix) {
$uri = substr($uri, $router->_sitePathLength);
}
}
// @codeCoverageIgnoreEnd
Events::fire('router:before_dispatch', $router, ['uri' => $uri]);
$realUri = $uri === null ? $router->getRewriteUri() : $uri;
$handledUri = $realUri === '/' ? $realUri : rtrim($realUri, '/');
static::$currentUri = $handledUri;
static::$useLiteHandler ? $router->liteHandle($handledUri) : $router->handle($handledUri);
($route = $router->getMatchedRoute()) or static::throw404Exception();
$controllerClass = $router->getControllerName();
$controllerClass instanceof SerializableClosure and $controllerClass = $controllerClass->getClosure();
if ($controllerClass instanceof Closure) {
static::$disableSession or Session::start();
static::$disableCsrfCheck or static::checkCsrfToken();
$response = $controllerClass();
if (!$response instanceof Response) {
// @var Response $realResponse
$realResponse = $router->response;
$realResponse->setContent($response);
$response = $realResponse;
}
} else {
// @var Controller $controller
$controller = new $controllerClass;
method_exists($controller, 'initialize') and $controller->initialize();
static::$disableSession or Session::start();
static::$disableCsrfCheck or static::checkCsrfToken();
method_exists($controller, $method = $router->getActionName()) or static::throw404Exception();
$controller->{$method}();
$response = $controller->response;
}
Events::fire('router:after_dispatch', $router, ['response' => $response]);
Session::end();
return $response;
} catch (HttpException $e) {
Log::exception($e);
return static::$runningUnitTest ? $e : $e->toResponse();
}
}*/
public
static
function
dispatch
(
$uri
=
null
)
{
try
{
...
...
@@ -225,8 +140,8 @@ class Router extends PhalconRouter implements ServiceAwareInterface
static
::
$currentUri
=
$handledUri
;
static
::
$useLiteHandler
?
$router
->
liteHandle
(
$handledUri
)
:
$router
->
handle
(
$handledUri
);
(
$route
=
$router
->
getMatchedRoute
())
or
static
::
throw404Exception
();
//此处匹配了route正则规则,不执行static::throw404Exception();
//采用的是原来传入函数形式的话则执行后面的404(在我的电脑上显示404,原因是我的访问地址经过了ld,重写apache规则后无404);
//此处匹配了route正则规则,不执行static::throw404Exception();
//采用的是原来传入函数形式的话则执行后面的404(在我的电脑上显示404,原因是我的访问地址经过了ld,重写apache规则后无404);
$module
=
$router
->
getModulename
();
if
(
!
empty
(
$module
))
{
$moduleDefinition
=
MODULES_PATH
.
'/'
.
$module
.
'/Module.php'
;
...
...
@@ -239,8 +154,7 @@ class Router extends PhalconRouter implements ServiceAwareInterface
}
// $namespace = $router->getNamespaceName();
$controllerName
=
$router
->
getControllerName
();
// die($controllerName);
$controllerName
instanceof
SerializableClosure
?
$controllerClass
=
$controllerName
->
getClosure
()
:
$controllerClass
=
$controllerName
;
$controllerClass
=
$controllerName
;
if
(
$controllerClass
instanceof
Closure
)
{
...
...
@@ -278,21 +192,14 @@ class Router extends PhalconRouter implements ServiceAwareInterface
$controllerClass
=
$prefix
.
"\Controllers
\\
"
.
$controllerName
;
// $controllerClass = $prefix.ucfirst($namespace) . "\Controllers\\" . $controllerName;
/* @var Controller $controller */
// var_dump($controllerClass);exit;
// var_dump(class_exists($controllerClass));
// exit;
if
(
!
class_exists
(
$controllerClass
))
{
$controllerClass
=
$prefix
.
"
\\
"
.
$controllerName
;
// $controllerClass = $prefix.ucfirst($namespace) . "\\" . $controllerName;
}
// die($controllerClass);
$controller
=
new
$controllerClass
;
method_exists
(
$controller
,
'initialize'
)
and
$controller
->
initialize
();
// static::$disableSession or Session::start();
static
::
$disableCsrfCheck
or
static
::
checkCsrfToken
();
// var_dump($params);
// exit;
if
(
method_exists
(
$controller
,
$actionname
))
{
//...用于php5.6以上,变量为数组,且不能为空
empty
(
$params
)
?
$controller
->
{
$actionname
}()
:
$controller
->
{
$actionname
}(
...
$params
);
...
...
@@ -309,9 +216,7 @@ class Router extends PhalconRouter implements ServiceAwareInterface
}
catch
(
HttpException
$e
)
{
Log
::
exception
(
$e
);
return
$e
->
toResponse
();
//return static::$runningUnitTest ? $e : $e->toResponse();
}
catch
(
\Exception
$exception
)
{
debug
(
'[Exception] File:['
.
$exception
->
getFile
()
.
'] '
.
'Line:['
.
$exception
->
getLine
()
.
'] '
.
'Message:['
.
$exception
->
getMessage
()
.
'] '
,
'ERROR'
);
//捕获异常
$response
=
new
Response
();
//获取响应实例
//判断是否为API异常
...
...
@@ -526,73 +431,6 @@ class Router extends PhalconRouter implements ServiceAwareInterface
/*is_array($routes) ? $this->addRoutes($routes) : print("路由文件非数组形式,请更改");*/
}
/* public function prefix($prefix, array $routes, $filter = null)
{
$this->addRoutes($routes, $prefix, $filter);
return $this;
}*/
/*public function quickAdd($method, $uri, $handler, $filter = null)
{
$uri{0} == '/' or $uri = '/' . $uri;
if ($isArrayHandler = is_array($handler)) {
if (!$filter && isset($handler['filter'])) {
$filter = $handler['filter'];
unset($handler['filter']);
}
// Support for callable: ['Class', 'method']
if (isset($handler[0]) && isset($handler[1])) {
$handler['controller'] = $handler[0];
$handler['action'] = $handler[1];
unset($handler[0], $handler[1]);
}
empty($handler['controller']) and $handler = reset($handler);
}
if (is_string($handler)) {
list($controller, $action) = explode('::', $handler);
$handler = ['controller' => $controller, 'action' => $action];
} elseif ($handler instanceof Closure) {
$handler = ['controller' => new SerializableClosure($handler)];
} elseif ($isArrayHandler && isset($handler['controller']) && $handler['controller'] instanceof Closure) {
$handler['controller'] = new SerializableClosure($handler['controller']);
}
$method == 'ANY' and $method = null;
$method == 'GET' and $method = ['GET', 'HEAD'];
$route = $this->add($uri, $handler, $method);
is_callable($filter) or $filter = null;
$filter and $route->beforeMatch($filter);
return $route;
}*/
public
function
quickAdd
(
$pattern
,
$paths
,
$method
,
$filter
=
null
)
{
$method
==
'ANY'
and
$method
=
null
;
$method
==
'GET'
and
$method
=
[
'GET'
,
'HEAD'
];
$route
=
$this
->
add
(
$pattern
,
$paths
,
$method
);
is_callable
(
$filter
)
or
$filter
=
null
;
$filter
and
$route
->
beforeMatch
(
$filter
);
return
$route
;
}
public
function
CquickAdd
(
$pattern
,
$function
,
$method
,
$filter
=
null
)
{
$method
==
'ANY'
and
$method
=
null
;
$method
==
'GET'
and
$method
=
[
'GET'
,
'HEAD'
];
$handler
[
'controller'
]
=
new
SerializableClosure
(
$function
);
$route
=
$this
->
add
(
$pattern
,
$handler
,
$method
);
is_callable
(
$filter
)
or
$filter
=
null
;
$filter
and
$route
->
beforeMatch
(
$filter
);
return
$route
;
}
/*public function CquickAdd($function, $method, $filter = null)
{
$method == 'ANY' and $method = null;
$method == 'GET' and $method = ['GET', 'HEAD'];
is_callable($filter) or $filter = null;
$filter and $route->beforeMatch($filter);
return $route;
}*/
public
static
function
register
(
Di
$di
)
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment