1. Go to this page and download the library: Download simpla-components/container library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
simpla-components / container example snippets
namespace App\Classes;
interface CalculatorInterface
{
public function sum($a, $b);
public function subtract($a, $b);
public function multiply($a, $b);
public function divide($a, $b);
}
namespace App\Classes;
class Calculator implements CalculatorInterface
{
public function sum($a, $b)
{
return $a + $b;
}
public function subtract($a, $b)
{
return $a - $b;
}
public function multiply($a, $b)
{
return $a * $b;
}
public function divide($a, $b)
{
return $a / $b;
}
}
use Simpla\Container\Container;
use Xtreamwayz\Pimple\Container as Pimple;
/* @var $app Simpla\Container\Container */
$app = Container::instance(new Pimple);
global $app;
$app->make("calc", function(){
return new \App\Core\Calculator();
});
$calc = new \App\Classes\Calculator();
$app->make("calc", $calc);
// ou diretamente
$app->make("calc", new \App\Classes\Calculator());
$app->tagged("ICalc", App\Classes\CalculatorInterface::class);
$app->make("ICalc", \App\Classes\Calculator::class);
// também podemos definir como array ou singleton
$app["ICalc"] = new \App\Classes\Calculator;
$app->singleton("ICalc", \App\Classes\Calculator::class);
class Car
{
private $placa;
private $ano;
function getPlaca()
{
return $this->placa;
}
function getAno()
{
return $this->ano;
}
function setPlaca($placa)
{
$this->placa = $placa;
}
function setAno($ano)
{
$this->ano = $ano;
}
}
$app['car'] = function(){
return new Car();
};
$app['car'] = $app->extend('car', function($car){
$car->setPlaca("HNT-2299");
$car->setAno(2010);
return $car;
});
var_dump($app['car']);
/*
object(Car)#37 (2) {
["placa":"Car":private]=>
string(8) "HNT-2299"
["ano":"Car":private]=>
int(2010)
}
*/
namespace App\Providers;
use Simpla\Contracts\ServiceProviderInterface;
use Simpla\Contracts\ContainerInterface;
class TodayServiceProvider implements ServiceProviderInterface
{
/**
* Register the services
*
* @return void
*/
public function register(ContainerInterface $serviceContainer)
{
$tz = new \DateTimeZone("America/Sao_Paulo");
$serviceContainer->make("today", function(){
return new DateTime("now");
});
}
}
// CalculatorServiceProvider.php
namespace App\Providers;
use Simpla\Contracts\ServiceProviderInterface;
use Simpla\Contracts\ContainerInterface;
class CalculatorServiceProvider implements ServiceProviderInterface
{
/**
* Register the services
*
* @return void
*/
public function register(ContainerInterface $serviceContainer)
{
$serviceContainer->make("calc", function(){
return new \App\Classes\Calculator();
});
}
}
// CalculatorServiceProvider.php
namespace App\Providers;
use Simpla\Contracts\ServiceProviderInterface;
use Simpla\Contracts\ContainerInterface;
class TestServiceProvider implements ServiceProviderInterface
{
/**
* Bootstrap the services
*
* @return void
*/
public function boot(ContainerInterface $app)
{
echo "Isso é uma mensagem de inicialização";
//Imprimindo serviços disponíveis
print_r($app->get());
}
/**
* Register the services
*
* @return void
*/
public function register(ContainerInterface $serviceContainer)
{
// code
}
}
use Simpla\Container\Container;
use Xtreamwayz\Pimple\Container as Pimple;
global $app;
/* @var $app Simpla\Container\Container */
$app = Container::instance(new Pimple);
$app->createAlias($aliases);
$app->registerProviders($providers);
class CalculatorServiceProvider implements ServiceProviderInterface
{
protected $defer = true;
public function register(ContainerInterface $serviceContainer)
{
$calc = new \App\Classes\Calculator();
$serviceContainer->make("calc", $calc);
$serviceContainer->make("calcSum",
$serviceContainer->closure(function ($a, $b) {
return $a + $b;
})
);
}
public static function providers()
{
return ['calc','callSum'];
}
}