public function testGetCode()
 {
     $definitionBuilder = new DefinitionBuilder(new ParameterBuilder());
     $definitionBuilder->addDefinition(Car::class)->defineIsSingleton()->defineConstructor()->defineParameter('driver')->defineDependency(new ClassReference(SlowDriver::class))->end()->end()->defineProperty('driver')->defineDependency(new ClassReference(SlowDriver::class))->end()->end()->addDefinition(WheelController::class)->defineConstructor()->defineParameter('fastDriver')->defineDependency(new ClassReference(SlowDriver::class))->end()->defineParameter('slowDriver')->defineDependency(new ClassReference(SlowDriver::class))->end()->defineParameter('car')->defineDependency(new ClassReference(Car::class))->end()->defineParameter('params')->defineDependency((new CollectionReference([CollectionItem::create(new ConstantReference('PHP_VERSION'), 1), 'key0' => 33, 'key1' => new ConstantReference('PHP_MAJOR_VERSION'), 'kye2' => new StringReference('value'), 1 => 5555]))->addItem(CollectionItem::create(2, new ConstantReference('PHP_MINOR_VERSION')))->addItem(CollectionItem::create(3, 'sdf'))->addItem(CollectionItem::create('key3', 'dsddd')))->end()->defineParameter('id')->defineDependency(new ConstantReference('PHP_RELEASE_VERSION'))->end()->end()->defineMethod('setLeg')->defineParameter('leg')->defineDependency(new ClassReference(Leg::class))->end()->end()->defineProperty('car')->defineDependency(new ClassReference(Car::class))->end()->end()->addDefinition(ProductClass::class)->defineConstructor()->end()->end();
     $compiler = new DefinitionCompiler(new DefinitionGenerator(new ClassGenerator()), $this->getAnalyzer());
     $namespace = (new \ReflectionClass(self::class))->getNamespaceName();
     /** @var ContainerInterface $container */
     $container = $compiler->compile($definitionBuilder, 'ContainerGeneratorTest', $namespace, __DIR__ . '/../generated');
     static::assertInstanceOf(WheelController::class, $container->get(WheelController::class));
 }
示例#2
0
 /**
  * Load modules
  *
  * @throws \Exception
  */
 public function init()
 {
     $containerPath = __DIR__ . '/../../../../../www/cache';
     $containerName = 'ContainerCore';
     $containerNamespace = 'samsonphp\\core\\loader';
     /** @var Module $module */
     $modules = $this->moduleManager->getRegisteredModules();
     $localModulesPath = '../src';
     ResourceMap::get('cache');
     $resourceMap = ResourceMap::get($localModulesPath);
     $localModules = $resourceMap->modules;
     if (false || !file_exists($containerPath . '/' . $containerName . '.php')) {
         $builder = new DefinitionBuilder(new ParameterBuilder());
         $xmlResolver = new XmlResolver();
         $xmlResolver->resolveFile($builder, __DIR__ . '/../../../../../app/config/config.xml');
         new Service('');
         new InjectService('');
         new InjectClass('');
         new InjectParameter('');
         foreach ($modules as $module) {
             if ($module->className && !$builder->hasDefinition($module->className)) {
                 // Fix samson.php files
                 if (!class_exists($module->className)) {
                     require_once $module->pathName;
                 }
                 /** @var ClassDefinition $classDefinition */
                 $classDefinition = $builder->addDefinition($module->className);
                 if ($id = $this->getModuleId($module->pathName)) {
                     $classDefinition->setServiceName($id);
                 } else {
                     // Generate identifier from module class
                     $classDefinition->setServiceName(strtolower(ltrim(str_replace(__NS_SEPARATOR__, '_', $module->className), '_')));
                 }
                 $classDefinition->addScope(new ModuleScope())->setIsSingleton(true);
                 $this->defineConstructor($classDefinition, $module->path);
             }
         }
         $classDefinition = $builder->addDefinition(VirtualModule::class);
         $classDefinition->addScope(new ModuleScope())->setServiceName('local')->setIsSingleton(true);
         $this->defineConstructor($classDefinition, getcwd());
         foreach ($localModules as $moduleFile) {
             if (!$builder->hasDefinition($moduleFile[0])) {
                 /** @var ClassDefinition $classDefinition */
                 $classDefinition = $builder->addDefinition($moduleFile[0]);
                 $classDefinition->addScope(new ModuleScope());
                 $classDefinition->setIsSingleton(true);
                 if ($id = $this->getModuleId($moduleFile[1])) {
                     $classDefinition->setServiceName($id);
                 } else {
                     throw new \Exception('Can not get id of local module');
                 }
                 $modulePath = explode('/', str_replace(realpath($localModulesPath), '', $moduleFile[1]));
                 $this->defineConstructor($classDefinition, $localModulesPath . '/' . $modulePath[1]);
             }
         }
         /**
          * Add implementors
          */
         foreach ($this->moduleManager->implements as $interfaceName => $class) {
             $builder->defineImplementors($interfaceName, new ClassReference($class));
         }
         // Init compiler
         $reader = new AnnotationReader();
         $compiler = new DefinitionCompiler(new DefinitionGenerator(new ClassGenerator()), (new DefinitionAnalyzer())->addClassAnalyzer(new AnnotationClassAnalyzer($reader))->addClassAnalyzer(new ReflectionClassAnalyzer())->addMethodAnalyzer(new AnnotationMethodAnalyzer($reader))->addMethodAnalyzer(new ReflectionMethodAnalyzer())->addPropertyAnalyzer(new AnnotationPropertyAnalyzer($reader))->addPropertyAnalyzer(new ReflectionPropertyAnalyzer())->addParameterAnalyzer(new ReflectionParameterAnalyzer()));
         $container = $compiler->compile($builder, $containerName, $containerNamespace, $containerPath);
     } else {
         $containerClassName = $containerNamespace . '\\' . $containerName;
         require_once $containerPath . '/' . $containerName . '.php';
         $container = new $containerClassName();
     }
     $GLOBALS['__core'] = $container->get('core');
     $this->prepareModules($modules, $container);
     /** @var array $module */
     foreach ($localModules as $module) {
         $instance = $container->get($module[0]);
         $instance->parent = $this->getClassParentModule($container, get_parent_class($instance));
     }
     //        $container->get('core')->active($container->get('local'));
     return $container;
 }