 HexMakina    /
                    LeMarchand
                      HexMakina    /
                    LeMarchand
                
                            | 1 | <?php | ||
| 2 | namespace HexMakina\LeMarchand; | ||
| 3 | |||
| 4 | use Psr\Container\ContainerInterface; | ||
| 5 | use Psr\Container\ContainerExceptionInterface; | ||
| 6 | use Psr\Container\NotFoundExceptionInterface; | ||
| 7 | |||
| 8 | class LeMarchand implements ContainerInterface | ||
| 9 | { | ||
| 10 | private static $instance = null; | ||
| 11 | |||
| 12 | private array $configurations; | ||
| 13 | |||
| 14 | /** | ||
| 15 | * Get a container instance | ||
| 16 | * | ||
| 17 | * @param array|null $settings // The container settings | ||
| 18 | * | ||
| 19 | * @return ContainerInterface | ||
| 20 | * | ||
| 21 | * @throws ContainerException | ||
| 22 | */ | ||
| 23 | public static function box($settings = null): ContainerInterface | ||
| 24 |     { | ||
| 25 |         if (is_null(self::$instance)) { | ||
| 26 |             if (is_array($settings)) { | ||
| 27 | return (self::$instance = new LeMarchand($settings)); | ||
| 28 | } | ||
| 29 |             throw new ContainerException('UNABLE_TO_OPEN_BOX'); | ||
| 30 | } | ||
| 31 | |||
| 32 | return self::$instance; | ||
| 33 | } | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Construct a new instance of LeMarchand | ||
| 37 | * | ||
| 38 | * @param array $settings // The settings of LeMarchand | ||
| 39 | */ | ||
| 40 | public function __construct($settings) | ||
| 41 |     { | ||
| 42 | $this->configurations = $settings[__CLASS__] ?? []; | ||
| 43 | |||
| 44 | unset($settings[__CLASS__]); | ||
| 45 | |||
| 46 | $this->configurations['settings'] = $settings; | ||
| 47 | } | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Return information about the instance | ||
| 51 | * | ||
| 52 | * @return array | ||
| 53 | */ | ||
| 54 | public function __debugInfo(): array | ||
| 55 |     { | ||
| 56 | $dbg = get_object_vars($this); | ||
| 57 | |||
| 58 |         foreach ($dbg['configurations']['wiring'] as $interface => $wire) { | ||
| 59 |             if (is_array($wire)) { | ||
| 60 | $wire = array_shift($wire) . ' --array #' . count($wire); | ||
| 61 | } | ||
| 62 | $dbg['configurations']['wiring'] = $wire; | ||
| 63 | } | ||
| 64 | |||
| 65 | return $dbg; | ||
| 66 | } | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Check if an item is set in the container | ||
| 70 | * | ||
| 71 | * @param string $id // The ID of the item | ||
| 72 | * | ||
| 73 | * @return bool | ||
| 74 | */ | ||
| 75 | public function has($id) | ||
| 76 |     { | ||
| 77 |         try { | ||
| 78 | $this->get($id); | ||
| 79 | return true; | ||
| 80 |         } catch (NotFoundExceptionInterface $e) { | ||
| 81 | // return false; | ||
| 82 |         } catch (ContainerExceptionInterface $e) { | ||
| 83 | // return false; | ||
| 84 | } | ||
| 85 | |||
| 86 | return false; | ||
| 87 | } | ||
| 88 | |||
| 89 | /** | ||
| 90 | * Get an item from the container | ||
| 91 | * | ||
| 92 | * @param string $id // The ID of the item | ||
| 93 | * | ||
| 94 | * @return mixed | ||
| 95 | * | ||
| 96 | * @throws NotFoundExceptionInterface If the item is not found | ||
| 97 | * @throws ContainerExceptionInterface If there is a problem with getting the item | ||
| 98 | */ | ||
| 99 | // This method gets an item from the container based on its ID | ||
| 100 | public function get($id) | ||
| 101 |     { | ||
| 102 | // Check if the ID is a string, if not, throw an exception | ||
| 103 |         if (!is_string($id) || empty($id)) { | ||
| 0 ignored issues–
                            show     
    
    
        introduced 
                            by  
  Loading history... | |||
| 104 | throw new ContainerException($id); | ||
| 105 | } | ||
| 106 | |||
| 107 | // Check if the ID is a simple configuration string, if so, return the configuration | ||
| 108 |         if (isset($this->configurations[$id])) { | ||
| 109 | return $this->configurations[$id]; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | $victim = new Solver($this); | ||
| 114 | $res = $victim->solve($id); | ||
| 115 | |||
| 116 | // $prober = new Solver($configuration, $this->configurations['cascade'] ?? []); | ||
| 117 | |||
| 118 | // // Try to get the item from the container by probing the settings, classes, interface wiring, and namespace cascade | ||
| 119 | // $res = $prober->probeSettings($this->configurations) | ||
| 120 | // ?? $prober->probeClasses() | ||
| 121 | // ?? $prober->probeInterface($this->interface_wiring) | ||
| 122 | // ?? $prober->probeCascade(); | ||
| 123 | |||
| 124 | // If the item is not found, throw a NotFoundException | ||
| 125 |         if (is_null($res)) { | ||
| 126 | throw new NotFoundException($id); | ||
| 127 | } | ||
| 128 | |||
| 129 | // Return the item | ||
| 130 | return $res; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | 
