Example #1
0
 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);
 }
Example #2
0
 public function __construct($options = [])
 {
     if (is_string($options)) {
         $this->options['email'] = $options;
         unset($options);
     }
     if (!empty($options)) {
         $this->options = array_merge($this->options, $options);
     }
     if (empty($this->options['lang'])) {
         $this->options['lang'] = self::DefaultLang;
     }
     // Apply configuration
     EmbeDi::fly()->apply($this->options, $this);
     // Setup view
     $this->mv = new MiniView($this);
     //		var_dump($_POST);
     if (!empty($_POST['ContactForm'])) {
         $data = (object) $_POST['ContactForm'];
         if (!isset($data->name)) {
             $data->name = $data->email;
         }
         if (!isset($data->subject)) {
             $data->subject = substr($data->body, 0, 32);
         }
         if ($this->validate($data)) {
             $email = $this->mv->render(sprintf('%s/%s', $this->lang, 'contact-email'), ['data' => $data], true);
             $headers = "MIME-Version: 1.0" . "\r\n";
             $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
             $headers .= "From: <{$this->email}>" . "\r\n";
             $headers .= "Reply-to: {$data->name} <{$data->email}>" . "\r\n";
             $this->success = mail($this->email, $data->subject, $email, $headers);
         }
     }
 }
Example #3
0
 public function testIfWIllApplyConfigurationFromArrayUsingFlyweightInstances()
 {
     $config = ['scalar' => 'one', 'foo' => ['blah' => 'new', 'class' => 'EmbeDiTest\\Models\\FooSubComponent'], 'bar' => ['name' => 'Tequila', 'class' => 'EmbeDiTest\\Models\\BarSubComponent'], 'class' => 'EmbeDiTest\\Models\\CompoundComponent'];
     foreach ([1, 2, 3] as $id) {
         $config['scalar'] = $id;
         $comp = EmbeDi::fly()->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);
     }
     // Configure second instance
     // This instance have different `classField`
     new EmbeDi('second', null, ['classField' => '_cls']);
     $config = ['scalar' => 'one', 'foo' => ['blah' => 'new', '_cls' => 'EmbeDiTest\\Models\\FooSubComponent'], 'bar' => ['name' => 'Tequila', '_cls' => 'EmbeDiTest\\Models\\BarSubComponent'], '_cls' => 'EmbeDiTest\\Models\\CompoundComponent'];
     foreach ([1, 2, 3] as $id) {
         $config['scalar'] = $id;
         $comp = EmbeDi::fly('second')->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);
     }
 }
Example #4
0
 public function __construct($options = [])
 {
     if (!empty($options)) {
         $this->options = array_merge($this->options, $options);
     }
     // Apply configuration
     EmbeDi::fly()->apply($this->options, $this);
     // Setup view
     $this->mv = new MiniView($this);
 }
Example #5
0
 public function testIfWillExportProperly()
 {
     $comp = new CompoundComponent();
     $comp->scalar = 'one';
     $foo = new FooSubComponent();
     $foo->blah = 'new';
     $bar = new BarSubComponent();
     $bar->name = 'Tequila';
     $comp->foo = $foo;
     $comp->bar = $bar;
     $di = new EmbeDi();
     $exported = $di->export($comp);
     $this->assertSame(CompoundComponent::class, $exported[$di->classField]);
     $this->assertSame($comp->scalar, $exported['scalar']);
     $this->assertSame(FooSubComponent::class, $exported['foo'][$di->classField]);
     $this->assertSame($foo->blah, $exported['foo']['blah']);
     $this->assertSame(BarSubComponent::class, $exported['bar'][$di->classField]);
     $this->assertSame($bar->name, $exported['bar']['name']);
 }
Example #6
0
 public function get($configName)
 {
     foreach ($this->storage->sources as $configs) {
         foreach ($configs as $name => $config) {
             if ($name === $configName) {
                 return EmbeDi::fly($this->_instanceId, $this->_presetId)->apply($config);
             }
         }
     }
 }
Example #7
0
 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();
     }
 }
Example #8
0
 public function __construct($options = [])
 {
     if (!empty($options)) {
         $this->options = array_merge($this->options, $options);
     }
     // Apply configuration
     EmbeDi::fly()->apply($this->options, $this);
     // Setup view
     $this->mv = new MiniView($this);
     // Set auto id if id is default
     if ($this->id === self::DefaultId && self::$idCounter > 0) {
         $this->id = sprintf('%s-%d', $this->id, self::$idCounter++);
     }
 }
