Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
e09a791c
Commit
e09a791c
authored
Jan 31, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MVC cleanup
parent
4402073d
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
84 additions
and
66 deletions
+84
-66
Controller.php
framework/base/Controller.php
+5
-5
ErrorHandler.php
framework/base/ErrorHandler.php
+1
-2
InlineAction.php
framework/base/InlineAction.php
+1
-1
Module.php
framework/base/Module.php
+31
-31
Application.php
framework/console/Application.php
+24
-2
CreateController.php
framework/console/controllers/CreateController.php
+2
-2
HelpController.php
framework/console/controllers/HelpController.php
+14
-17
ConsoleHelper.php
framework/util/ConsoleHelper.php
+1
-1
yiic.php
framework/yiic.php
+5
-5
No files found.
framework/base/Controller.php
View file @
e09a791c
...
...
@@ -100,10 +100,6 @@ class Controller extends Component
*/
public
function
runAction
(
$id
,
$params
=
array
())
{
if
(
$id
===
''
)
{
$id
=
$this
->
defaultAction
;
}
$action
=
$this
->
createAction
(
$id
);
if
(
$action
!==
null
)
{
$oldAction
=
$this
->
action
;
...
...
@@ -143,7 +139,7 @@ class Controller extends Component
}
elseif
(
$pos
>
0
)
{
return
$this
->
module
->
runAction
(
$route
,
$params
);
}
else
{
return
\Yii
::
$application
->
runAction
(
$route
,
$params
);
return
\Yii
::
$application
->
runAction
(
ltrim
(
$route
,
'/'
)
,
$params
);
}
}
...
...
@@ -174,6 +170,10 @@ class Controller extends Component
*/
public
function
createAction
(
$id
)
{
if
(
$id
===
''
)
{
$id
=
$this
->
defaultAction
;
}
$actionMap
=
$this
->
actions
();
if
(
isset
(
$actionMap
[
$id
]))
{
return
Yii
::
createObject
(
$actionMap
[
$id
],
$id
,
$this
);
...
...
framework/base/ErrorHandler.php
View file @
e09a791c
...
...
@@ -320,8 +320,7 @@ class ErrorHandler extends Component
*/
public
function
renderAsHtml
(
$exception
)
{
$view
=
new
View
;
$view
->
owner
=
$this
;
$view
=
new
View
(
$this
);
$name
=
!
YII_DEBUG
||
$exception
instanceof
HttpException
?
$this
->
errorView
:
$this
->
exceptionView
;
echo
$view
->
render
(
$name
,
array
(
'exception'
=>
$exception
,
...
...
framework/base/InlineAction.php
View file @
e09a791c
...
...
@@ -47,6 +47,6 @@ class InlineAction extends Action
{
$method
=
new
\ReflectionMethod
(
$this
->
controller
,
$this
->
actionMethod
);
$args
=
$this
->
bindActionParams
(
$method
,
$params
);
return
(
int
)
$method
->
invokeArgs
(
$this
,
$args
);
return
(
int
)
$method
->
invokeArgs
(
$this
->
controller
,
$args
);
}
}
framework/base/Module.php
View file @
e09a791c
...
...
@@ -559,53 +559,52 @@ abstract class Module extends Component
*/
public
function
runAction
(
$route
,
$params
=
array
())
{
$route
=
trim
(
$route
,
'/'
);
if
(
$route
===
''
)
{
$route
=
trim
(
$this
->
defaultRoute
,
'/'
);
}
if
((
$pos
=
strpos
(
$route
,
'/'
))
!==
false
)
{
$id
=
substr
(
$route
,
0
,
$pos
);
$route2
=
substr
(
$route
,
$pos
+
1
);
}
else
{
$id
=
$route
;
$route2
=
''
;
}
$module
=
$this
->
getModule
(
$id
);
if
(
$module
!==
null
)
{
return
$module
->
runAction
(
$route2
,
$params
);
}
$controller
=
$this
->
createController
(
$id
);
if
(
$controller
!==
null
)
{
$result
=
$this
->
createController
(
$route
);
if
(
is_array
(
$result
))
{
/** @var $controller Controller */
list
(
$controller
,
$actionID
)
=
$result
;
$oldController
=
Yii
::
$application
->
controller
;
Yii
::
$application
->
controller
=
$controller
;
$status
=
$controller
->
runAction
(
$route2
,
$params
);
$status
=
$controller
->
runAction
(
$actionID
,
$params
);
Yii
::
$application
->
controller
=
$oldController
;
return
$status
;
}
else
{
throw
new
InvalidRouteException
(
'Unable to resolve the request: '
.
$this
->
getUniqueId
()
.
'/'
.
$route
);
throw
new
InvalidRouteException
(
'Unable to resolve the request: '
.
trim
(
$this
->
getUniqueId
()
.
'/'
.
$route
,
'/'
)
);
}
}
/**
* Creates a controller instance based on the controller ID.
*
* The controller is created within th
e given
module. The method first attempts to
* The controller is created within th
is
module. The method first attempts to
* create the controller based on the [[controllerMap]] of the module. If not available,
* it will look for the controller class under the [[controllerPath]] and create an
* instance of it.
*
* @param string $id the controller ID
* @return Controller the newly created controller instance
* @param string $route the route consisting of module, controller and action IDs.
* @return array|boolean if the controller is created successfully, it will be returned together
* with the remainder of the route which represents the action ID. Otherwise false will be returned.
*/
public
function
createController
(
$
id
)
public
function
createController
(
$
route
)
{
if
(
$route
===
''
)
{
$route
=
$this
->
defaultRoute
;
}
if
((
$pos
=
strpos
(
$route
,
'/'
))
!==
false
)
{
$id
=
substr
(
$route
,
0
,
$pos
);
$route
=
substr
(
$route
,
$pos
+
1
);
}
else
{
$id
=
$route
;
$route
=
''
;
}
$module
=
$this
->
getModule
(
$id
);
if
(
$module
!==
null
)
{
return
$module
->
createController
(
$route
);
}
if
(
isset
(
$this
->
controllerMap
[
$id
]))
{
return
Yii
::
createObject
(
$this
->
controllerMap
[
$id
],
$id
,
$this
);
$controller
=
Yii
::
createObject
(
$this
->
controllerMap
[
$id
],
$id
,
$this
);
}
elseif
(
preg_match
(
'/^[a-z0-9\\-_]+$/'
,
$id
))
{
$className
=
StringHelper
::
id2camel
(
$id
)
.
'Controller'
;
$classFile
=
$this
->
controllerPath
.
DIRECTORY_SEPARATOR
.
$className
.
'.php'
;
...
...
@@ -615,10 +614,11 @@ abstract class Module extends Component
require
(
$classFile
);
}
if
(
class_exists
(
$className
,
false
)
&&
is_subclass_of
(
$className
,
'\yii\base\Controller'
))
{
return
new
$className
(
$id
,
$this
);
$controller
=
new
$className
(
$id
,
$this
);
}
}
}
return
null
;
return
isset
(
$controller
)
?
array
(
$controller
,
$route
)
:
false
;
}
}
framework/console/Application.php
View file @
e09a791c
...
...
@@ -10,7 +10,7 @@
namespace
yii\console
;
use
yii\base\Exception
;
use
yii\
util\ReflectionHelper
;
use
yii\
base\InvalidRouteException
;
/**
* Application represents a console application.
...
...
@@ -94,7 +94,29 @@ class Application extends \yii\base\Application
if
(
$request
->
getIsConsoleRequest
())
{
return
$this
->
runAction
(
$request
->
route
,
$request
->
params
);
}
else
{
die
(
'This script must be run from the command line.'
);
echo
"Error: this script must be run from the command line."
;
return
1
;
}
}
/**
* Runs a controller action specified by a route.
* This method parses the specified route and creates the corresponding child module(s), controller and action
* instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
* If the route is empty, the method will use [[defaultRoute]].
* @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to the action
* @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
* @throws InvalidRouteException if the requested route cannot be resolved into an action successfully
*/
public
function
runAction
(
$route
,
$params
=
array
())
{
try
{
return
parent
::
runAction
(
$route
,
$params
);
}
catch
(
InvalidRouteException
$e
)
{
echo
"
\n
Error: unknown command
\"
$route
\"
.
\n
"
;
return
1
;
}
}
...
...
framework/console/controllers/CreateController.php
View file @
e09a791c
...
...
@@ -165,8 +165,8 @@ class CreateController extends Controller
}
/**
* @param string $path1 ab
o
solute path
* @param string $path2 ab
o
solute path
* @param string $path1 absolute path
* @param string $path2 absolute path
*
* @return string relative path
*/
...
...
framework/console/controllers/HelpController.php
View file @
e09a791c
...
...
@@ -12,6 +12,7 @@ namespace yii\console\controllers;
use
yii\base\Application
;
use
yii\base\InlineAction
;
use
yii\console\Controller
;
use
yii\util\StringHelper
;
/**
* This command provides help information about console commands.
...
...
@@ -54,16 +55,16 @@ class HelpController extends Controller
}
else
{
$result
=
\Yii
::
$application
->
createController
(
$args
[
0
]);
if
(
$result
===
false
)
{
echo
"
Unknown command: "
.
$args
[
0
]
.
"
\n
"
;
echo
"
\n
Error: no help for unknown command
\"
{
$args
[
0
]
}
\"
.
\n
"
;
return
1
;
}
list
(
$controller
,
$action
)
=
$result
;
list
(
$controller
,
$action
ID
)
=
$result
;
if
(
$action
===
''
)
{
if
(
$action
ID
===
''
)
{
$status
=
$this
->
getControllerHelp
(
$controller
);
}
else
{
$status
=
$this
->
getActionHelp
(
$controller
,
$action
);
$status
=
$this
->
getActionHelp
(
$controller
,
$action
ID
);
}
}
return
$status
;
...
...
@@ -87,13 +88,13 @@ class HelpController extends Controller
*/
public
function
getActions
(
$controller
)
{
$actions
=
array_keys
(
$controller
->
action
Map
);
$actions
=
array_keys
(
$controller
->
action
s
()
);
$class
=
new
\ReflectionClass
(
$controller
);
foreach
(
$class
->
getMethods
()
as
$method
)
{
/** @var $method \ReflectionMethod */
$name
=
$method
->
getName
();
if
(
$method
->
isPublic
()
&&
!
$method
->
isStatic
()
&&
strpos
(
$name
,
'action'
)
===
0
)
{
$actions
[]
=
lcfirst
(
substr
(
$name
,
6
));
if
(
$method
->
isPublic
()
&&
!
$method
->
isStatic
()
&&
strpos
(
$name
,
'action'
)
===
0
&&
$name
!==
'actions'
)
{
$actions
[]
=
StringHelper
::
camel2id
(
substr
(
$name
,
6
));
}
}
sort
(
$actions
);
...
...
@@ -107,11 +108,7 @@ class HelpController extends Controller
*/
protected
function
getModuleCommands
(
$module
)
{
if
(
$module
instanceof
Application
)
{
$prefix
=
''
;
}
else
{
$prefix
=
$module
->
getUniqueId
()
.
'/'
;
}
$prefix
=
$module
instanceof
Application
?
''
:
$module
->
getUniqueID
()
.
'/'
;
$commands
=
array
();
foreach
(
array_keys
(
$module
->
controllerMap
)
as
$id
)
{
...
...
@@ -145,12 +142,12 @@ class HelpController extends Controller
{
$commands
=
$this
->
getCommands
();
if
(
$commands
!==
array
())
{
echo
"
\n
Usage: yiic <command-name> [...options...]
\n\n
"
;
echo
"The following commands are available:
\n
"
;
echo
"
\n
Usage: yiic <command-name> [...options...]
\n\n
"
;
echo
"The following commands are available:
\n
\n
"
;
foreach
(
$commands
as
$command
)
{
echo
"
-
$command
\n
"
;
echo
"
*
$command
\n
"
;
}
echo
"
\n
To see
individual command help
, enter:
\n
"
;
echo
"
\n
To see
the help of each command
, enter:
\n
"
;
echo
"
\n
yiic help <command-name>
\n
"
;
}
else
{
echo
"
\n
No commands are found.
\n
"
;
...
...
@@ -195,7 +192,7 @@ class HelpController extends Controller
$prefix
=
$controller
->
getUniqueId
();
foreach
(
$actions
as
$action
)
{
if
(
$controller
->
defaultAction
===
$action
)
{
echo
" *
$prefix
/
$action
(default)
\n
"
;
echo
" *
$prefix
(default)
\n
"
;
}
else
{
echo
" *
$prefix
/
$action
\n
"
;
}
...
...
framework/util/ConsoleHelper.php
View file @
e09a791c
...
...
@@ -10,7 +10,7 @@
namespace
yii\util
;
/**
* ConsoleHelper provides additional u
n
ility functions for console applications.
* ConsoleHelper provides additional u
t
ility functions for console applications.
*
* @author Carsten Brandt <mail@cebe.cc>
* @author Alexander Makarov <sam@rmcreative.ru>
...
...
framework/yiic.php
View file @
e09a791c
<?php
define
(
'YII_DEBUG'
,
true
);
/**
* Yii console bootstrap file.
*
...
...
@@ -8,16 +7,17 @@ define('YII_DEBUG', true);
* @license http://www.yiiframework.com/license/
*/
defined
(
'YII_DEBUG'
)
or
define
(
'YII_DEBUG'
,
true
);
// fcgi doesn't have STDIN defined by default
defined
(
'STDIN'
)
or
define
(
'STDIN'
,
fopen
(
'php://stdin'
,
'r'
));
require
(
__DIR__
.
'/yii.php'
);
$config
=
array
(
'controllerPath'
=>
'@yii/console/controllers'
,
);
$id
=
'yiic'
;
$basePath
=
__DIR__
.
'/console'
;
$application
=
new
yii\console\Application
(
$id
,
$basePath
,
$config
);
$application
=
new
yii\console\Application
(
$id
,
$basePath
,
array
(
'controllerPath'
=>
'@yii/console/controllers'
,
));
$application
->
run
();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment