Recommend this page to a friend! |
Download .zip |
Info | Documentation | View files (64) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2022-05-03 (1 month ago) | Not yet rated by the users | Total: 13 This week: 13 | All time: 10,626 This week: 13 |
Version | License | PHP version | Categories | |||
laravel-service-cont 1.0 | The PHP License | 5 | PHP 5, Libraries, Language, Design Pa... |
Description | Author | |
This package demonstrates how to resolve class dependencies using an Inversion of Control as a Laravel service container. |
>An IoC Container is a convenience Mechanism for achieving Dependency Injection -Taylor Otwell
Laravel is one of the most popular, highly used, open-source modern web application framework. It provides unique features like Eloquent ORM, Query builder ,Homestead which are the modern features, only present in Laravel.
I like Laravel because of its unique architectural design.Behind the scene Laravel uses different design pattern such as Singleton, Factory, Builder, Facade, Strategy, Provider, Proxy etc. So when my knowledge is increasing, I am finding its beauty. Laravel makes developer?s life more easy and removes boringness.
Learning code with Laravel is not just about learning to use the different classes but also learning the philosophy of Laravel, its elegance and its beautiful syntax. An important part of Laravel?s philosophy is the IoC container or Service Container. Understanding and using the IoC container is a crucial part in mastering our craft, as it is the core part of a Laravel application.
Service Container is a powerful tool for managing class dependencies and performing dependency injection. It has the power to automatically resolve classes without configuration. Here I will try to discuss why we need it and How it works.
If at first we know Dependency Inversion Principle it will help us to understand why we need Service Container. So in the beginning I will discuss Dependency Inversion Principle.
The principle states:
>High-level modules should not depend on low-level modules. Both should depend on abstractions.
> Abstractions should not depend on details. Details should depend on abstractions.
Or simply : Depend on Abstractions not on concretions
class MySQLConnection
{
/
* db connection
*/
public function connect()
{
var_dump(?MYSQL Connection?);
}
}
class PasswordReminder
{
/
* @var MySQLConnection
*/
private $dbConnection;
public function __construct(MySQLConnection $dbConnection)
{
$this->dbConnection = $dbConnection;
}
}
There?s a common misunderstanding that dependency inversion is simply another way to say dependency injection. However, the two are not the same.In the above code Inspite of Injecting MySQLConnection class in PasswordReminder class but it is depends on MySQLConnection.
High-level module PasswordReminder should not depend on low-level module MySQLConnection.
If we want to change connection from MySQLConnection to MongoDBConnection, we have to change hard coded constructor injection in PasswordReminder class.
PasswordReminder class should depend upon on Abstractions not on concretions. But How can we do it ? Please see the following example :
interface ConnectionInterface
{
public function connect();
}
class DbConnection implements ConnectionInterface
{
/
* db connection
*/
public function connect()
{
var_dump(?MYSQL Connection?);
}
}
class PasswordReminder
{
/
* @var DBConnection
*/
private $dbConnection;
public function __construct(ConnectionInterface $dbConnection)
{
$this->dbConnection = $dbConnection;
}
}
In the above code we want to change connection from MySQLConnection to MongoDBConnection, we no need to change constructor injection in PasswordReminder class.Because here PasswordReminder class depends upon on Abstractions not on concretions.
If your concept is not clear about interface then you can read this doc . This doc will help you to understand Dependency Inversion Principle, IoC container etc clearly.
Now I will discuss what happens in IoC container. we can simply say that IoC container is a Container that contains Inversion of Control (dependencies of a class).
OrderRepositoryInterface :
namespace App\Repositories;
interface OrderRepositoryInterface
{
public function getAll();
}
DbOrderRepository class:
namespace App\Repositories;
class DbOrderRepository implements OrderRepositoryInterface
{
function getAll()
{
return 'Getting all from mysql';
}
}
OrdersController class:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Repositories\OrderRepositoryInterface;
class OrdersController extends Controller
{
protected $order;
function __construct(OrderRepositoryInterface $order)
{
$this->order = $order;
}
public function index()
{
dd($this->order->getAll());
return View::make(orders.index);
}
}
Routing:
Route::resource('orders', 'OrdersController');
Now if you hit browser using this url http://localhost:8000/orders
Yow will get this error .Because container is trying to instantiate the interface. We can fix that by creating a specific binding for our interface.
BindingResolutionException in Container.php line 748: Target [App\Repositories\OrderRepositoryInterface] is not instantiable while building [App\Http\Controllers\OrdersController].
Just adding this line code in route page we can solve it.
App::bind('App\Repositories\OrderRepositoryInterface', 'App\Repositories\DbOrderRepository');
Now if you hit browser you will get :
"Getting all from mysql"
Here, Need to mention that, We should not resolve app bind in route page. Here I have added only for example purpose.
In our professional project we should have resolve app binding issue in `AppServiceProvider
class
register
` method
as described below:
$this->app->bind('App\Repositories\OrderRepositoryInterface', 'App\Repositories\DbOrderRepository');
we can define a container class in following way:
class SimpleContainer
{
protected static $container = [];
public static function bind($name, Callable $resolver)
{
static::$container[$name] = $resolver;
}
public static function make($name)
{
if(isset(static::$container[$name])){
$resolver = static::$container[$name] ;
return $resolver();
}
throw new Exception("Binding does not exist in containeer");
}
}
Here I will try to show that how simple container resolves dependency
class LogToDatabase
{
public function execute($message)
{
var_dump('log the message to a database :'.$message);
}
}
class UsersController {
protected $logger;
public function __construct(LogToDatabase $logger)
{
$this->logger = $logger;
}
public function show()
{
$user = 'JohnDoe';
$this->logger->execute($user);
}
}
Here bind dependency.
SimpleContainer::bind('Foo', function()
{
return new UsersController(new LogToDatabase);
});
$foo = SimpleContainer::make('Foo');
print_r($foo->show());
Output :
string(36) "Log the messages to a file : JohnDoe"
Laravel?s container code :
public function bind($abstract, $concrete = null, $shared = false)
{
$abstract = $this->normalize($abstract);
$concrete = $this->normalize($concrete);
if (is_array($abstract)) {
list($abstract, $alias) = $this->extractAlias($abstract);
$this->alias($abstract, $alias);
}
$this->dropStaleInstances($abstract);
if (is_null($concrete)) {
$concrete = $abstract;
}
if (! $concrete instanceof Closure) {
$concrete = $this->getClosure($abstract, $concrete);
}
$this->bindings[$abstract] = compact('concrete', 'shared');
if ($this->resolved($abstract)) {
$this->rebound($abstract);
}
}
public function make($abstract, array $parameters = [])
{
$abstract = $this->getAlias($this->normalize($abstract));
if (isset($this->instances[$abstract])) {
return $this->instances[$abstract];
}
$concrete = $this->getConcrete($abstract);
if ($this->isBuildable($concrete, $abstract)) {
$object = $this->build($concrete, $parameters);
} else {
$object = $this->make($concrete, $parameters);
}
foreach ($this->getExtenders($abstract) as $extender) {
$object = $extender($object, $this);
}
if ($this->isShared($abstract)) {
$this->instances[$abstract] = $object;
}
$this->fireResolvingCallbacks($abstract, $object);
$this->resolved[$abstract] = true;
return $object;
}
public function build($concrete, array $parameters = [])
{
if ($concrete instanceof Closure) {
return $concrete($this, $parameters);
}
$reflector = new ReflectionClass($concrete);
if (! $reflector->isInstantiable()) {
if (! empty($this->buildStack)) {
$previous = implode(', ', $this->buildStack);
$message = "Target [$concrete] is not instantiable while building [$previous].";
} else {
$message = "Target [$concrete] is not instantiable.";
}
throw new BindingResolutionException($message);
}
$this->buildStack[] = $concrete;
$constructor = $reflector->getConstructor();
if (is_null($constructor)) {
array_pop($this->buildStack);
return new $concrete;
}
$dependencies = $constructor->getParameters();
$parameters = $this->keyParametersByArgument(
$dependencies, $parameters
);
$instances = $this->getDependencies($dependencies,$parameters);
array_pop($this->buildStack);
return $reflector->newInstanceArgs($instances);
}
If you want to know more details all method about container then you can see
vendor/laravel/framwork/src/Illuminate/Container/Container.php
Simple Bindings
$this->app->bind('HelpSpot\API', function ($app) {
return new HelpSpot\API($app->make('HttpClient'));
});
Binding A Singleton
The singleton method binds a class or interface into the container that should only be resolved one time.
$this->app->singleton('HelpSpot\API', function ($app) {
return new HelpSpot\API($app->make('HttpClient'));
});
Binding Instances
You may also bind an existing object instance into the container using the instance method. The given instance will always be returned on subsequent calls into the container:
$api = new HelpSpot\API(new HttpClient);
$this->app->instance('HelpSpot\API', $api);
If there is no binding, PHP?s Reflection class is used to resolve the instance and dependencies.
You can learn more about it by reading the docs
I have published this article in the medium. if you?d like to read from the medium blog site, please go to this link
Thank you for reading.
Files |
File | Role | Description | ||
---|---|---|---|---|
app (1 file, 7 directories) | ||||
bootstrap (2 files) | ||||
config (12 files) | ||||
database (3 directories) | ||||
public (4 files) | ||||
resources (3 directories) | ||||
tests (2 files) | ||||
.env.example | Data | Auxiliary data | ||
artisan | Class | Class source | ||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
gulpfile.js | Data | Auxiliary data | ||
index.php | Example | Example script | ||
package.json | Data | Auxiliary data | ||
phpunit.xml | Data | Auxiliary data | ||
readme.md | Doc. | Documentation | ||
server.php | Aux. | Auxiliary script |
Files | / | app |
File | Role | Description | ||
---|---|---|---|---|
Console (1 file, 1 directory) | ||||
Events (1 file) | ||||
Exceptions (1 file) | ||||
Http (2 files, 3 directories) | ||||
Jobs (1 file) | ||||
Providers (4 files) | ||||
Repositories (2 files) | ||||
User.php | Class | Class source |
Files | / | app | / | Http |
File | Role | Description | ||
---|---|---|---|---|
Controllers (2 files, 1 directory) | ||||
Middleware (4 files) | ||||
Requests (1 file) | ||||
Kernel.php | Class | Class source | ||
routes.php | Aux. | Auxiliary script |
Files | / | app | / | Http | / | Controllers |
File | Role | Description | ||
---|---|---|---|---|
Auth (2 files) | ||||
Controller.php | Class | Class source | ||
OrdersController.php | Class | Class source |
Files | / | app | / | Http | / | Controllers | / | Auth |
File | Role | Description |
---|---|---|
AuthController.php | Class | Class source |
PasswordController.php | Class | Class source |
Files | / | app | / | Http | / | Middleware |
File | Role | Description |
---|---|---|
Authenticate.php | Class | Class source |
EncryptCookies.php | Class | Class source |
RedirectIfAuthenticated.php | Class | Class source |
VerifyCsrfToken.php | Class | Class source |
Files | / | app | / | Providers |
File | Role | Description |
---|---|---|
AppServiceProvider.php | Class | Class source |
AuthServiceProvider.php | Class | Class source |
EventServiceProvider.php | Class | Class source |
RouteServiceProvider.php | Class | Class source |
Files | / | app | / | Repositories |
File | Role | Description |
---|---|---|
DbOrderRepository.php | Class | Class source |
OrderRepositoryInterface.php | Class | Class source |
Files | / | bootstrap |
File | Role | Description |
---|---|---|
app.php | Class | Class source |
autoload.php | Aux. | Auxiliary script |
Files | / | config |
File | Role | Description |
---|---|---|
app.php | Class | Class source |
auth.php | Class | Class source |
broadcasting.php | Aux. | Auxiliary script |
cache.php | Aux. | Auxiliary script |
compile.php | Aux. | Auxiliary script |
database.php | Aux. | Auxiliary script |
filesystems.php | Aux. | Auxiliary script |
mail.php | Aux. | Auxiliary script |
queue.php | Aux. | Auxiliary script |
services.php | Class | Class source |
session.php | Aux. | Auxiliary script |
view.php | Aux. | Auxiliary script |
Files | / | database | / | migrations |
File | Role | Description |
---|---|---|
2014_10_12_000000_create_users_table.php | Class | Class source |
2014_10_12_100000_...rd_resets_table.php | Class | Class source |
Files | / | public |
File | Role | Description |
---|---|---|
.htaccess | Data | Auxiliary data |
index.php | Class | Class source |
robots.txt | Doc. | Documentation |
web.config | Data | Auxiliary data |
Files | / | resources |
Files | / | resources | / | lang | / | en |
File | Role | Description |
---|---|---|
auth.php | Aux. | Auxiliary script |
pagination.php | Aux. | Auxiliary script |
passwords.php | Aux. | Auxiliary script |
validation.php | Aux. | Auxiliary script |
Files | / | resources | / | views |
File | Role | Description | ||
---|---|---|---|---|
errors (1 file) | ||||
welcome.blade.php | Aux. | Auxiliary script |
Files | / | tests |
File | Role | Description |
---|---|---|
ExampleTest.php | Class | Class source |
TestCase.php | Class | Class source |
laravel-service-cont-2022-05-03.zip 54KB | |
laravel-service-cont-2022-05-03.tar.gz 39KB | |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.