Skip to content

Commit 4aba9c2

Browse files
committed
First commit
0 parents  commit 4aba9c2

File tree

16 files changed

+422
-0
lines changed

16 files changed

+422
-0
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# .gitattributes
2+
tests/ export-ignore
3+
.travis.yml export-ignore
4+
5+
# Auto detect text files and perform LF normalization
6+
* text=auto

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
.idea/*
3+
vendor/*
4+
composer.phar
5+
composer.lock

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
7+
before_script:
8+
- wget http://getcomposer.org/composer.phar
9+
- php composer.phar require satooshi/php-coveralls:dev-master --dev --no-progress --prefer-source
10+
11+
script:
12+
- mkdir -p build/logs
13+
- phpunit --coverage-clover build/logs/clover.xml
14+
15+
after_script:
16+
- php vendor/bin/coveralls -v

LICENSE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
The MIT License (MIT)
2+
3+
Copyright (C) 2013 My C-Sense
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6+
associated documentation files (the "Software"), to deal in the Software without restriction,
7+
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9+
subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all copies or substantial
12+
portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
PHP-DI is a Container that makes [*Dependency Injection*](http://en.wikipedia.org/wiki/Dependency_injection)
2+
as practical as possible.
3+
4+
PHP-DI also tries to avoid falling into the trap of the "Service Locator" antipattern and help you do *real* dependency injection.
5+
6+
[![Latest Stable Version](https://poser.pugx.org/mnapoli/php-di/v/stable.png)](https://packagist.org/packages/mnapoli/php-di) [![Total Downloads](https://poser.pugx.org/mnapoli/php-di/downloads.png)](https://packagist.org/packages/mnapoli/php-di)
7+
8+
9+
## Features
10+
11+
* Simple to start with
12+
* Supports different configuration alternatives to suit every taste:
13+
* **Reflection**: zero configuration, intelligent guessing
14+
* **Annotations**: modern, practical and simple
15+
* **PHP code**: if you like complete control and auto-completion
16+
* **PHP array**: allows you to store it in a configuration file
17+
* **YAML**: elegant and concise
18+
* **Performances**: supports a large number of Caches
19+
* Lazy injection: lazy-loading of dependencies
20+
* Supports constructor injection, setter injection and property injection
21+
* Easy integration with any framework with [Injection over an existing instance](doc/inject-on-instance.md)
22+
23+
24+
## Usage
25+
26+
Let's go to the [Getting started guide](doc/getting-started.md)!
27+
28+
And there is a [complete documentation](doc/) waiting for you.
29+
30+
31+
## What is dependency injection, and why use PHP-DI
32+
33+
You can first read the [introduction to dependency injection with an example](doc/example.md).
34+
35+
Dependency injection and DI containers are separate notions, and one should use of a container only if it makes things more practical (which is not always the case depending on the container you use).
36+
37+
PHP-DI is about this: make dependency injection more practical.
38+
39+
### How classic PHP code works
40+
41+
Here is how a code **not** using DI will roughly work:
42+
43+
* Application needs Foo (e.g. a controller), so:
44+
* Application creates Foo
45+
* Application calls Foo
46+
* Foo needs Bar (e.g. a service), so:
47+
* Foo creates Bar
48+
* Foo calls Bar
49+
* Bar needs Bim (a service, a repository, …), so:
50+
* Bar creates Bim
51+
* Bar does something
52+
53+
### How Dependency Injection works
54+
55+
Here is how a code using DI will roughly work:
56+
57+
* Application needs Foo, which needs Bar, which needs Bim, so:
58+
* Application creates Bim
59+
* Application creates Bar and gives it Bim
60+
* Application creates Foo and gives it Bar
61+
* Application calls Foo
62+
* Foo calls Bar
63+
* Bar does something
64+
65+
This is the pattern of **Inversion of Control**. The control of the dependencies is **inversed** from one being called to the one calling.
66+
67+
The main advantage: the one at the end of the caller chain is always **you**. So you can control every dependencies: you have a complete control on how your application works. You can replace a dependency by another (one you made for example).
68+
69+
For example that wouldn't be so easy if Library X uses Logger Y and you have to change the code of Library X to make it use your logger Z.
70+
71+
### How code using PHP-DI works
72+
73+
Now how does a code using PHP-DI works:
74+
75+
* Application needs Foo so:
76+
* Application gets Foo from the Container, so:
77+
* Container creates Bim
78+
* Container creates Bar and gives it Bim
79+
* Container creates Foo and gives it Bar
80+
* Application calls Foo
81+
* Foo calls Bar
82+
* Bar does something
83+
84+
In short, PHP-DI takes away all the work of creating and injecting dependencies.

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "myclabs/work",
3+
"type": "library",
4+
"description": "Generic work queue library",
5+
"keywords": ["work", "work queue", "rabbitmq"],
6+
"license": "MIT",
7+
"autoload": {
8+
"psr-0": {
9+
"MyCLabs": "src/",
10+
"UnitTest": "tests/"
11+
}
12+
},
13+
"require": {
14+
"php": ">=5.4.0",
15+
"psr/log": "~1.0",
16+
"videlalvaro/php-amqplib": "~2.0"
17+
}
18+
}

phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false"
11+
bootstrap="./tests/bootstrap.php">
12+
13+
<testsuites>
14+
<testsuite name="Work unit tests">
15+
<directory>./tests/UnitTest/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
</phpunit>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MyCLabs\Work\Dispatcher;
4+
5+
use MyCLabs\Work\Task\Task;
6+
7+
/**
8+
* Simple implementation not using any work queue: tasks are executed right away in the same process.
9+
*
10+
* @author Matthieu Napoli <[email protected]>
11+
*/
12+
class SimpleWorkDispatcher implements WorkDispatcher
13+
{
14+
/**
15+
* Workers indexés par le nom de la tâche qu'ils traitent
16+
* @var Worker[]
17+
*/
18+
private $workers = [];
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function runBackground(Task $task)
24+
{
25+
$worker = $this->getWorker($task);
26+
27+
$worker->execute($task);
28+
}
29+
30+
/**
31+
* Retourne le worker enregistré pour une tâche donnée
32+
* @param Task $task
33+
* @return Worker|null
34+
*/
35+
private function getWorker(Task $task)
36+
{
37+
$taskType = get_class($task);
38+
39+
if (array_key_exists($taskType, $this->workers)) {
40+
return $this->workers[$taskType];
41+
}
42+
43+
return null;
44+
}
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace MyCLabs\Work\Dispatcher;
4+
5+
use MyCLabs\Work\Task\Task;
6+
7+
/**
8+
* Dispatch tasks.
9+
*
10+
* @author Matthieu Napoli <[email protected]>
11+
*/
12+
interface WorkDispatcher
13+
{
14+
/**
15+
* Run a task in background
16+
*
17+
* @param Task $task
18+
* @return void No results
19+
*/
20+
public function runBackground(Task $task);
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace MyCLabs\Work\Task;
4+
5+
/**
6+
* Represents the call of the method of a service.
7+
*
8+
* @author Matthieu Napoli <[email protected]>
9+
*/
10+
class ServiceCall implements Task
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $serviceName;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $methodName;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $parameters;
26+
27+
/**
28+
* @param string $serviceName Name of the service class
29+
* @param string $methodName Name of the method to call
30+
* @param array $parameters Parameters for the method call, must be serializable
31+
*/
32+
public function __construct($serviceName, $methodName, array $parameters = [])
33+
{
34+
$this->serviceName = $serviceName;
35+
$this->methodName = $methodName;
36+
$this->parameters = $parameters;
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function getServiceName()
43+
{
44+
return $this->serviceName;
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getMethodName()
51+
{
52+
return $this->methodName;
53+
}
54+
55+
/**
56+
* @return array
57+
*/
58+
public function getParameters()
59+
{
60+
return $this->parameters;
61+
}
62+
}

0 commit comments

Comments
 (0)