# ServiceContainer

class ServiceContainer

## Methods

`public static` | `` | `create()` | No description |
`protected` | `` | `__contruct()` | No description |
`public static` | `` | `resolve($str)` | No description |
`public static` | `` | `bind($serviceName, $service)` | No description |
`public static` | `` | `get($serviceName)` | No description |
`public static` | `` | `all()` | No description |
`public static` | `` | `has($serviceName)` | No description |
`public` | `` | `__set($serviceName, $service)` | No description |
`public` | `` | `__get($serviceName)` | No description |
`public` | `` | `__invoke()` | No description |

## Details

at line 13

### `create()`

`public static create()`
at line 21

### `__contruct()`

`protected __contruct()`
at line 25

### `resolve()`

`public static resolve($str)`

#### Parameters

| `$str` |

at line 40

### `bind()`

`public static bind($serviceName, $service)`

#### Parameters

| `$serviceName` |
| `$service` |

at line 49

### `get()`

`public static get($serviceName)`

#### Parameters

| `$serviceName` |

at line 58

### `all()`

`public static all()`
at line 62

### `has()`

`public static has($serviceName)`

#### Parameters

| `$serviceName` |

at line 67

### `__set()`

`public __set($serviceName, $service)`

#### Parameters

| `$serviceName` |
| `$service` |

at line 76

### `__get()`

`public __get($serviceName)`

#### Parameters

| `$serviceName` |

at line 85

### `__invoke()`

`public __invoke()`

## Source code

    <?php

        namespace App\Khan\Component\Container;

        use stdClass;
        use Closure;

        class ServiceContainer {

            private static $containers = [];
            private static $instance = null;

            public static function create()
            {
                if (!self::$instance) {
                    self::$instance = new ServiceContainer();
                }
                return self::$instance;
            }

            protected function __contruct()
            {
            }

            public static function resolve($str){
                $r = [];
                foreach (self::$containers as $key => $value) {
                    if(preg_match("/".$str."\./", $key) && !strpos($key, '.use')){
                        $r[str_replace("{$str}.", "", $key)] = $value;
                    }
                }
                return function ($use = null) use($r){
                    if(is_null($use)){
                        return $r;
                    }
                    return $r[$use];
                };
            }

            public static function bind($serviceName, $service)
            {
                if (!ServiceContainer::has($serviceName) && strlen($serviceName) > 0 && $service) {
                    self::$containers[$serviceName] = $service;
                } else {
                    die("Já existe um servico {$serviceName} no Container!!");
                }
            }

            public static function get($serviceName)
            {
                if (ServiceContainer::has($serviceName)) {
                    return self::$containers[$serviceName];
                } else {
                    die("Não existe servico com esse nome {$serviceName} no Container!!");
                }
            }

            public static function all(){
                return self::$containers;
            }

            public static function has($serviceName)
            {
                return isset(self::$containers[$serviceName]);
            }

            public function __set($serviceName, $service)
            {
                if (!ServiceContainer::has($serviceName) && strlen($serviceName) > 0 && $service) {
                    self::$containers[$serviceName] = $service;
                } else {
                    die("Já existe um servico {$serviceName} no Container!!");
                }
            }

            public function __get($serviceName)
            {
                if (ServiceContainer::has($serviceName)) {
                    return self::$containers[$serviceName];
                } else {
                    die("Não existe servico com esse nome {$serviceName} no Container!!");
                }
            }

            public function __invoke()
            {
                $class_cache = new stdClass;
                foreach (self::$containers as $key => $value) {
                    $class_cache->$key = $value;
                }
                print_r($class_cache);
                return $class_cache;
            }

        }