/** * @param Container $Container */ public function __construct(Container $Container) { $this->container = $Container; $this->curl = $Container->singleton('Curl'); $this->db = $Container->singleton('Db'); $this->sql = $Container->singleton('Sql'); }
/** * @inheritdoc */ public function __get($name) { $val = parent::__get($name); if (!is_object($val) and $this->container->isInstantiable($name)) { $val = $this->container->singleton($name); $this->set($name, $val); } return $val; }
/** * @param $name * * @return IConfig|IEntity|IInput|IOutput|IRenderer|IRole|IRouter */ public function __get($name) { $type = $this->getType(); $interface_name = $this->getInterface(); if ($this->container->isInstantiable($name . $type)) { $class = $name . $type; } elseif ($this->container->isInstantiable($name)) { $class = $name; } else { throw new InvalidArgumentException("Param \$name:'{$name}' is not a valid {$type} class."); } $obj = $this->container->singleton($class); if (!$obj instanceof $interface_name) { throw new UnexpectedValueException("Class:'{$class}' must implement {$interface_name}."); } $this->{$name} = $obj; return $obj; }
<?php use Pudding\Library\Container; require __DIR__ . '/../vendor/autoload.php'; $src_dir = isset($_SERVER['argv'][1]) ? rtrim($_SERVER['argv'][1], '/\\') : ''; if (!$src_dir) { $current_file = __FILE__; $pudding_src_dir = realpath(__DIR__ . '/../src'); exit('Error: param src_dir is needed but not initialized.' . PHP_EOL . "Use command: \"php '{$current_file}' 'YourSrcDir'\" to run again." . PHP_EOL . "Example: \"php '{$current_file}' '{$pudding_src_dir}'\"" . PHP_EOL); } if (!is_dir($src_dir)) { exit("Error: {$src_dir} is not a directory."); } $classes = Container::discoverClasses($src_dir); Container::generateCacheFile($classes, $src_dir); $path = Container::getCacheFile($src_dir); if (!is_file($path)) { exit("Error: could not create file '{$path}'." . PHP_EOL); } $cache_classes = (require $path); if (!is_array($cache_classes)) { exit("Error: failed writing data to the file '{$path}'." . PHP_EOL); } exit("Success: generated file '{$path}'." . PHP_EOL);
/** * @param $name * * @return object */ public function __get($name) { return $this->container->singleton($name); }