Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
dependency-injector
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Flori
dependency-injector
Commits
53b1de62
There was a problem fetching the latest pipeline status.
Commit
53b1de62
authored
8 years ago
by
Thomas Flori
Browse files
Options
Downloads
Patches
Plain Diff
initial commit
parents
No related branches found
No related tags found
No related merge requests found
Pipeline
#181
skipped
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
README.md
+105
-0
105 additions, 0 deletions
README.md
with
105 additions
and
0 deletions
README.md
0 → 100644
+
105
−
0
View file @
53b1de62
# Dependency Injector
A simple and lightweight dependency injector. You will need nothing more to tests your old legacy code.
## What is a dependency
Something that your script needs to work correct. For example an instance of
`Calculator`
or
`Config`
. Or even
a class itself.
## Examples
### The `Config`
```
php
<?php
class
Config
{
private
static
$_instance
;
public
$database
=
[
'host'
=>
'localhost'
,
'user'
=>
'john'
,
'password'
=>
'does.secret'
,
'database'
=>
'john_doe'
];
public
$redis
=
[
'host'
=>
'localhost'
];
private
function
__construct
()
{
// maybe some logic to change the config or initialize variables
}
public
static
function
getInstance
()
{
if
(
!
self
::
$_instance
)
{
self
::
$_instance
=
new
Config
();
}
return
self
::
$_instance
;
}
}
class_alias
(
'DependencyInjector'
,
'DI'
);
// create an alias for easier access
DI
::
set
(
'config'
,
function
()
{
return
Config
::
getInstance
();
});
function
someStaticFunction
()
{
// before
if
(
empty
(
Config
::
getInstance
()
->
database
[
'host'
]))
{
throw
new
Exception
(
'No database host configured'
);
}
// now
if
(
empty
(
DI
::
config
()
->
database
[
'host'
]))
{
throw
new
Exception
(
'No database host configured'
);
}
// or if you prefer
if
(
empty
(
DI
::
get
(
'config'
)
->
database
[
'host'
]))
{
throw
new
Exception
(
'No database host configured'
);
}
}
```
### The database connection
```
php
<?php
DI
::
set
(
'database'
,
function
()
{
$dbConfig
=
DI
::
config
()
->
database
;
$mysql
=
new
mysqli
(
$dbConfig
[
'host'
],
$dbConfig
[
'user'
],
$dbConfig
[
'password'
],
$dbConfig
[
'database'
]);
if
(
!
empty
(
$mysql
->
connect_error
))
{
throw
new
Exception
(
'could not connect to database ('
.
$mysql
->
connect_error
.
')'
);
}
return
$mysql
;
});
function
someStaticFunction
()
{
// before it maybe looked like this
$mysql
=
MyApp
::
getDatabaseConnection
();
// now
$mysql
=
DI
::
database
();
$mysql
->
query
(
'SELECT * FROM table'
);
}
```
The problem before: you can not mock the call to
`MyApp::getDatabaseConnection()`
. You can still not mock the call to
`DI::database()`
or
`DI::get('database')`
. But you can set the dependency to return a mock object:
```
php
<?php
class
ApplicationTest
extends
PHPUnit_Framework_TestCase
{
public
function
testSomeStaticFunction
()
{
$mock
=
$this
->
getMock
(
mysqli
::
class
);
$mock
->
expects
(
$this
->
once
())
->
method
(
'query'
)
->
with
(
'SELECT * FROM table'
);
DI
::
set
(
'database'
,
function
()
use
(
$mock
)
{
return
$mock
;
});
someStaticFunction
();
}
}
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment