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
135b944c
Commit
135b944c
authored
May 04, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished guide about DI container and service locator.
parent
4e5079ab
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
154 additions
and
50 deletions
+154
-50
concept-di-container.md
docs/guide/concept-di-container.md
+129
-31
concept-service-locator.md
docs/guide/concept-service-locator.md
+16
-19
ContainerTest.php
tests/unit/framework/di/ContainerTest.php
+9
-0
No files found.
docs/guide/concept-di-container.md
View file @
135b944c
This diff is collapsed.
Click to expand it.
docs/guide/concept-service-locator.md
View file @
135b944c
Service Locator
===============
> Note: This chapter needs cleanup.
Both service locator and dependency injection are popular design patterns that allow building software
in a loosely-coupled fashion. Yii uses service locator and dependency injection extensively,
even though you may not be aware of them. In this tutorial, we will explore their implementation
and support to help you write code more consciously. We also highly recommend you to read
[
Martin's article
](
http://martinfowler.com/articles/injection.html
)
to get a deeper understanding of
service locator and dependency injection.
A service locator is an object that knows how to provide all sorts of services (or components) that an application
might need. Within a service locator, each component has only a single instance which is uniquely identified by an ID.
You use the ID to retrieve a component from the service locator. In Yii, a service locator is simply an instance
of
[
[yii\di\ServiceLocator
]
] or its child class.
You use the ID to retrieve a component from the service locator.
In Yii, a service locator is simply an instance of
[
[yii\di\ServiceLocator
]
] or its child class.
The most commonly used service locator in Yii is the
*application*
object which can be accessed through
`\Yii::$app`
. The services it provides are called
*application components*
, such as
`request`
,
`response`
,
`urlManager`
. You may configure these components or
replace them with your own implementations easily
`\Yii::$app`
. The services it provides are called
*application components*
, such as
the
`request`
,
`response`
,
`urlManager`
components. You may configure these components or even
replace them with your own implementations easily
through functionality provided the service locator.
Besides the application object, each module object is also a service locator.
...
...
@@ -26,7 +18,10 @@ To use a service locator, the first step is to register components. A component
via
[
[yii\di\ServiceLocator::set()
]
]. The following code shows different ways of registering components:
```
php
$locator
=
new
\yii\di\ServiceLocator
;
use
yii\di\ServiceLocator
;
use
yii\caching\FileCache
;
$locator
=
new
ServiceLocator
;
// register "cache" using a class name that can be used to create a component
$locator
->
set
(
'cache'
,
'yii\caching\ApcCache'
);
...
...
@@ -43,6 +38,9 @@ $locator->set('db', [
$locator
->
set
(
'search'
,
function
()
{
return
new
app\components\SolrService
;
});
// register "pageCache" using a component
$locator
->
set
(
'pageCache'
,
new
FileCache
);
```
Once a component is registered, you can access it using its ID in one of the following two ways:
...
...
@@ -62,11 +60,10 @@ You may use [[yii\di\ServiceLocator::has()]] to check if a component ID has alre
If you call
[
[yii\di\ServiceLocator::get()
]
] with an invalid ID, an exception will be thrown.
Because service locators are often being configured using configuration arrays, a method named
[
[yii\di\ServiceLocator::setComponents()
]
] is provided to allow registering components in configuration arrays.
The method is a setter which defines a writable property
`components`
that can be configured.
The following code shows a configuration array that can be used to configure an application and register
the "db", "cache" and "search" components:
Because service locators are often being created with
[
configurations
](
concept-configurations.md
)
,
a writable property named
[
[yii\di\ServiceLocator::setComponents()|components
]
] is provided so that
you can configure it and register multiple components at once. The following code shows a configuration array
that can be used to configure an application and register the "db", "cache" and "search" components:
```
php
return
[
...
...
tests/unit/framework/di/ContainerTest.php
View file @
135b944c
...
...
@@ -36,6 +36,8 @@ class ContainerTest extends TestCase
$this
->
assertTrue
(
$foo
instanceof
$Foo
);
$this
->
assertTrue
(
$foo
->
bar
instanceof
$Bar
);
$this
->
assertTrue
(
$foo
->
bar
->
qux
instanceof
$Qux
);
$foo2
=
$container
->
get
(
$Foo
);
$this
->
assertFalse
(
$foo
===
$foo2
);
// full wiring
$container
=
new
Container
;
...
...
@@ -80,5 +82,12 @@ class ContainerTest extends TestCase
$this
->
assertTrue
(
$foo
instanceof
$Foo
);
$this
->
assertTrue
(
$foo
->
bar
instanceof
$Bar
);
$this
->
assertTrue
(
$foo
->
bar
->
qux
instanceof
$Qux
);
// wiring by closure
$container
=
new
Container
;
$container
->
set
(
'qux'
,
new
Qux
);
$qux1
=
$container
->
get
(
'qux'
);
$qux2
=
$container
->
get
(
'qux'
);
$this
->
assertTrue
(
$qux1
===
$qux2
);
}
}
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