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
5e830689
Commit
5e830689
authored
Nov 11, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored component base classes.
parent
5357388d
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
319 additions
and
22 deletions
+319
-22
YiiBase.php
framework/YiiBase.php
+4
-4
Behavior.php
framework/base/Behavior.php
+1
-1
Component.php
framework/base/Component.php
+0
-0
Dictionary.php
framework/base/Dictionary.php
+10
-3
Event.php
framework/base/Event.php
+1
-1
Initable.php
framework/base/Initable.php
+2
-2
Model.php
framework/base/Model.php
+1
-1
Module.php
framework/base/Module.php
+3
-3
Object.php
framework/base/Object.php
+282
-0
Vector.php
framework/base/Vector.php
+10
-2
Connection.php
framework/db/dao/Connection.php
+2
-2
Router.php
framework/logging/Router.php
+2
-2
Validator.php
framework/validators/Validator.php
+1
-1
No files found.
framework/YiiBase.php
View file @
5e830689
...
@@ -290,9 +290,9 @@ class YiiBase
...
@@ -290,9 +290,9 @@ class YiiBase
* the class. For example,
* the class. For example,
*
*
* ~~~
* ~~~
* $component = Yii::create
Component
('@app/components/GoogleMap');
* $component = Yii::create('@app/components/GoogleMap');
* $component = Yii::create
Component
('\application\components\GoogleMap');
* $component = Yii::create('\application\components\GoogleMap');
* $component = Yii::create
Component
(array(
* $component = Yii::create(array(
* 'class' => '@app/components/GoogleMap',
* 'class' => '@app/components/GoogleMap',
* 'apiKey' => 'xyz',
* 'apiKey' => 'xyz',
* ));
* ));
...
@@ -310,7 +310,7 @@ class YiiBase
...
@@ -310,7 +310,7 @@ class YiiBase
* @return mixed the created object
* @return mixed the created object
* @throws \yii\base\Exception if the configuration is invalid.
* @throws \yii\base\Exception if the configuration is invalid.
*/
*/
public
static
function
create
Component
(
$config
)
public
static
function
create
(
$config
)
{
{
if
(
is_string
(
$config
))
{
if
(
is_string
(
$config
))
{
$class
=
$config
;
$class
=
$config
;
...
...
framework/base/Behavior.php
View file @
5e830689
...
@@ -19,7 +19,7 @@ namespace yii\base;
...
@@ -19,7 +19,7 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Behavior
extends
Componen
t
class
Behavior
extends
Objec
t
{
{
private
$_owner
;
private
$_owner
;
...
...
framework/base/Component.php
View file @
5e830689
This diff is collapsed.
Click to expand it.
framework/base/Dictionary.php
View file @
5e830689
...
@@ -31,7 +31,7 @@ namespace yii\base;
...
@@ -31,7 +31,7 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Dictionary
extends
Componen
t
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
class
Dictionary
extends
Objec
t
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
{
{
/**
/**
* @var array internal data storage
* @var array internal data storage
...
@@ -140,13 +140,20 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
...
@@ -140,13 +140,20 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
}
}
/**
/**
* Removes all items in the dictionary.
* Removes all items from the dictionary.
* @param boolean $safeClear whether to clear every item by calling [[remove]].
* Defaults to false, meaning all items in the dictionary will be cleared directly
* without calling [[remove]].
*/
*/
public
function
clear
()
public
function
clear
(
$safeClear
=
false
)
{
{
if
(
$safeClear
)
{
foreach
(
array_keys
(
$this
->
_d
)
as
$key
)
{
foreach
(
array_keys
(
$this
->
_d
)
as
$key
)
{
$this
->
remove
(
$key
);
$this
->
remove
(
$key
);
}
}
}
else
{
$this
->
_d
=
array
();
}
}
}
/**
/**
...
...
framework/base/Event.php
View file @
5e830689
...
@@ -22,7 +22,7 @@ namespace yii\base;
...
@@ -22,7 +22,7 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Event
extends
Componen
t
class
Event
extends
Objec
t
{
{
/**
/**
* @var string the event name. This property is set by [[Component::raiseEvent]].
* @var string the event name. This property is set by [[Component::raiseEvent]].
...
...
framework/base/Initable.php
View file @
5e830689
...
@@ -13,7 +13,7 @@ namespace yii\base;
...
@@ -13,7 +13,7 @@ namespace yii\base;
* Initable is an interface indicating a class needs initialization to work properly.
* Initable is an interface indicating a class needs initialization to work properly.
*
*
* Initable requires a class to implement the [[init]] method.
* Initable requires a class to implement the [[init]] method.
* When [[\Yii::create
Component
]] is being used to create a new component which implements
* When [[\Yii::create]] is being used to create a new component which implements
* Initable, it will call the [[init]] method after setting the initial values of the
* Initable, it will call the [[init]] method after setting the initial values of the
* component properties.
* component properties.
*
*
...
@@ -24,7 +24,7 @@ interface Initable
...
@@ -24,7 +24,7 @@ interface Initable
{
{
/**
/**
* Initializes this component.
* Initializes this component.
* This method is invoked by [[\Yii::create
Component
]] after its creates the new
* This method is invoked by [[\Yii::create]] after its creates the new
* component instance and initializes the component properties. In other words,
* component instance and initializes the component properties. In other words,
* at this stage, the component has been fully configured.
* at this stage, the component has been fully configured.
*/
*/
...
...
framework/base/Model.php
View file @
5e830689
...
@@ -52,7 +52,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
...
@@ -52,7 +52,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
/**
/**
* Initializes this model.
* Initializes this model.
*
*
* This method is required by the [[Initable]] interface. It is invoked by [[\Yii::create
Component
]]
* This method is required by the [[Initable]] interface. It is invoked by [[\Yii::create]]
* after its creates the new model instance and initializes the model properties.
* after its creates the new model instance and initializes the model properties.
*
*
* The default implementation calls [[behaviors]] and registers any available behaviors.
* The default implementation calls [[behaviors]] and registers any available behaviors.
...
...
framework/base/Module.php
View file @
5e830689
...
@@ -257,9 +257,9 @@ abstract class Module extends Component
...
@@ -257,9 +257,9 @@ abstract class Module extends Component
$class
=
$config
[
'class'
];
$class
=
$config
[
'class'
];
unset
(
$config
[
'class'
],
$config
[
'enabled'
]);
unset
(
$config
[
'class'
],
$config
[
'enabled'
]);
if
(
$this
===
Yii
::
app
())
if
(
$this
===
Yii
::
app
())
$module
=
Yii
::
create
Component
(
$class
,
$id
,
null
,
$config
);
$module
=
Yii
::
create
(
$class
,
$id
,
null
,
$config
);
else
else
$module
=
Yii
::
create
Component
(
$class
,
$this
->
getId
()
.
'/'
.
$id
,
$this
,
$config
);
$module
=
Yii
::
create
(
$class
,
$this
->
getId
()
.
'/'
.
$id
,
$this
,
$config
);
return
$this
->
_modules
[
$id
]
=
$module
;
return
$this
->
_modules
[
$id
]
=
$module
;
}
}
}
}
...
@@ -362,7 +362,7 @@ abstract class Module extends Component
...
@@ -362,7 +362,7 @@ abstract class Module extends Component
{
{
Yii
::
trace
(
"Loading
\"
$id
\"
application component"
,
'system.CModule'
);
Yii
::
trace
(
"Loading
\"
$id
\"
application component"
,
'system.CModule'
);
unset
(
$config
[
'enabled'
]);
unset
(
$config
[
'enabled'
]);
$component
=
Yii
::
create
Component
(
$config
);
$component
=
Yii
::
create
(
$config
);
$component
->
init
();
$component
->
init
();
return
$this
->
_components
[
$id
]
=
$component
;
return
$this
->
_components
[
$id
]
=
$component
;
}
}
...
...
framework/base/Object.php
0 → 100644
View file @
5e830689
<?php
/**
* Object class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\base
;
/**
* Object is the base class that implements the *property* feature.
*
* A property is defined by a getter method (e.g. `getLabel`),
* and/or a setter method (e.g. `setLabel`). For example, the following
* getter and setter methods define a property named `label`:
*
* ~~~
* private $_label;
*
* public function getLabel()
* {
* return $this->_label;
* }
*
* public function setLabel($value)
* {
* $this->_label = $value;
* }
* ~~~
*
* A property can be accessed like a member variable of an object.
* Reading or writing a property will cause the invocation of the corresponding
* getter or setter method. For example,
*
* ~~~
* // equivalent to $label = $object->getLabel();
* $label = $object->label;
* // equivalent to $object->setLabel('abc');
* $object->label = 'abc';
* ~~~
*
* If a property only has a getter method and has no setter method, it is
* considered as *read-only*. In this case, trying to modify the property value
* will cause an exception.
*
* Property names are *case-insensitive*.
*
* One can call [[hasProperty]], [[canGetProperty]] and/or [[canSetProperty]]
* to check the existence of a property.
*
* Besides the property feature, the Object class defines a static method
* [[create]] which provides a convenient alternative way of creating a new
* object instance.
*
* The Object class also defines the [[evaluateExpression]] method so that a PHP
* expression or callback can be dynamically evaluated within the context of an object.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Object
{
/**
* Returns the value of a object property.
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `$value = $object->property;`.
* @param string $name the property name
* @return mixed the property value, event handlers attached to the event,
* the named behavior, or the value of a behavior's property
* @throws Exception if the property is not defined
* @see __set
*/
public
function
__get
(
$name
)
{
$getter
=
'get'
.
$name
;
if
(
method_exists
(
$this
,
$getter
))
{
return
$this
->
$getter
();
}
throw
new
Exception
(
'Getting unknown property: '
.
get_class
(
$this
)
.
'.'
.
$name
);
}
/**
* Sets value of a object property.
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `$object->property = $value;`.
* @param string $name the property name or the event name
* @param mixed $value the property value
* @throws Exception if the property is not defined or read-only.
* @see __get
*/
public
function
__set
(
$name
,
$value
)
{
$setter
=
'set'
.
$name
;
if
(
method_exists
(
$this
,
$setter
))
{
return
$this
->
$setter
(
$value
);
}
if
(
method_exists
(
$this
,
'get'
.
$name
))
{
throw
new
Exception
(
'Setting read-only property: '
.
get_class
(
$this
)
.
'.'
.
$name
);
}
else
{
throw
new
Exception
(
'Setting unknown property: '
.
get_class
(
$this
)
.
'.'
.
$name
);
}
}
/**
* Checks if the named property is set (not null).
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `isset($object->property)`.
*
* Note that if the property is not defined, false will be returned.
* @param string $name the property name or the event name
* @return boolean whether the named property is set (not null).
*/
public
function
__isset
(
$name
)
{
$getter
=
'get'
.
$name
;
if
(
method_exists
(
$this
,
$getter
))
{
// property is not null
return
$this
->
$getter
()
!==
null
;
}
return
false
;
}
/**
* Sets a object property to be null.
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `unset($object->property)`.
*
* Note that if the property is not defined, this method will do nothing.
* If the property is read-only, it will throw an exception.
* @param string $name the property name
* @throws Exception if the property is read only.
*/
public
function
__unset
(
$name
)
{
$setter
=
'set'
.
$name
;
if
(
method_exists
(
$this
,
$setter
))
{
// write property
$this
->
$setter
(
null
);
}
elseif
(
method_exists
(
$this
,
'get'
.
$name
))
{
throw
new
Exception
(
'Unsetting read-only property: '
.
get_class
(
$this
)
.
'.'
.
$name
);
}
}
/**
* Returns a value indicating whether a property is defined.
* A property is defined if there is a getter or setter method
* defined in the class. Note that property names are case-insensitive.
* @param string $name the property name
* @return boolean whether the property is defined
* @see canGetProperty
* @see canSetProperty
*/
public
function
hasProperty
(
$name
)
{
return
$this
->
canGetProperty
(
$name
)
||
$this
->
canSetProperty
(
$name
);
}
/**
* Returns a value indicating whether a property can be read.
* A property can be read if the class has a getter method
* for the property name. Note that property name is case-insensitive.
* @param string $name the property name
* @return boolean whether the property can be read
* @see canSetProperty
*/
public
function
canGetProperty
(
$name
)
{
return
method_exists
(
$this
,
'get'
.
$name
);
}
/**
* Returns a value indicating whether a property can be set.
* A property can be written if the class has a setter method
* for the property name. Note that property name is case-insensitive.
* @param string $name the property name
* @return boolean whether the property can be written
* @see canGetProperty
*/
public
function
canSetProperty
(
$name
)
{
return
method_exists
(
$this
,
'set'
.
$name
);
}
/**
* Evaluates a PHP expression or callback under the context of this object.
*
* Valid PHP callback can be class method name in the form of
* array(ClassName/Object, MethodName), or anonymous function.
*
* If a PHP callback is used, the corresponding function/method signature should be
*
* ~~~
* function foo($param1, $param2, ..., $object) { ... }
* ~~~
*
* where the array elements in the second parameter to this method will be passed
* to the callback as `$param1`, `$param2`, ...; and the last parameter will be the object itself.
*
* If a PHP expression is used, the second parameter will be "extracted" into PHP variables
* that can be directly accessed in the expression.
* See [PHP extract](http://us.php.net/manual/en/function.extract.php)
* for more details. In the expression, the object can be accessed using `$this`.
*
* @param mixed $_expression_ a PHP expression or PHP callback to be evaluated.
* @param array $_data_ additional parameters to be passed to the above expression/callback.
* @return mixed the expression result
*/
public
function
evaluateExpression
(
$_expression_
,
$_data_
=
array
())
{
if
(
is_string
(
$_expression_
))
{
extract
(
$_data_
);
return
eval
(
'return '
.
$_expression_
.
';'
);
}
else
{
$_data_
[]
=
$this
;
return
call_user_func_array
(
$_expression_
,
$_data_
);
}
}
/**
* Creates a new object instance.
*
* This method calls [[\Yii::create]] to create the new object instance.
*
* This method differs from the PHP `new` operator in that it does the following
* steps to create a new object instance:
*
* - Call class constructor (same the `new` operator);
* - Initialize the object properties using the name-value pairs given as the
* last parameter to this method;
* - Call [[Initable::init|init]] if the class implements [[Initable]].
*
* Parameters passed to this method will be used as the parameters to the object
* constructor.
*
* Additionally, one can pass in an associative array as the last parameter to
* this method. This method will treat the array as name-value pairs that initialize
* the corresponding object properties. For example,
*
* ~~~
* class Foo extends \yii\base\Object
* {
* public $c;
* public function __construct($a, $b)
* {
* ...
* }
* }
*
* $model = Foo::create(1, 2, array('c' => 3));
* // which is equivalent to the following lines:
* $model = new Foo(1, 2);
* $model->c = 3;
* ~~~
*
* @return object the created object
* @throws Exception if the configuration is invalid.
*/
public
static
function
create
()
{
$class
=
'\\'
.
get_called_class
();
if
((
$n
=
func_num_args
())
>
0
)
{
$args
=
func_get_args
();
if
(
is_array
(
$args
[
$n
-
1
]))
{
// the last parameter could be configuration array
$method
=
new
\ReflectionMethod
(
$class
,
'__construct'
);
if
(
$method
->
getNumberOfParameters
()
+
1
==
$n
)
{
$config
=
$args
[
$n
-
1
];
array_pop
(
$args
);
}
}
$config
[
'class'
]
=
$class
;
array_unshift
(
$args
,
$config
);
return
call_user_func_array
(
'\Yii::create'
,
$args
);
}
else
{
return
\Yii
::
create
(
$class
);
}
}
}
framework/base/Vector.php
View file @
5e830689
...
@@ -37,7 +37,7 @@ namespace yii\base;
...
@@ -37,7 +37,7 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Vector
extends
Componen
t
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
class
Vector
extends
Objec
t
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
{
{
/**
/**
* @var array internal data storage
* @var array internal data storage
...
@@ -193,12 +193,20 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
...
@@ -193,12 +193,20 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
/**
/**
* Removes all items from the vector.
* Removes all items from the vector.
* @param boolean $safeClear whether to clear every item by calling [[removeAt]].
* Defaults to false, meaning all items in the vector will be cleared directly
* without calling [[removeAt]].
*/
*/
public
function
clear
()
public
function
clear
(
$safeClear
=
false
)
{
{
if
(
$safeClear
)
{
for
(
$i
=
$this
->
_c
-
1
;
$i
>=
0
;
--
$i
)
{
for
(
$i
=
$this
->
_c
-
1
;
$i
>=
0
;
--
$i
)
{
$this
->
removeAt
(
$i
);
$this
->
removeAt
(
$i
);
}
}
}
else
{
$this
->
_d
=
array
();
$this
->
_c
=
0
;
}
}
}
/**
/**
...
...
framework/db/dao/Connection.php
View file @
5e830689
...
@@ -227,7 +227,7 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -227,7 +227,7 @@ class Connection extends \yii\base\ApplicationComponent
/**
/**
* @var array mapping between PDO driver names and [[Schema]] classes.
* @var array mapping between PDO driver names and [[Schema]] classes.
* The keys of the array are PDO driver names while the values the corresponding
* The keys of the array are PDO driver names while the values the corresponding
* schema class name or configuration. Please refer to [[\Yii::create
Component
]] for
* schema class name or configuration. Please refer to [[\Yii::create]] for
* details on how to specify a configuration.
* details on how to specify a configuration.
*
*
* This property is mainly used by [[getSchema]] when fetching the database schema information.
* This property is mainly used by [[getSchema]] when fetching the database schema information.
...
@@ -463,7 +463,7 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -463,7 +463,7 @@ class Connection extends \yii\base\ApplicationComponent
else
{
else
{
$driver
=
$this
->
getDriverName
();
$driver
=
$this
->
getDriverName
();
if
(
isset
(
$this
->
schemaMap
[
$driver
]))
{
if
(
isset
(
$this
->
schemaMap
[
$driver
]))
{
return
$this
->
_schema
=
\Yii
::
create
Component
(
$this
->
schemaMap
[
$driver
],
$this
);
return
$this
->
_schema
=
\Yii
::
create
(
$this
->
schemaMap
[
$driver
],
$this
);
}
}
else
{
else
{
throw
new
Exception
(
"Connection does not support reading schema for '
$driver
' database."
);
throw
new
Exception
(
"Connection does not support reading schema for '
$driver
' database."
);
...
...
framework/logging/Router.php
View file @
5e830689
...
@@ -101,7 +101,7 @@ class Router extends \yii\base\ApplicationComponent
...
@@ -101,7 +101,7 @@ class Router extends \yii\base\ApplicationComponent
* Sets the log targets.
* Sets the log targets.
* @param array $config list of log target configurations. Each array element
* @param array $config list of log target configurations. Each array element
* represents the configuration for creating a single log target. It will be
* represents the configuration for creating a single log target. It will be
* passed to [[\Yii::create
Component
]] to create the target instance.
* passed to [[\Yii::create]] to create the target instance.
*/
*/
public
function
setTargets
(
$config
)
public
function
setTargets
(
$config
)
{
{
...
@@ -110,7 +110,7 @@ class Router extends \yii\base\ApplicationComponent
...
@@ -110,7 +110,7 @@ class Router extends \yii\base\ApplicationComponent
$this
->
_targets
[
$name
]
=
$target
;
$this
->
_targets
[
$name
]
=
$target
;
}
}
else
{
else
{
$this
->
_targets
[
$name
]
=
\Yii
::
create
Component
(
$target
);
$this
->
_targets
[
$name
]
=
\Yii
::
create
(
$target
);
}
}
}
}
}
}
...
...
framework/validators/Validator.php
View file @
5e830689
...
@@ -166,7 +166,7 @@ abstract class Validator extends \yii\base\Component
...
@@ -166,7 +166,7 @@ abstract class Validator extends \yii\base\Component
foreach
(
$params
as
$name
=>
$value
)
{
foreach
(
$params
as
$name
=>
$value
)
{
$config
[
$name
]
=
$value
;
$config
[
$name
]
=
$value
;
}
}
$validator
=
\Yii
::
create
Component
(
$config
);
$validator
=
\Yii
::
create
(
$config
);
return
$validator
;
return
$validator
;
}
}
...
...
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