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
7dc893d7
Commit
7dc893d7
authored
Jul 19, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
w
parent
28af2924
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
142 additions
and
177 deletions
+142
-177
YiiBase.php
framework/YiiBase.php
+142
-177
No files found.
framework/YiiBase.php
View file @
7dc893d7
...
@@ -62,14 +62,15 @@ class YiiBase
...
@@ -62,14 +62,15 @@ class YiiBase
* @var yii\base\Application the application instance
* @var yii\base\Application the application instance
*/
*/
public
static
$app
;
public
static
$app
;
/**
private
static
$_aliases
=
array
(
* @var array registered path aliases
*/
public
static
$aliases
=
array
(
'@yii'
=>
YII_PATH
,
'@yii'
=>
YII_PATH
,
);
);
private
static
$_imports
=
array
();
// alias => class name or directory
private
static
$_app
;
private
static
$_logger
;
private
static
$_imported
=
array
();
// alias => class name or directory
private
static
$_logger
;
/**
/**
...
@@ -114,187 +115,76 @@ class YiiBase
...
@@ -114,187 +115,76 @@ class YiiBase
}
}
/**
/**
* Creates an object and initializes it based on the given configuration.
*
* The specified configuration can be either a string or an array.
* If the former, the string is treated as the object type which can
* be either the class name or {@link YiiBase::getPathOfAlias class path alias}.
* If the latter, the 'class' element is treated as the object type,
* and the rest name-value pairs in the array are used to initialize
* the corresponding object properties.
*
* Any additional parameters passed to this method will be
* passed to the constructor of the object being created.
*
* NOTE: the array-typed configuration has been supported since version 1.0.1.
*
* @param mixed $config the configuration. It can be either a string or an array.
* @return mixed the created object
* @throws CException if the configuration does not have a 'class' element.
*/
public
static
function
createComponent
(
$config
)
{
if
(
is_string
(
$config
))
{
$type
=
$config
;
$config
=
array
();
}
elseif
(
isset
(
$config
[
'class'
]))
{
$type
=
$config
[
'class'
];
unset
(
$config
[
'class'
]);
}
else
throw
new
CException
(
Yii
::
t
(
'yii'
,
'Object configuration must be an array containing a "class" element.'
));
if
(
!
class_exists
(
$type
,
false
))
$type
=
Yii
::
import
(
$type
,
true
);
if
((
$n
=
func_num_args
())
>
1
)
{
$args
=
func_get_args
();
if
(
$n
===
2
)
$object
=
new
$type
(
$args
[
1
]);
elseif
(
$n
===
3
)
$object
=
new
$type
(
$args
[
1
],
$args
[
2
]);
elseif
(
$n
===
4
)
$object
=
new
$type
(
$args
[
1
],
$args
[
2
],
$args
[
3
]);
else
{
unset
(
$args
[
0
]);
$class
=
new
ReflectionClass
(
$type
);
// Note: ReflectionClass::newInstanceArgs() is available for PHP 5.1.3+
// $object=$class->newInstanceArgs($args);
$object
=
call_user_func_array
(
array
(
$class
,
'newInstance'
),
$args
);
}
}
else
$object
=
new
$type
;
foreach
(
$config
as
$key
=>
$value
)
$object
->
$key
=
$value
;
return
$object
;
}
/**
* Imports a class or a directory.
* Imports a class or a directory.
*
*
* Importing a class is like including the corresponding class file.
* Importing a class is like including the corresponding class file.
* The main difference is that importing a class is much lighter because it only
* The main difference is that importing a class is much lighter because it only
* includes the class file when the class is referenced the first time.
* includes the class file when the class is referenced in the code the first time.
*
* Importing a directory is equivalent to adding a directory into the PHP include path.
* If multiple directories are imported, the directories imported later will take
* precedence in class file searching (i.e., they are added to the front of the PHP include path).
*
*
*
Path aliases are used to import a class or directory. For example,
*
Importing a directory will add the directory to the front of the [[classPath]] array.
*
<ul>
*
When [[autoload]] is loading an unknown class, it will search in the directories
*
<li><code>application.components.GoogleMap</code>: import the <code>GoogleMap</code> class.</li>
*
specified in [[classPath]] to find the corresponding class file to include.
*
<li><code>application.components.*</code>: import the <code>components</code> directory.</li>
*
For this reason, if multiple directories are imported, the directories imported later
*
</ul>
*
will take precedence in class file searching.
*
*
* The same
path alias can be imported multiple times, but only the first time is effective.
* The same
class or directory can be imported multiple times. Only the first importing
* Importing a directory does not import any of its subdirectories.
*
will count.
Importing a directory does not import any of its subdirectories.
*
*
* Starting from version 1.1.5, this method can also be used to import a class in namespace format
* To import a class or a directory, one can use either path alias or class name (can be namespaced):
* (available for PHP 5.3 or above only). It is similar to importing a class in path alias format,
* except that the dot separator is replaced by the backslash separator. For example, importing
* <code>application\components\GoogleMap</code> is similar to importing <code>application.components.GoogleMap</code>.
* The difference is that the former class is using qualified name, while the latter unqualified.
*
*
* Note, importing a class in namespace format requires that the namespace is corresponding to
* - `@app/components/GoogleMap`: importing the `GoogleMap` class with a path alias;
* a valid path alias if we replace the backslash characters with dot characters.
* - `GoogleMap`: importing the `GoogleMap` class with a class name;
* For example, the namespace <code>application\components</code> must correspond to a valid
* - `@app/components/*`: importing the whole `components` directory with a path alias.
* path alias <code>application.components</code>.
*
*
* @param string $alias path alias to be imported
* @param string $alias path alias
or a simple class name
to be imported
* @param boolean $forceInclude whether to include the class file immediately. If false, the class file
* @param boolean $forceInclude whether to include the class file immediately. If false, the class file
* will be included only when the class is being used. This parameter is used only when
* will be included only when the class is being used. This parameter is used only when
* the path alias refers to a class.
* the path alias refers to a class.
* @return string the class name or the directory that this alias refers to
* @return string the class name or the directory that this alias refers to
* @throws
CException if the
alias is invalid
* @throws
Exception if the path
alias is invalid
*/
*/
public
static
function
import
(
$alias
,
$forceInclude
=
false
)
public
static
function
import
(
$alias
,
$forceInclude
=
false
)
{
{
if
(
isset
(
self
::
$_imports
[
$alias
]))
// previously imported
if
(
isset
(
self
::
$_imported
[
$alias
]))
{
return
self
::
$_imports
[
$alias
];
return
self
::
$_imported
[
$alias
];
}
if
(
class_exists
(
$alias
,
false
)
||
interface_exists
(
$alias
,
false
))
return
self
::
$_imports
[
$alias
]
=
$alias
;
if
((
$pos
=
strrpos
(
$alias
,
'\\'
))
!==
false
)
// a class name in PHP 5.3 namespace format
if
(
class_exists
(
$alias
,
false
)
||
interface_exists
(
$alias
,
false
))
{
{
return
self
::
$_imported
[
$alias
]
=
$alias
;
$namespace
=
str_replace
(
'\\'
,
'.'
,
ltrim
(
substr
(
$alias
,
0
,
$pos
),
'\\'
));
if
((
$path
=
self
::
getPathOfAlias
(
$namespace
))
!==
false
)
{
$classFile
=
$path
.
DIRECTORY_SEPARATOR
.
substr
(
$alias
,
$pos
+
1
)
.
'.php'
;
if
(
$forceInclude
)
{
if
(
is_file
(
$classFile
))
require
(
$classFile
);
else
throw
new
CException
(
Yii
::
t
(
'yii'
,
'Alias "{alias}" is invalid. Make sure it points to an existing PHP file.'
,
array
(
'{alias}'
=>
$alias
)));
self
::
$_imports
[
$alias
]
=
$alias
;
}
else
self
::
$classMap
[
$alias
]
=
$classFile
;
return
$alias
;
}
else
throw
new
CException
(
Yii
::
t
(
'yii'
,
'Alias "{alias}" is invalid. Make sure it points to an existing directory.'
,
array
(
'{alias}'
=>
$namespace
)));
}
}
if
(
(
$pos
=
strrpos
(
$alias
,
'.'
))
===
false
)
// a simple class name
if
(
$alias
[
0
]
!==
'@'
)
{
// a simple class name
{
if
(
$forceInclude
&&
self
::
autoload
(
$alias
))
{
if
(
$forceInclude
&&
self
::
autoload
(
$alias
))
self
::
$_imported
[
$alias
]
=
$alias
;
self
::
$_imports
[
$alias
]
=
$alias
;
}
return
$alias
;
return
$alias
;
}
}
$className
=
(
string
)
substr
(
$alias
,
$pos
+
1
);
$className
=
basename
(
$alias
);
$isClass
=
$className
!==
'*'
;
$isClass
=
$className
!==
'*'
;
if
(
$isClass
&&
(
class_exists
(
$className
,
false
)
||
interface_exists
(
$className
,
false
)))
if
(
$isClass
&&
(
class_exists
(
$className
,
false
)
||
interface_exists
(
$className
,
false
)))
{
return
self
::
$_imports
[
$alias
]
=
$className
;
return
self
::
$_imported
[
$alias
]
=
$className
;
}
if
((
$path
=
self
::
getPathOfAlias
(
$alias
))
!==
false
)
{
if
(
$isClass
)
{
if
(
$forceInclude
)
{
if
(
is_file
(
$path
.
'.php'
))
require
(
$path
.
'.php'
);
else
throw
new
CException
(
Yii
::
t
(
'yii'
,
'Alias "{alias}" is invalid. Make sure it points to an existing PHP file.'
,
array
(
'{alias}'
=>
$alias
)));
self
::
$_imports
[
$alias
]
=
$className
;
}
else
self
::
$classMap
[
$className
]
=
$path
.
'.php'
;
return
$className
;
}
else
// a directory
{
if
(
self
::
$classPath
===
null
)
{
self
::
$classPath
=
array_unique
(
explode
(
PATH_SEPARATOR
,
get_include_path
()));
if
((
$pos
=
array_search
(
'.'
,
self
::
$classPath
,
true
))
!==
false
)
unset
(
self
::
$classPath
[
$pos
]);
}
array_unshift
(
self
::
$classPath
,
$path
);
if
(
self
::
$useIncludePath
&&
set_include_path
(
'.'
.
PATH_SEPARATOR
.
implode
(
PATH_SEPARATOR
,
self
::
$classPath
))
===
false
)
if
((
$path
=
self
::
getPathOfAlias
(
dirname
(
$alias
)))
===
false
)
{
self
::
$useIncludePath
=
false
;
throw
new
\yii\base\Exception
(
'Invalid path alias: '
.
$alias
);
}
return
self
::
$_imports
[
$alias
]
=
$path
;
if
(
$isClass
)
{
if
(
$forceInclude
)
{
require
(
$path
.
"/
$className
.php"
);
self
::
$_imported
[
$alias
]
=
$className
;
}
}
else
{
self
::
$classMap
[
$className
]
=
$path
.
"/
$className
.php"
;
}
return
$className
;
}
else
{
// a directory
array_unshift
(
self
::
$classPath
,
$path
);
return
self
::
$_imported
[
$alias
]
=
$path
;
}
}
else
throw
new
CException
(
Yii
::
t
(
'yii'
,
'Alias "{alias}" is invalid. Make sure it points to an existing directory or file.'
,
array
(
'{alias}'
=>
$alias
)));
}
}
/**
/**
...
@@ -310,13 +200,13 @@ class YiiBase
...
@@ -310,13 +200,13 @@ class YiiBase
*/
*/
public
static
function
getPathOfAlias
(
$alias
)
public
static
function
getPathOfAlias
(
$alias
)
{
{
if
(
isset
(
self
::
$
_
aliases
[
$alias
]))
{
if
(
isset
(
self
::
$aliases
[
$alias
]))
{
return
self
::
$
_
aliases
[
$alias
];
return
self
::
$aliases
[
$alias
];
}
}
elseif
((
$pos
=
strpos
(
$alias
,
'/'
))
!==
false
)
{
elseif
((
$pos
=
strpos
(
$alias
,
'/'
))
!==
false
)
{
$rootAlias
=
substr
(
$alias
,
0
,
$pos
);
$rootAlias
=
substr
(
$alias
,
0
,
$pos
);
if
(
isset
(
self
::
$
_
aliases
[
$rootAlias
]))
{
if
(
isset
(
self
::
$aliases
[
$rootAlias
]))
{
return
self
::
$
_aliases
[
$alias
]
=
self
::
$_
aliases
[
$rootAlias
]
.
substr
(
$alias
,
$pos
);
return
self
::
$
aliases
[
$alias
]
=
self
::
$
aliases
[
$rootAlias
]
.
substr
(
$alias
,
$pos
);
}
}
}
}
return
false
;
return
false
;
...
@@ -335,10 +225,10 @@ class YiiBase
...
@@ -335,10 +225,10 @@ class YiiBase
public
static
function
setPathOfAlias
(
$alias
,
$path
)
public
static
function
setPathOfAlias
(
$alias
,
$path
)
{
{
if
(
$path
===
null
)
{
if
(
$path
===
null
)
{
unset
(
self
::
$
_
aliases
[
$alias
]);
unset
(
self
::
$aliases
[
$alias
]);
}
}
else
{
else
{
self
::
$
_
aliases
[
$alias
]
=
rtrim
(
$path
,
'\\/'
);
self
::
$aliases
[
$alias
]
=
rtrim
(
$path
,
'\\/'
);
}
}
}
}
...
@@ -370,7 +260,7 @@ class YiiBase
...
@@ -370,7 +260,7 @@ class YiiBase
// namespaced class, e.g. yii\base\Component
// namespaced class, e.g. yii\base\Component
if
(
strpos
(
$className
,
'\\'
)
!==
false
)
{
if
(
strpos
(
$className
,
'\\'
)
!==
false
)
{
// convert namespace to path alias, e.g. yii\base\Component to @yii/base/Component
// convert namespace to path alias, e.g. yii\base\Component to @yii/base/Component
$alias
=
'@'
.
str_replace
(
'\\'
,
'/'
,
$className
);
$alias
=
'@'
.
str_replace
(
'\\'
,
'/'
,
ltrim
(
$className
,
'\\'
)
);
if
((
$path
=
self
::
getPathOfAlias
(
$alias
))
!==
false
)
{
if
((
$path
=
self
::
getPathOfAlias
(
$alias
))
!==
false
)
{
include
(
$path
.
'.php'
);
include
(
$path
.
'.php'
);
return
true
;
return
true
;
...
@@ -401,6 +291,79 @@ class YiiBase
...
@@ -401,6 +291,79 @@ class YiiBase
}
}
/**
/**
* Creates an object and initializes its properties based on the given configuration.
*
* The specified configuration can be either a string or an array.
* If the former, the string is treated as the object type which can
* be either a class name or [[getPathOfAlias|path alias]].
* If the latter, the array must contain a `class` element which refers
* to a class name or [[getPathOfAlias|path alias]]. The rest of the name-value
* pairs in the array will be used to initialize the corresponding object properties.
* For example,
*
* Any additional parameters passed to this method will be
* passed to the constructor of the object being created.
*
* ~~~php
* $component = Yii::createComponent('@app/components/GoogleMap');
* $component = Yii::createComponent('\application\components\GoogleMap');
* $component = Yii::createComponent(array(
* 'class' => '@app/components/GoogleMap',
* 'apiKey' => 'xyz',
* ));
* ~~~
*
* @param mixed $config the configuration. It can be either a string or an array.
* @return mixed the created object
* @throws Exception if the configuration does not have a 'class' element.
*/
public
static
function
createComponent
(
$config
)
{
if
(
is_string
(
$config
))
{
$type
=
$config
;
$config
=
array
();
}
elseif
(
isset
(
$config
[
'class'
]))
{
$type
=
$config
[
'class'
];
unset
(
$config
[
'class'
]);
}
else
{
throw
new
\yii\base\Exception
(
'Object configuration must be an array containing a "class" element.'
);
}
if
(
!
class_exists
(
$type
,
false
))
{
$type
=
Yii
::
import
(
$type
,
true
);
}
if
((
$n
=
func_num_args
())
>
1
)
{
$args
=
func_get_args
();
if
(
$n
===
2
)
{
$object
=
new
$type
(
$args
[
1
]);
}
elseif
(
$n
===
3
)
{
$object
=
new
$type
(
$args
[
1
],
$args
[
2
]);
}
elseif
(
$n
===
4
)
{
$object
=
new
$type
(
$args
[
1
],
$args
[
2
],
$args
[
3
]);
}
else
{
unset
(
$args
[
0
]);
$class
=
new
ReflectionClass
(
$type
);
$object
=
$class
->
newInstanceArgs
(
$args
);
}
}
else
{
$object
=
new
$type
;
}
foreach
(
$config
as
$key
=>
$value
)
{
$object
->
$key
=
$value
;
}
return
$object
;
}
/**
* Writes a trace message.
* Writes a trace message.
* This method will only log a message when the application is in debug mode.
* This method will only log a message when the application is in debug mode.
* @param string $msg message to be logged
* @param string $msg message to be logged
...
@@ -484,20 +447,22 @@ class YiiBase
...
@@ -484,20 +447,22 @@ class YiiBase
}
}
/**
/**
* @return CLogger message logger
* Returns the message logger object.
* @return \yii\logging\Logger message logger
*/
*/
public
static
function
getLogger
()
public
static
function
getLogger
()
{
{
if
(
self
::
$_logger
!==
null
)
if
(
self
::
$_logger
!==
null
)
{
return
self
::
$_logger
;
return
self
::
$_logger
;
else
}
return
self
::
$_logger
=
new
CLogger
;
else
{
return
self
::
$_logger
=
new
\yii\logging\Logger
;
}
}
}
/**
/**
* Sets the logger object.
* Sets the logger object.
* @param CLogger $logger the logger object.
* @param \yii\logging\Logger $logger the logger object.
* @since 1.1.8
*/
*/
public
static
function
setLogger
(
$logger
)
public
static
function
setLogger
(
$logger
)
{
{
...
@@ -505,8 +470,8 @@ class YiiBase
...
@@ -505,8 +470,8 @@ class YiiBase
}
}
/**
/**
* Returns a
string that can be displayed on your Web page showing Powered-by-Yii information
* Returns a
n HTML hyperlink that can be displayed on your Web page showing Powered by Yii" information.
* @return string a
string that can be displayed on your Web page showing Powered-by-Yii
information
* @return string a
n HTML hyperlink that can be displayed on your Web page showing Powered by Yii"
information
*/
*/
public
static
function
powered
()
public
static
function
powered
()
{
{
...
@@ -539,11 +504,11 @@ class YiiBase
...
@@ -539,11 +504,11 @@ class YiiBase
*/
*/
public
static
function
t
(
$category
,
$message
,
$params
=
array
(),
$source
=
null
,
$language
=
null
)
public
static
function
t
(
$category
,
$message
,
$params
=
array
(),
$source
=
null
,
$language
=
null
)
{
{
if
(
self
::
$
_
app
!==
null
)
if
(
self
::
$app
!==
null
)
{
{
if
(
$source
===
null
)
if
(
$source
===
null
)
$source
=
(
$category
===
'yii'
||
$category
===
'zii'
)
?
'coreMessages'
:
'messages'
;
$source
=
$category
===
'yii'
?
'coreMessages'
:
'messages'
;
if
((
$source
=
self
::
$
_
app
->
getComponent
(
$source
))
!==
null
)
if
((
$source
=
self
::
$app
->
getComponent
(
$source
))
!==
null
)
$message
=
$source
->
translate
(
$category
,
$message
,
$language
);
$message
=
$source
->
translate
(
$category
,
$message
,
$language
);
}
}
if
(
$params
===
array
())
if
(
$params
===
array
())
...
@@ -557,7 +522,7 @@ class YiiBase
...
@@ -557,7 +522,7 @@ class YiiBase
if
(
strpos
(
$message
,
'#'
)
===
false
)
if
(
strpos
(
$message
,
'#'
)
===
false
)
{
{
$chunks
=
explode
(
'|'
,
$message
);
$chunks
=
explode
(
'|'
,
$message
);
$expressions
=
self
::
$
_
app
->
getLocale
(
$language
)
->
getPluralRules
();
$expressions
=
self
::
$app
->
getLocale
(
$language
)
->
getPluralRules
();
if
(
$n
=
min
(
count
(
$chunks
),
count
(
$expressions
)))
if
(
$n
=
min
(
count
(
$chunks
),
count
(
$expressions
)))
{
{
for
(
$i
=
0
;
$i
<
$n
;
$i
++
)
for
(
$i
=
0
;
$i
<
$n
;
$i
++
)
...
...
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