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
e0dd12fb
Commit
e0dd12fb
authored
May 25, 2012
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
c6db38ff
7865a56d
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
184 additions
and
27 deletions
+184
-27
Application.php
framework/base/Application.php
+9
-0
Controller.php
framework/base/Controller.php
+36
-0
ErrorHandler.php
framework/base/ErrorHandler.php
+22
-0
Module.php
framework/base/Module.php
+62
-14
RenderEvent.php
framework/base/RenderEvent.php
+5
-5
View.php
framework/base/View.php
+30
-8
FileHelper.php
framework/util/FileHelper.php
+20
-0
No files found.
framework/base/Application.php
View file @
e0dd12fb
...
@@ -80,6 +80,10 @@ class Application extends Module
...
@@ -80,6 +80,10 @@ class Application extends Module
*/
*/
public
$name
=
'My Application'
;
public
$name
=
'My Application'
;
/**
/**
* @var string the version of this application. Defaults to '1.0'.
*/
public
$version
=
'1.0'
;
/**
* @var string the charset currently used for the application. Defaults to 'UTF-8'.
* @var string the charset currently used for the application. Defaults to 'UTF-8'.
*/
*/
public
$charset
=
'UTF-8'
;
public
$charset
=
'UTF-8'
;
...
@@ -99,6 +103,11 @@ class Application extends Module
...
@@ -99,6 +103,11 @@ class Application extends Module
* @var Controller the currently active controller instance
* @var Controller the currently active controller instance
*/
*/
public
$controller
;
public
$controller
;
/**
* @var mixed the layout that should be applied for views in this application. Defaults to 'main'.
* If this is false, layout will be disabled.
*/
public
$layout
=
'main'
;
// todo
// todo
public
$localeDataPath
=
'@yii/i18n/data'
;
public
$localeDataPath
=
'@yii/i18n/data'
;
...
...
framework/base/Controller.php
View file @
e0dd12fb
...
@@ -69,6 +69,10 @@ class Controller extends Component implements Initable
...
@@ -69,6 +69,10 @@ class Controller extends Component implements Initable
* @var Action the action that is currently being executed
* @var Action the action that is currently being executed
*/
*/
public
$action
;
public
$action
;
/**
* @var View the view currently being used
*/
private
$_view
;
/**
/**
* @param string $id ID of this controller
* @param string $id ID of this controller
...
@@ -301,6 +305,12 @@ class Controller extends Component implements Initable
...
@@ -301,6 +305,12 @@ class Controller extends Component implements Initable
{
{
if
(
$this
->
beforeRender
(
$view
))
{
if
(
$this
->
beforeRender
(
$view
))
{
$v
=
$this
->
createView
();
$v
=
$this
->
createView
();
if
((
$theme
=
\Yii
::
$application
->
getTheme
())
!==
null
)
{
$v
->
basePath
[]
=
$theme
->
getViewPath
(
$this
);
$v
->
rootPath
[]
=
$theme
->
getViewPath
();
}
$v
->
basePath
[]
=
$this
->
getViewPath
();
$v
->
rootPath
[]
=
\Yii
::
$application
->
getViewPath
();
$v
->
render
(
$view
,
$params
);
$v
->
render
(
$view
,
$params
);
$this
->
afterRender
(
$view
);
$this
->
afterRender
(
$view
);
}
}
...
@@ -317,6 +327,32 @@ class Controller extends Component implements Initable
...
@@ -317,6 +327,32 @@ class Controller extends Component implements Initable
}
}
public
function
resolveLayout
()
{
$layout
=
$this
->
layout
;
$module
=
$this
->
module
;
while
(
$layout
===
null
&&
$module
!==
null
)
{
$layout
=
$module
->
layout
;
$module
=
$module
->
module
;
$layout
=
$
}
}
public
function
getView
()
{
if
(
$this
->
_view
===
null
)
{
$this
->
_view
=
$this
->
createView
();
$this
->
_view
->
owner
=
$this
;
if
((
$theme
=
\Yii
::
$application
->
getTheme
())
!==
null
)
{
$this
->
_view
->
basePath
[]
=
$theme
->
getViewPath
(
$this
);
$this
->
_view
->
rootPath
[]
=
$theme
->
getViewPath
();
}
$this
->
_view
->
basePath
[]
=
$this
->
getViewPath
();
$this
->
_view
->
rootPath
[]
=
\Yii
::
$application
->
getViewPath
();
}
return
$this
->
_view
;
}
public
function
createView
()
public
function
createView
()
{
{
return
new
View
;
return
new
View
;
...
...
framework/base/ErrorHandler.php
View file @
e0dd12fb
...
@@ -50,6 +50,11 @@ class ErrorHandler extends ApplicationComponent
...
@@ -50,6 +50,11 @@ class ErrorHandler extends ApplicationComponent
* @var \Exception the exception that is being handled currently
* @var \Exception the exception that is being handled currently
*/
*/
public
$exception
;
public
$exception
;
/**
* @var boolean whether to log errors also using error_log(). Defaults to true.
* Note that errors captured by the error handler are always logged by [[\Yii::error()]].
*/
public
$logErrors
=
true
;
public
function
init
()
public
function
init
()
{
{
...
@@ -95,6 +100,20 @@ class ErrorHandler extends ApplicationComponent
...
@@ -95,6 +100,20 @@ class ErrorHandler extends ApplicationComponent
// use the most primitive way to display exception thrown in the error view
// use the most primitive way to display exception thrown in the error view
$this
->
renderAsText
(
$e
);
$this
->
renderAsText
(
$e
);
}
}
try
{
\Yii
::
$application
->
end
(
1
);
}
catch
(
Exception
$e2
)
{
// use the most primitive way to log error occurred in end()
$msg
=
get_class
(
$e2
)
.
': '
.
$e2
->
getMessage
()
.
' ('
.
$e2
->
getFile
()
.
':'
.
$e2
->
getLine
()
.
")
\n
"
;
$msg
.=
$e2
->
getTraceAsString
()
.
"
\n
"
;
$msg
.=
"Previous error:
\n
"
;
$msg
.=
$e2
->
getTraceAsString
()
.
"
\n
"
;
$msg
.=
'$_SERVER='
.
var_export
(
$_SERVER
,
true
);
error_log
(
$msg
);
exit
(
1
);
}
}
}
protected
function
render
(
$exception
)
protected
function
render
(
$exception
)
...
@@ -269,6 +288,9 @@ class ErrorHandler extends ApplicationComponent
...
@@ -269,6 +288,9 @@ class ErrorHandler extends ApplicationComponent
$category
.=
'\\'
.
$exception
->
getSeverity
();
$category
.=
'\\'
.
$exception
->
getSeverity
();
}
}
\Yii
::
error
((
string
)
$exception
,
$category
);
\Yii
::
error
((
string
)
$exception
,
$category
);
if
(
$this
->
logErrors
)
{
error_log
(
$exception
);
}
}
}
public
function
clearOutput
()
public
function
clearOutput
()
...
...
framework/base/Module.php
View file @
e0dd12fb
...
@@ -9,6 +9,8 @@
...
@@ -9,6 +9,8 @@
namespace
yii\base
;
namespace
yii\base
;
use
yii\util\FileHelper
;
/**
/**
* Module is the base class for module and application classes.
* Module is the base class for module and application classes.
*
*
...
@@ -44,6 +46,12 @@ abstract class Module extends Component implements Initable
...
@@ -44,6 +46,12 @@ abstract class Module extends Component implements Initable
*/
*/
public
$module
;
public
$module
;
/**
/**
* @var mixed the layout that should be applied for views within this module. This refers to a view name
* relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]]
* will be taken. If this is false, layout will be disabled within this module.
*/
public
$layout
;
/**
* @var array mapping from controller ID to controller configurations.
* @var array mapping from controller ID to controller configurations.
* Each name-value pair specifies the configuration of a single controller.
* Each name-value pair specifies the configuration of a single controller.
* A controller configuration can be either a string or an array.
* A controller configuration can be either a string or an array.
...
@@ -73,14 +81,18 @@ abstract class Module extends Component implements Initable
...
@@ -73,14 +81,18 @@ abstract class Module extends Component implements Initable
public
$defaultRoute
=
'default'
;
public
$defaultRoute
=
'default'
;
/**
/**
* @var string the root directory of the module.
* @var string the root directory of the module.
* @see getBasePath
* @see setBasePath
*/
*/
protected
$_basePath
;
protected
$_basePath
;
/**
/**
* @var string the root directory that contains view files.
*/
protected
$_viewPath
;
/**
* @var string the root directory that contains layout view files.
*/
protected
$_layoutPath
;
/**
* @var string the directory containing controller classes in the module.
* @var string the directory containing controller classes in the module.
* @see getControllerPath
* @see setControllerPath
*/
*/
protected
$_controllerPath
;
protected
$_controllerPath
;
/**
/**
...
@@ -182,12 +194,7 @@ abstract class Module extends Component implements Initable
...
@@ -182,12 +194,7 @@ abstract class Module extends Component implements Initable
*/
*/
public
function
setBasePath
(
$path
)
public
function
setBasePath
(
$path
)
{
{
$p
=
\Yii
::
getAlias
(
$path
);
$this
->
_basePath
=
FileHelper
::
ensureDirectory
(
$path
);
if
(
$p
===
false
||
!
is_dir
(
$p
))
{
throw
new
Exception
(
'Invalid base path: '
.
$path
);
}
else
{
$this
->
_basePath
=
realpath
(
$p
);
}
}
}
/**
/**
...
@@ -212,12 +219,53 @@ abstract class Module extends Component implements Initable
...
@@ -212,12 +219,53 @@ abstract class Module extends Component implements Initable
*/
*/
public
function
setControllerPath
(
$path
)
public
function
setControllerPath
(
$path
)
{
{
$p
=
\Yii
::
getAlias
(
$path
);
$this
->
_controllerPath
=
FileHelper
::
ensureDirectory
(
$path
);
if
(
$p
===
false
||
!
is_dir
(
$p
))
{
}
throw
new
Exception
(
'Invalid controller path: '
.
$path
);
/**
* @return string the root directory of view files. Defaults to 'moduleDir/views' where
* moduleDir is the directory containing the module class.
*/
public
function
getViewPath
()
{
if
(
$this
->
_viewPath
!==
null
)
{
return
$this
->
_viewPath
;
}
else
{
}
else
{
$this
->
_controllerPath
=
realpath
(
$p
);
return
$this
->
_viewPath
=
$this
->
getBasePath
()
.
DIRECTORY_SEPARATOR
.
'views'
;
}
}
}
/**
* Sets the directory that contains the view files.
* @param string $path the root directory of view files.
* @throws Exception if the directory is invalid
*/
public
function
setViewPath
(
$path
)
{
$this
->
_viewPath
=
FileHelper
::
ensureDirectory
(
$path
);
}
/**
* @return string the root directory of layout files. Defaults to 'moduleDir/views/layouts' where
* moduleDir is the directory containing the module class.
*/
public
function
getLayoutPath
()
{
if
(
$this
->
_layoutPath
!==
null
)
{
return
$this
->
_layoutPath
;
}
else
{
return
$this
->
_layoutPath
=
$this
->
getViewPath
()
.
DIRECTORY_SEPARATOR
.
'layouts'
;
}
}
/**
* Sets the directory that contains the layout files.
* @param string $path the root directory of layout files.
* @throws Exception if the directory is invalid
*/
public
function
setLayoutPath
(
$path
)
{
$this
->
_layoutPath
=
FileHelper
::
ensureDirectory
(
$path
);
}
}
/**
/**
...
...
framework/base/RenderEvent.php
View file @
e0dd12fb
...
@@ -20,9 +20,9 @@ namespace yii\base;
...
@@ -20,9 +20,9 @@ namespace yii\base;
class
RenderEvent
extends
Event
class
RenderEvent
extends
Event
{
{
/**
/**
* @var
Action the action currently being execut
ed
* @var
string the view currently being render
ed
*/
*/
public
$
action
;
public
$
view
;
/**
/**
* @var boolean whether the action is in valid state and its life cycle should proceed.
* @var boolean whether the action is in valid state and its life cycle should proceed.
*/
*/
...
@@ -30,10 +30,10 @@ class RenderEvent extends Event
...
@@ -30,10 +30,10 @@ class RenderEvent extends Event
/**
/**
* Constructor.
* Constructor.
* @param
Action $action the action associated with this action event.
* @param
string $view the view currently being rendered
*/
*/
public
function
__construct
(
Action
$action
)
public
function
__construct
(
$view
)
{
{
$this
->
action
=
$action
;
$this
->
view
=
$view
;
}
}
}
}
framework/base/View.php
View file @
e0dd12fb
...
@@ -22,13 +22,22 @@ class View extends Component
...
@@ -22,13 +22,22 @@ class View extends Component
*/
*/
public
$owner
;
public
$owner
;
/**
/**
* @var string|array the base path where the view file should be looked for using the specified view name.
* @var string|array the directories where the view file should be looked for a *relative* view name is given.
* This can be either a string representing a single base path, or an array representing multiple base paths.
* This can be either a string representing a single directory, or an array representing multiple directories.
* If the latter, the view file will be looked for in the given base paths in the order they are specified.
* If the latter, the view file will be looked for in the given directories in the order they are specified.
* Path aliases can be used. This property must be set before calling [[render()]].
* Path aliases can be used. This property must be set before calling [[render()]] with a relative view name.
* @see roothPath
*/
*/
public
$basePath
;
public
$basePath
;
/**
/**
* @var string|array the directories where the view file should be looked for an *absolute* view name is given.
* This can be either a string representing a single directory, or an array representing multiple directories.
* If the latter, the view file will be looked for in the given directories in the order they are specified.
* Path aliases can be used. This property must be set before calling [[render()]] with an absolute view name.
* @see basePath
*/
public
$rootPath
;
/**
* @var string the language that the view should be rendered in. If not set, it will use
* @var string the language that the view should be rendered in. If not set, it will use
* the value of [[Application::language]].
* the value of [[Application::language]].
*/
*/
...
@@ -255,15 +264,28 @@ class View extends Component
...
@@ -255,15 +264,28 @@ class View extends Component
}
}
if
(
$view
[
0
]
===
'@'
)
{
if
(
$view
[
0
]
===
'@'
)
{
$file
=
\Yii
::
getAlias
(
$view
);
$file
=
\Yii
::
getAlias
(
$view
);
}
elseif
(
!
empty
(
$this
->
basePath
))
{
if
(
$file
===
false
)
{
$basePaths
=
is_array
(
$this
->
basePath
)
?
$this
->
basePath
:
array
(
$this
->
basePath
);
return
false
;
foreach
(
$basePaths
as
$basePath
)
{
}
$file
=
\Yii
::
getAlias
(
$basePath
.
DIRECTORY_SEPARATOR
.
$view
);
}
else
{
if
(
$view
[
0
]
===
'/'
)
{
$paths
=
$this
->
rootPath
;
$view
=
substr
(
$view
,
1
);
}
else
{
$paths
=
$this
->
basePath
;
}
if
(
!
empty
(
$paths
))
{
if
(
!
is_array
(
$paths
))
{
$paths
=
array
(
$paths
);
}
foreach
(
$paths
as
$path
)
{
$file
=
\Yii
::
getAlias
(
$path
.
'/'
.
$view
);
if
(
is_file
(
$file
))
{
if
(
is_file
(
$file
))
{
break
;
break
;
}
}
}
}
}
}
}
if
(
isset
(
$file
)
&&
is_file
(
$file
))
{
if
(
isset
(
$file
)
&&
is_file
(
$file
))
{
$file
=
FileHelper
::
localize
(
$file
,
$this
->
language
,
$this
->
sourceLanguage
);
$file
=
FileHelper
::
localize
(
$file
,
$this
->
language
,
$this
->
sourceLanguage
);
return
is_file
(
$file
)
?
$file
:
false
;
return
is_file
(
$file
)
?
$file
:
false
;
...
...
framework/util/FileHelper.php
View file @
e0dd12fb
...
@@ -9,6 +9,8 @@
...
@@ -9,6 +9,8 @@
namespace
yii\util
;
namespace
yii\util
;
use
yii\base\Exception
;
/**
/**
* Filesystem helper
* Filesystem helper
*
*
...
@@ -30,6 +32,24 @@ class FileHelper
...
@@ -30,6 +32,24 @@ class FileHelper
}
}
/**
/**
* Checks the given path and ensures it is a directory.
* This method will call `realpath()` to "normalize" the given path.
* If the given path does not refer to an existing directory, an exception will be thrown.
* @param string $path the given path. This can also be a path alias.
* @return string the normalized path
* @throws Exception if the path does not refer to an existing directory.
*/
public
static
function
ensureDirectory
(
$path
)
{
$p
=
\Yii
::
getAlias
(
$path
);
if
(
$p
!==
false
&&
(
$p
=
realpath
(
$p
))
!==
false
&&
is_dir
(
$p
))
{
return
$p
;
}
else
{
throw
new
Exception
(
'Directory does not exist: '
.
$path
);
}
}
/**
* Returns the localized version of a specified file.
* Returns the localized version of a specified file.
*
*
* The searching is based on the specified language code. In particular,
* The searching is based on the specified language code. In particular,
...
...
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