public function testSet_withExistingIdAndReadonly_throwsException() { $this->subject->get(Service1::class); $this->setExpectedException('Neat\\Container\\Exception\\ReadonlyException'); $this->subject->set(Service1::class, Definition::object(Service2::class)); }
<?php use Neat\Router\Router; use Neat\Config\Config; use Neat\Container; use Neat\Container\Definition; use Neat\Event\Dispatcher; use Neat\Http\Request; use Neat\Profiler\Profiler; use Neat\Util\Timer; return [Definition::singleton(Config::class), Definition::singleton(Dispatcher::class), Definition::singleton(Profiler::class, null, '@app:isInDevMode')->addTab('@Neat\\Profiler\\Tab\\Files')->addTab('@Neat\\Profiler\\Tab\\Locations')->addTab('@Neat\\Profiler\\Tab\\Memory')->addTab('@Neat\\Profiler\\Tab\\Time'), Definition::singleton(Request::class), Definition::singleton(Router::class)->properties(true)->addRoute('/module/:module/controller/:controller/action/:action')->addRoute('/controller/:controller/action/:action')->addRoute('/action/:action'), Definition::singleton(TemplateLoader::class, '@app:getAppDir')->setLocation('*', ['{{module}}/View/{{controller}}']), Definition::singleton(Timer::class)];
public function testProperty_withNonExistingProperty_throwsException() { $this->setExpectedException('Neat\\Container\\Exception\\UnexpectedValueException'); Definition::object(Service1::class)->property('property6', 'test'); }
/** * Makes an object. * * @param Definition $definition * * @return mixed */ private function makeObject(Definition $definition) { $class = $definition->getClass(); $constructorInjections = $this->parseReference($definition->getConstructorInjections()); $methodInjections = $this->parseReference($definition->getMethodInjections()); $propertyInjections = $this->parseReference($definition->getPropertyInjections()); $object = $class->newInstanceArgs($constructorInjections); foreach ($methodInjections as $methodName => $injections) { foreach ($injections as $args) { $class->getMethod($methodName)->invokeArgs($object, $args); } } foreach ($propertyInjections as $name => $value) { if ($class->hasProperty($name)) { $property = $class->getProperty($name); $property->setAccessible(true); $property->setValue($object, $value); } else { $object->{$name} = $value; } } return $object; }
<?php use Neat\Config\Config; use Neat\Container; use Neat\Container\Definition; use Neat\Event\Dispatcher; use Neat\Http\Request; use Neat\Loader\FileLoader; use Neat\Profiler\Profiler; use Neat\Router\Router; use Neat\Util\Timer; return [Definition::singleton(Config::class, null, '@Neat\\Parser\\Json')->setPlaceholders(['app_dir' => '@app:getBasedir', 'app_namespace' => '@app:getNamespace']), Definition::singleton(Dispatcher::class), Definition::singleton(Profiler::class, null, '@app:isInDevMode')->setTabs(['@Neat\\Profiler\\Tab\\Files', '@Neat\\Profiler\\Tab\\Locations', '@Neat\\Profiler\\Tab\\Memory', '@Neat\\Profiler\\Tab\\Time']), Definition::singleton(Request::class), Definition::singleton(Router::class)->setRoutes(['/module/:module/controller/:controller/action/:action', '/controller/:controller/action/:action', '/action/:action']), Definition::singleton(FileLoader::class)->setPlaceholders(['app_dir' => '@app:getBasedir', 'module' => '@Neat\\Http\\Request:get:module:Module', 'controller' => '@Neat\\Http\\Request:get:controller:Home'])->setLocations(['config' => ['{{app_dir}}/etc/config'], 'template' => ['{{app_dir}}/src/{{module}}/View/Default', '{{app_dir}}/src/{{module}}/View/{{controller}}']]), Definition::singleton(Timer::class)];