Example #9
0
 public function testIfWillConfigureFromArrayWithPresetWithFlyInComponent()
 {
     $config = ['preseted' => ['one' => ['class' => PresetedComponentFly::class, 'scalar' => 'fxx', 'bar' => ['class' => BarSubComponent::class, 'name' => 'Razmatazz']], 'two' => ['class' => PresetedComponentFly::class, 'scalar' => 'ccc', 'bar' => ['class' => BarSubComponent::class, 'name' => 'Cheyenne']]]];
     // Helper arrays
     $cfg1 = $config['preseted']['one'];
     $cfg2 = $config['preseted']['two'];
     EmbeDi::fly()->addAdapter(new ArrayAdapter($config));
     $comp1 = new PresetedComponentFly('preseted', 'one');
     $this->assertSame($cfg1['scalar'], $comp1->scalar);
     $this->assertInstanceOf(BarSubComponent::class, $comp1->bar);
     $this->assertSame($cfg1['bar']['name'], $comp1->bar->name);
     $comp2 = new PresetedComponentFly('preseted', 'two');
     $this->assertSame($cfg2['scalar'], $comp2->scalar);
     $this->assertInstanceOf(BarSubComponent::class, $comp2->bar);
     $this->assertSame($cfg2['bar']['name'], $comp2->bar->name);
 }
Example #10
0
 /**
  * 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;
 }
Example #11
0
 public function __construct($options = [])
 {
     if (is_string($options)) {
         $this->options['path'] = $options;
         unset($options);
     }
     if (!empty($options)) {
         $this->options = array_merge($this->options, $options);
     }
     if (empty($this->options['path'])) {
         $this->options['path'] = self::DefaultPath;
     }
     // Apply configuration
     EmbeDi::fly()->apply($this->options, $this);
     // Setup view
     $this->view = new MiniView($this);
     $this->path = sprintf('%s/%s', Staple::fly()->getContentPath(), $this->options['path']);
 }
Example #12
0
 /**
  * Set pagination
  * @param boolean|array|PaginationInterface $pagination
  * @return static
  */
 public function setPagination($pagination)
 {
     // Disable pagination completely
     if (false === $pagination) {
         $this->pagination = false;
         return $this;
     }
     // Configure from array
     if (is_array($pagination)) {
         if (empty($pagination['class'])) {
             $pagination['class'] = Pagination::class;
         }
         $this->pagination = EmbeDi::fly()->apply($pagination);
         return $this;
     }
     // Set object instance
     if ($pagination instanceof PaginationInterface) {
         $this->pagination = $pagination;
         return $this;
     }
     throw new UnexpectedValueException(sprintf('Expected `false` or `array` or `%s`, got %s', PaginationInterface::class, is_object($pagination) ? get_class($pagination) : gettype($pagination)));
 }
Example #13
0
 /**
  * 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;
 }
Example #14
0
 /**
  * Add annotations namespace.
  * Every added namespace will be included in annotation name resolving for current instance.
  *
  * @param string $ns
  * @renturn Addendum
  */
 public function addNamespace($ns)
 {
     NameNormalizer::normalize($ns, false);
     if (!in_array($ns, $this->namespaces)) {
         $before = count($this->namespaces);
         $this->namespaces[] = $ns;
         $this->namespaces = array_unique($this->namespaces);
         $after = count($this->namespaces);
         if ($after !== $before) {
             $this->nameKeys = array_flip($this->namespaces);
             Cache\NsCache::$addeNs = true;
         }
         $this->di->store($this, [], true);
         // Reconfigure flyweight instances if present
         if (!empty(self::$addendums[$this->instanceId])) {
             self::$addendums[$this->instanceId]->di->configure(self::$addendums[$this->instanceId]);
         }
     }
     return $this;
 }
Example #15
0
 /**
  * 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));
 }
Example #16
0
 public function init()
 {
     $this->_di->store($this);
 }
Example #17
0
 public function __construct($instanceId = EmbeDi::DefaultInstanceId, $presetId = null, $config = [])
 {
     parent::__construct($instanceId, $presetId, $config);
     $this->setAdapters([new YiiAdapter()]);
 }