Advertisement
Guest User

Untitled

a guest
Aug 24th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. App.php
  2. <?php
  3.  
  4. namespace core;
  5.  
  6. class App {
  7.  
  8.     protected static $container;
  9.  
  10.     public static function setContainer($container){
  11.  
  12.         static::$container = $container;
  13.  
  14.     }
  15.  
  16.     public static function Container(){
  17.  
  18.         return static::$container;
  19.  
  20.     }
  21.  
  22.     public static function resolve($key){
  23.  
  24.        return App::$container->resolve($key);
  25.  
  26.     }
  27.  
  28. }
  29.  
  30. Container.php
  31. <?php
  32.  
  33. namespace core;
  34.  
  35. class Container{
  36.  
  37.     protected $bindings=[];
  38.  
  39.     public function bind($key, $resolver){
  40.  
  41.         $this->bindings[$key] = $resolver;
  42.  
  43.     }
  44.  
  45.     public function resolve($key){
  46.  
  47.         if (!array_key_exists($key, $this->bindings)){
  48.  
  49.             throw new \Exception('No matching binding found for ' . $key);
  50.  
  51.         }
  52.  
  53.         $resolver = $this->bindings[$key];
  54.         return call_user_func($resolver);
  55.  
  56.     }
  57.  
  58. }
  59.  
  60. Bootstrap.php
  61. <?php
  62. use core\App;
  63. use core\Container;
  64. use core\Database;
  65. use core\Session;
  66.  
  67. $container = new Container();
  68.  
  69. $container->bind('core\Database', function (){
  70.  
  71. $config = require base_path("src/config.php");
  72. return new Database($config['database'], $config['database']['user'], $config['database']['password']);
  73.  
  74. });
  75.  
  76. $container->bind('core\Session', function(){
  77.     return new Session;
  78. });
  79.  
  80. App::setContainer($container);
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement