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
bd320533
Commit
bd320533
authored
Jan 30, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MVC WIP
parent
9165a159
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
125 additions
and
33 deletions
+125
-33
YiiBase.php
framework/YiiBase.php
+6
-11
Controller.php
framework/base/Controller.php
+11
-0
ErrorHandler.php
framework/base/ErrorHandler.php
+1
-1
Theme.php
framework/base/Theme.php
+80
-21
View.php
framework/base/View.php
+0
-0
Widget.php
framework/base/Widget.php
+13
-0
FileHelper.php
framework/util/FileHelper.php
+14
-0
No files found.
framework/YiiBase.php
View file @
bd320533
...
@@ -8,9 +8,8 @@
...
@@ -8,9 +8,8 @@
*/
*/
use
yii\base\Exception
;
use
yii\base\Exception
;
use
yii\logging\Logger
;
use
yii\base\InvalidCallException
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
use
yii\logging\Logger
;
/**
/**
* Gets the application start timestamp.
* Gets the application start timestamp.
...
@@ -189,14 +188,14 @@ class YiiBase
...
@@ -189,14 +188,14 @@ class YiiBase
*
*
* Note, this method does not ensure the existence of the resulting path.
* Note, this method does not ensure the existence of the resulting path.
* @param string $alias alias
* @param string $alias alias
* @param boolean $throwException whether to throw exception if the alias is invalid.
* @return string|boolean path corresponding to the alias, false if the root alias is not previously registered.
* @return string|boolean path corresponding to the alias, false if the root alias is not previously registered.
* @throws Exception if the alias is invalid and $throwException is true.
* @see setAlias
* @see setAlias
*/
*/
public
static
function
getAlias
(
$alias
,
$throwException
=
false
)
public
static
function
getAlias
(
$alias
)
{
{
if
(
isset
(
self
::
$aliases
[
$alias
]))
{
if
(
!
is_string
(
$alias
))
{
return
false
;
}
elseif
(
isset
(
self
::
$aliases
[
$alias
]))
{
return
self
::
$aliases
[
$alias
];
return
self
::
$aliases
[
$alias
];
}
elseif
(
$alias
===
''
||
$alias
[
0
]
!==
'@'
)
{
// not an alias
}
elseif
(
$alias
===
''
||
$alias
[
0
]
!==
'@'
)
{
// not an alias
return
$alias
;
return
$alias
;
...
@@ -206,12 +205,8 @@ class YiiBase
...
@@ -206,12 +205,8 @@ class YiiBase
return
self
::
$aliases
[
$alias
]
=
self
::
$aliases
[
$rootAlias
]
.
substr
(
$alias
,
$pos
);
return
self
::
$aliases
[
$alias
]
=
self
::
$aliases
[
$rootAlias
]
.
substr
(
$alias
,
$pos
);
}
}
}
}
if
(
$throwException
)
{
throw
new
Exception
(
"Invalid path alias:
$alias
"
);
}
else
{
return
false
;
return
false
;
}
}
}
/**
/**
* Registers a path alias.
* Registers a path alias.
...
@@ -361,7 +356,7 @@ class YiiBase
...
@@ -361,7 +356,7 @@ class YiiBase
$class
=
$config
[
'class'
];
$class
=
$config
[
'class'
];
unset
(
$config
[
'class'
]);
unset
(
$config
[
'class'
]);
}
else
{
}
else
{
throw
new
InvalidC
all
Exception
(
'Object configuration must be an array containing a "class" element.'
);
throw
new
InvalidC
onfig
Exception
(
'Object configuration must be an array containing a "class" element.'
);
}
}
if
(
!
class_exists
(
$class
,
false
))
{
if
(
!
class_exists
(
$class
,
false
))
{
...
...
framework/base/Controller.php
View file @
bd320533
...
@@ -279,4 +279,15 @@ class Controller extends Component
...
@@ -279,4 +279,15 @@ class Controller extends Component
{
{
return
new
View
(
$this
);
return
new
View
(
$this
);
}
}
/**
* Returns the directory containing view files for this controller.
* The default implementation returns the directory named as controller [[id]] under the [[module]]'s
* [[viewPath]] directory.
* @return string the directory containing the view files for this controller.
*/
public
function
getViewPath
()
{
return
$this
->
module
->
getViewPath
()
.
DIRECTORY_SEPARATOR
.
$this
->
id
;
}
}
}
framework/base/ErrorHandler.php
View file @
bd320533
...
@@ -321,7 +321,7 @@ class ErrorHandler extends Component
...
@@ -321,7 +321,7 @@ class ErrorHandler extends Component
public
function
renderAsHtml
(
$exception
)
public
function
renderAsHtml
(
$exception
)
{
{
$view
=
new
View
;
$view
=
new
View
;
$view
->
context
=
$this
;
$view
->
_owner
=
$this
;
$name
=
!
YII_DEBUG
||
$exception
instanceof
HttpException
?
$this
->
errorView
:
$this
->
exceptionView
;
$name
=
!
YII_DEBUG
||
$exception
instanceof
HttpException
?
$this
->
errorView
:
$this
->
exceptionView
;
echo
$view
->
render
(
$name
,
array
(
echo
$view
->
render
(
$name
,
array
(
'exception'
=>
$exception
,
'exception'
=>
$exception
,
...
...
framework/base/Theme.php
View file @
bd320533
...
@@ -9,55 +9,114 @@
...
@@ -9,55 +9,114 @@
namespace
yii\base
;
namespace
yii\base
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
use
yii\util\FileHelper
;
/**
/**
* Theme represents an application theme.
* Theme represents an application theme.
*
*
* A theme is directory consisting of view and layout files which are meant to replace their
* non-themed counterparts.
*
* Theme uses [[pathMap]] to achieve the file replacement. A view or layout file will be replaced
* with its themed version if part of its path matches one of the keys in [[pathMap]].
* Then the matched part will be replaced with the corresponding array value.
*
* For example, if [[pathMap]] is `array('/www/views' => '/www/themes/basic')`,
* then the themed version for a view file `/www/views/site/index.php` will be
* `/www/themes/basic/site/index.php`.
*
* @property string $baseUrl the base URL for this theme. This is mainly used by [[getUrl()]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Theme
extends
Component
class
Theme
extends
Component
{
{
/**
* @var string the root path of this theme.
* @see pathMap
*/
public
$basePath
;
public
$basePath
;
public
$baseUrl
;
/**
* @var array the mapping between view directories and their corresponding themed versions.
* If not set, it will be initialized as a mapping from [[Application::basePath]] to [[basePath]].
* This property is used by [[apply()]] when a view is trying to apply the theme.
*/
public
$pathMap
;
private
$_baseUrl
;
/**
* Initializes the theme.
* @throws InvalidConfigException if [[basePath]] is not set.
*/
public
function
init
()
public
function
init
()
{
{
parent
::
init
();
if
(
empty
(
$this
->
pathMap
))
{
if
(
$this
->
basePath
!==
null
)
{
if
(
$this
->
basePath
!==
null
)
{
$this
->
basePath
=
\Yii
::
getAlias
(
$this
->
basePath
,
true
);
$this
->
basePath
=
FileHelper
::
ensureDirectory
(
$this
->
basePath
);
$this
->
pathMap
=
array
(
Yii
::
$application
->
getBasePath
()
=>
$this
->
basePath
);
}
else
{
}
else
{
throw
new
InvalidConfigException
(
"Theme.
basePath must be set."
);
throw
new
InvalidConfigException
(
"Theme::
basePath must be set."
);
}
}
if
(
$this
->
baseUrl
!==
null
)
{
$this
->
baseUrl
=
\Yii
::
getAlias
(
$this
->
baseUrl
,
true
);
}
else
{
throw
new
InvalidConfigException
(
"Theme.baseUrl must be set."
);
}
}
$paths
=
array
();
foreach
(
$this
->
pathMap
as
$from
=>
$to
)
{
$paths
[
FileHelper
::
normalizePath
(
$from
)
.
DIRECTORY_SEPARATOR
]
=
FileHelper
::
normalizePath
(
$to
)
.
DIRECTORY_SEPARATOR
;
}
$this
->
pathMap
=
$paths
;
}
}
/**
/**
* @param Application|Module|Controller|Object $context
* Returns the base URL for this theme.
* @return string
* The method [[getUrl()]] will prefix this to the given URL.
* @return string the base URL for this theme.
*/
*/
public
function
get
ViewPath
(
$context
=
null
)
public
function
get
BaseUrl
(
)
{
{
$viewPath
=
$this
->
basePath
.
DIRECTORY_SEPARATOR
.
'views'
;
return
$this
->
_baseUrl
;
if
(
$context
===
null
||
$context
instanceof
Application
)
{
}
return
$viewPath
;
}
elseif
(
$context
instanceof
Controller
||
$context
instanceof
Module
)
{
/**
return
$viewPath
.
DIRECTORY_SEPARATOR
.
$context
->
getUniqueId
();
* Sets the base URL for this theme.
}
else
{
* @param string $value the base URL for this theme.
return
$viewPath
.
DIRECTORY_SEPARATOR
.
str_replace
(
'\\'
,
'_'
,
get_class
(
$context
));
*/
public
function
setBaseUrl
(
$value
)
{
$this
->
_baseUrl
=
rtrim
(
Yii
::
getAlias
(
$value
),
'/'
);
}
/**
* Converts a file to a themed file if possible.
* If there is no corresponding themed file, the original file will be returned.
* @param string $path the file to be themed
* @return string the themed file, or the original file if the themed version is not available.
*/
public
function
apply
(
$path
)
{
$path
=
FileHelper
::
normalizePath
(
$path
);
foreach
(
$this
->
pathMap
as
$from
=>
$to
)
{
if
(
strpos
(
$path
,
$from
)
===
0
)
{
$n
=
strlen
(
$from
);
$file
=
$to
.
substr
(
$path
,
$n
);
if
(
is_file
(
$file
))
{
return
$file
;
}
}
}
}
return
$path
;
}
}
/**
/**
* @param Module $module
* Converts a relative URL into an absolute URL using [[basePath]].
* @return string
* @param string $url the relative URL to be converted.
* @return string the absolute URL
*/
*/
public
function
get
LayoutPath
(
$module
=
nul
l
)
public
function
get
Url
(
$ur
l
)
{
{
return
$this
->
getViewPath
(
$module
)
.
DIRECTORY_SEPARATOR
.
'layouts'
;
return
$this
->
baseUrl
.
'/'
.
ltrim
(
$url
,
'/'
)
;
}
}
}
}
framework/base/View.php
View file @
bd320533
This diff is collapsed.
Click to expand it.
framework/base/Widget.php
View file @
bd320533
...
@@ -102,4 +102,16 @@ class Widget extends Component
...
@@ -102,4 +102,16 @@ class Widget extends Component
{
{
return
new
View
(
$this
);
return
new
View
(
$this
);
}
}
/**
* Returns the directory containing the view files for this widget.
* The default implementation returns the 'views' subdirectory under the directory containing the widget class file.
* @return string the directory containing the view files for this widget.
*/
public
function
getViewPath
()
{
$className
=
get_class
(
$this
);
$class
=
new
\ReflectionClass
(
$className
);
return
dirname
(
$class
->
getFileName
())
.
DIRECTORY_SEPARATOR
.
'views'
;
}
}
}
\ No newline at end of file
framework/util/FileHelper.php
View file @
bd320533
...
@@ -51,6 +51,20 @@ class FileHelper
...
@@ -51,6 +51,20 @@ class FileHelper
}
}
/**
/**
* Normalizes a file/directory path.
* After normalization, the directory separators in the path will be `DIRECTORY_SEPARATOR`,
* and any trailing directory separators will be removed. For example, '/home\demo/' on Linux
* will be normalized as '/home/demo'.
* @param string $path the file/directory path to be normalized
* @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`.
* @return string the normalized file/directory path
*/
public
static
function
normalizePath
(
$path
,
$ds
=
DIRECTORY_SEPARATOR
)
{
return
rtrim
(
strtr
(
$path
,
array
(
'/'
=>
$ds
,
'\\'
=>
$ds
)),
$ds
);
}
/**
* 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