Bu videoyu izledim ve şöyle
App.php
<?php
namespace Core;
class App
{
protected static $container;
public static function getContainer()
{
return static::$container;
}
public static function setContainer($container)
{
static::$container = $container;
}
public static function bind($key,$resolver)
{
static::getContainer()->bind($key, $resolver);
}
public static function resolve($key)
{
return static::getContainer()->resolve($key);
}
}
Container.php
<?php
namespace Core;
class Container
{
protected $bindings = [];
public function bind($key,$resolver)
{
$this->bindings[$key] = $resolver;
}
public function resolve($key)
{
if(! array_key_exists($key,$this->bindings)) {
throw new \Exception('No matching bindings found for your {$key}');
}
$resolver = $this->bindings[$key];
return call_user_func($resolver);
}
}
bootstrap.php
<?php
use Core\App;
use Core\Container;
use Core\Database;
$container = new Container();
$container->bind('Core\Database', function() {
$config = require base_path('config.php');
return new Database($config['database']);
});
App::setContainer($container);
ve sonrasında diyelim store.php
veya destroy.php
de şu şekilde
$db = App::getContainer()->resolve(Database::class);
çağırıyor. Fakat ben tam anlayamadım mantığını.