/** * Build resolving method injection declaration. * * @param array $dependencies Collection of method dependencies * @param string $methodName Method name * @param string $containerVariable Container declaration variable name * @param string $reflectionVariable Reflection class variable name * @param bool $isPublic Flag if method is public */ protected function buildResolverMethodDeclaration(array $dependencies, string $methodName, string $containerVariable, string $reflectionVariable, bool $isPublic) { // Get method arguments $argumentsCount = count($dependencies); $this->generator->comment('Invoke ' . $methodName . '() and pass dependencies(y)'); if ($isPublic) { $this->generator->newLine($containerVariable . '->' . $methodName . '(')->increaseIndentation(); } else { $this->generator->newLine('$method = ' . $reflectionVariable . '->getMethod(\'' . $methodName . '\');')->newLine('$method->setAccessible(true);')->newLine('$method->invoke(')->increaseIndentation()->newLine($containerVariable . ','); } $i = 0; // Iterate method arguments foreach ($dependencies as $argument => $dependency) { // Add dependencies $this->buildResolverArgument($dependency); // Add comma if this is not last dependency if (++$i < $argumentsCount) { $this->generator->text(','); } } $this->generator->decreaseIndentation()->newLine(');'); }
/** * 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); }