/** * {@inheritdoc} */ public function build(array $classesMetadata, $containerClass = 'Container', $namespace = '', ContainerInterface $parentContainer = null) { $this->classesMetadata = $this->processClassMetadata($classesMetadata); $this->parentContainer = $parentContainer; // Build dependency injection container function name $this->resolverFunction = uniqid(self::DI_FUNCTION_PREFIX); $containerDependencies = []; foreach ($classesMetadata as $classMetadata) { $className = $classMetadata->className; // Store inner dependencies if (array_key_exists('__construct', $classMetadata->methodsMetadata)) { $containerDependencies[$className] = array_values($classMetadata->methodsMetadata['__construct']->dependencies ?? []); } } $this->generator->text('<?php declare(strict_types = 1);')->newLine()->defNamespace($namespace)->multiComment(['Application container'])->defClass($containerClass, '\\' . Container::class)->multiComment(['@var array Collection of service instances'])->defClassFunction('__construct', 'public', [], ['Container constructor'])->newLine('$this->dependencies = ')->arrayValue($containerDependencies)->text(';')->newLine('$this->aliases = ')->arrayValue($this->classAliases)->text(';')->newLine('$this->scopes = ')->arrayValue($this->scopes)->text(';')->newLine('$this->services = ')->arrayValue($this->scopes[self::SCOPE_SERVICES])->text(';')->endClassFunction()->defClassFunction('logic', 'protected', ['$dependency'], ['{@inheritdoc}'])->newLine('return $this->' . $this->resolverFunction . '($dependency);')->endClassFunction(); foreach ($this->classesMetadata as $classMetadata) { $className = $classMetadata->className; $dependencyName = $classMetadata->name ?? $className; // Generate camel case getter method $camelMethodName = 'get' . str_replace(' ', '', ucwords(ucfirst(str_replace(['\\', '_'], ' ', $dependencyName)))); $this->generator->defClassFunction($camelMethodName, 'public', [], ['@return ' . '\\' . ltrim($className, '\\') . ' Get ' . $dependencyName . ' instance'])->newLine('return $this->' . $this->resolverFunction . '(\'' . $dependencyName . '\');')->endClassFunction(); } // Build di container function and add to container class and return class code $this->buildDependencyResolver($this->resolverFunction); return $this->generator->endClass()->flush(); }
/** * Create View class ancestor. * * @param Metadata $metadata View file metadata * @param string $path Entry path for generated classes and folders * @param null|callable $viewHandler View code handler */ protected function generateViewClass(Metadata $metadata, $path, $viewHandler = null) { $metadataParentClass = eval('return ' . $metadata->parentClass . ';'); // Read view file $viewCode = trim(file_get_contents($metadata->path)); // If we have external handler - pass view code to it for conversion if (is_callable($viewHandler)) { $viewCode = call_user_func($viewHandler, $viewCode); } // Convert to string for defining $viewCode = '<<<\'EOT\'' . "\n" . $viewCode . "\n" . 'EOT'; $parentClass = !isset($metadata->parentClass) ? $this->parentViewClass : $metadataParentClass; $this->generator->defNamespace($metadata->namespace)->multiComment(array('Class for view "' . $metadata->path . '" rendering'))->defClass($metadata->className, '\\' . $parentClass)->commentVar('string', 'Path to view file')->defClassVar('$file', 'protected', $metadata->path)->commentVar('string', 'Parent block name')->defClassVar('$parentBlock', 'protected', $metadata->parentBlock)->commentVar('array', 'Blocks list')->defClassVar('$blocks', 'protected', $metadata->blocks)->commentVar('string', 'View source code')->defClassVar('$source', 'protected', $viewCode); //->commentVar('array', 'Collection of view variables') //->defClassVar('$variables', 'public static', array_keys($metadata->variables)) //->commentVar('array', 'Collection of view variable types') //->defClassVar('$types', 'public static', $metadata->types) // Iterate all view variables foreach (array_keys($metadata->variables) as $name) { $type = array_key_exists($name, $metadata->types) ? $metadata->types[$name] : 'mixed'; $static = array_key_exists($name, $metadata->static) ? ' static' : ''; $this->generator->commentVar($type, 'View variable')->defClassVar('$' . $name, 'public' . $static); // Do not generate setters for static variables if ($static !== ' static') { $this->generator->text($this->generateViewVariableSetter($name, $metadata->originalVariables[$name], $type)); } } // Iterate namespace and create folder structure $path .= '/' . str_replace('\\', '/', $metadata->namespace); if (!is_dir($path)) { mkdir($path, 0777, true); } $newClassFile = $path . '/' . $metadata->className . '.php'; file_put_contents($newClassFile, '<?php' . $this->generator->endClass()->flush()); // Store path to generated class $metadata->generatedPath = $newClassFile; // Make generated cache files accessible chmod($newClassFile, 0777); }