public function testIfWIllApplyConfigurationFromArray() { $config = ['scalar' => 'one', 'foo' => ['blah' => 'new', 'class' => 'EmbeDiTest\\Models\\FooSubComponent'], 'bar' => ['name' => 'Tequila', 'class' => 'EmbeDiTest\\Models\\BarSubComponent'], 'class' => 'EmbeDiTest\\Models\\CompoundComponent']; $di = new EmbeDi(); $comp = $di->apply($config); /* @var $comp CompoundComponent */ $this->assertInstanceOf(CompoundComponent::class, $comp); $this->assertSame($config['scalar'], $comp->scalar); $this->assertInstanceOf(FooSubComponent::class, $comp->foo); $this->assertSame($config['foo']['blah'], $comp->foo->blah); $this->assertInstanceOf(BarSubComponent::class, $comp->bar); $this->assertSame($config['bar']['name'], $comp->bar->name); }
/** * Instantiate object based on configuration * @param string|mixed[] $config * @return object */ private function _instantiate($config, $fly = false) { $key = $this->getKey($config); $className = $this->_getClassName($config); if ($fly) { if (isset($this->plugins[$key])) { $plugin = $this->plugins[$key]; } else { $plugin = $this->plugins[$key] = new $className(); } } else { $plugin = new $className(); } if (is_array($config)) { $plugin = $this->di->apply($config, $plugin); } return $plugin; }
public function generate() { $di = new EmbeDi(); $sprites = (new ImageFinder())->find($this->packages); $collection = new Collection($sprites); foreach ($this->generators as $generatorConfig) { $generator = $di->apply($generatorConfig); /* @var $generator GeneratorInterface */ if ($generator instanceof CollectionAwareInterface) { $generator->setCollection($collection); } if ($generator instanceof ConfigurationAwareInterface) { $generator->setConfig($this); } if ($generator instanceof LoggerAwareInterface) { $generator->setLogger($this->getLogger()); } $generator->generate(); } }
/** * Get configured property * @param string $property * @return SignalAwareInterface */ private function getConfigured($property) { if (is_object($this->{$property})) { $object = $this->{$property}; } else { $object = $this->di->apply($this->{$property}); } if ($object instanceof SignalAwareInterface) { $object->setSignal($this); } return $object; }
/** * Get renderer * @param string $fileName * @return RendererInterface */ public function getRenderer($fileName) { if (empty($fileName)) { return (new ErrorRenderer('404', 'File not found'))->setOwner($this); } $renderers = $this->renderers; $keys = array_map('strlen', array_keys($renderers)); array_multisort($keys, SORT_DESC, $renderers); foreach ($renderers as $extension => $config) { $matches = []; $ext = preg_quote($extension); if (preg_match("~{$ext}\$~i", $fileName, $matches)) { $renderer = $this->di->apply($config); /* @var $renderer RendererInterface */ if ($renderer instanceof RendererExtensionInterface) { // Use $matches[0] here to ensure extension letters case $renderer->setExtension($matches[0]); } $renderer->setOwner($this); return $renderer; } } new ErrorRenderer(500, sprintf('Unsupported file extension: `%s`', $ext)); }
/** * Create new mangan instance. * * **NOTE: While it's ok to use constructor to create Mangan, it is recommended to use * Mangan::fly() to create/get instance, as creating new instance has some overhead.** * * @param string $connectionId */ public function __construct($connectionId = self::DefaultConnectionId) { $this->di = EmbeDi::fly($connectionId); // Load built-in config $config = ConfigManager::getDefault(); // Gather additional config options via signals (new Signal())->emit(new ConfigInit($config, $connectionId)); // Apply built-in configuration, as other configurations might not exists $this->di->apply($config, $this); if (empty($connectionId)) { $connectionId = self::DefaultConnectionId; } $this->connectionId = $connectionId; // Apply any configurations loaded $this->di->configure($this); $this->cs = new ConnectionStorage($this, $connectionId); if (empty(self::$mn[$connectionId])) { self::$mn[$connectionId] = $this; } }