private function createModel() { $class = new ClassType('Orm', $this->namespace); $class->setExtends('\\Nextras\\Orm\\Model\\Model'); foreach ($this->parsedSql as $block) { if (isset($block['CREATE'], $block['TABLE'])) { $table = $this->normalizeSqlName($block['TABLE']['name']); $type = $this->getCamelCase($table) . "Repository"; $property = '$' . $this->getCamelCase($table, true) . 's'; $class->addComment("@property-read {$type} {$property}"); } } $this->createClass($class); }
public function afterCompile(Nette\PhpGenerator\ClassType $class) { $initialize = $class->getMethod('initialize'); $container = $this->getContainerBuilder(); if ($this->debugMode && $this->config['debugger']) { Nette\Bridges\DITracy\ContainerPanel::$compilationTime = $this->time; $initialize->addBody($container->formatPhp('?;', array(new Nette\DI\Statement('@Tracy\\Bar::addPanel', array(new Nette\DI\Statement('Nette\\Bridges\\DITracy\\ContainerPanel')))))); } foreach (array_filter($container->findByTag('run')) as $name => $on) { $initialize->addBody('$this->getService(?);', array($name)); } if (!empty($this->config['accessors'])) { $definitions = $container->getDefinitions(); ksort($definitions); foreach ($definitions as $name => $def) { if (Nette\PhpGenerator\Helpers::isIdentifier($name)) { $type = $def->getImplement() ?: $def->getClass(); $class->addComment("@property {$type} \${$name}"); } } } }
private function addAutoGeneratedWarning(ClassType $class) { if (count($class->getDocuments())) { $class->addComment(""); } $class->addComment("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")->addComment("!!! !!!")->addComment("!!! THIS CLASS HAS BEEN AUTO-GENERATED, DO NOT EDIT !!!")->addComment("!!! !!!")->addComment("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); }
public function __construct($namespace, $className, $document = 'Generated Class') { $this->currentFile = new PhpFile(); $this->currentClass = $this->currentFile->addClass($namespace . '\\' . ucfirst($className)); $this->currentClass->addComment($document); }
public function generateTests($outputFolder) { $container = \Testbench\ContainerFactory::create(FALSE); $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); $presenters = $container->findByType('Nette\\Application\\UI\\Presenter'); foreach ($presenters as $presenter) { $this->renderMethods = $this->handleMethods = $this->componentMethods = []; /** @var \Nette\Application\UI\Presenter $service */ $service = $container->getService($presenter); if ($service instanceof \Testbench\Mocks\PresenterMock) { continue; } if ($service instanceof \KdybyModule\CliPresenter) { //Oh, Kdyby! :-( continue; } $rc = new \ReflectionClass($service); $renderPrefix = $service->formatActionMethod('') . '|' . $service->formatRenderMethod(''); $methods = $rc->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED); foreach ($methods as $method) { $methodName = $method->getName(); if (preg_match("~^({$renderPrefix})[a-z0-9]+~i", $methodName)) { try { $optionalArgs = $this->tryCall($service, $methodName, $service->getParameters(), TRUE); if (preg_match('~.*rss.*~i', $methodName)) { $this->renderMethods[$methodName] = 'rss'; } elseif (preg_match('~.*sitemap.*~i', $methodName)) { $this->renderMethods[$methodName] = 'sitemap'; } else { $requiredArgs = $this->tryCall($service, $methodName, $service->getParameters(), FALSE); $this->renderMethods[$methodName] = ['action', [$optionalArgs, $requiredArgs]]; } } catch (\Nette\Application\AbortException $exc) { $this->renderMethods[$methodName] = ['action', $this->getResponse($service)]; } catch (\Exception $exc) { $this->renderMethods[$methodName] = ['exception', $exc]; } } if (preg_match('~^handle[a-z0-9]+~i', $methodName)) { if ($methodName === 'handleInvalidLink') { //internal method continue; } $this->handleMethods[] = $methodName; } if (preg_match('~^createComponent[a-z0-9]+~i', $methodName)) { $method->setAccessible(TRUE); $form = $method->invoke($service); if ($form instanceof \Nette\Application\UI\Form) { $this->componentMethods[$methodName] = $form; } } } $testClassName = $rc->getShortName() . 'Test'; $testClass = new PhpGenerator\ClassType($testClassName); $testClass->setExtends('\\Tester\\TestCase'); $testClass->addTrait('\\Testbench\\TPresenter'); $testClass->addComment('@testCase'); foreach ($this->renderMethods as $testMethod => $testMethodType) { $generatedMethod = $testClass->addMethod('test' . ucfirst($testMethod)); $destination = $presenterFactory->unformatPresenterClass($rc->getName()) . ':'; $destination .= lcfirst(preg_replace('~^(action|render)([a-z]+)~i', '$2', $testMethod)); $extra = NULL; if (is_array($testMethodType)) { /** @var \Exception|\Nette\Application\IResponse $extra */ $extra = $testMethodType[1]; $testMethodType = $testMethodType[0]; //FIXME: fuj, hnus } switch ($testMethodType) { case 'rss': $generatedMethod->addBody('$this->checkRss(?);', [$destination]); break; case 'sitemap': $generatedMethod->addBody('$this->checkSitemap(?);', [$destination]); break; case 'action': if ($extra instanceof \Nette\Application\Responses\RedirectResponse) { $url = new \Nette\Http\Url($extra->getUrl()); $generatedMethod->addBody('$this->checkRedirect(?, ?);', [$destination, $url->getPath()]); } elseif ($extra instanceof \Nette\Application\Responses\JsonResponse) { $generatedMethod->addBody('$this->checkJson(?);', [$destination]); } else { if ($extra[0]) { $generatedMethod->addBody('//FIXME: parameters may not be correct'); $generatedMethod->addBody("\$this->checkAction(?, ?);\n", [$destination, $extra[0]]); $generatedMethod->addBody('$this->checkAction(?, ?);', [$destination, $extra[1]]); } else { $generatedMethod->addBody('$this->checkAction(?);', [$destination]); } } break; case 'exception': $this->generateExceptionBody($generatedMethod, $destination, $extra); break; } } foreach ($this->handleMethods as $testMethod) { $destination = $presenterFactory->unformatPresenterClass($rc->getName()); $action = lcfirst(preg_replace('~^handle([a-z]+)~i', '$1', $testMethod)); $testClass->addMethod('test' . ucfirst($testMethod))->addBody('$this->checkSignal(?, ?);', [$destination . ':', $action]); } foreach ($this->componentMethods as $testMethod => $form) { $destination = $presenterFactory->unformatPresenterClass($rc->getName()); $action = lcfirst(preg_replace('~^createComponent([a-z]+)~i', '$1', $testMethod)); $controls = ''; /** @var \Nette\Application\UI\Form $form */ foreach ($form->getControls() as $control) { if ($control->getName() === '_token_' || $control instanceof \Nette\Forms\Controls\SubmitButton) { continue; } $value = "'###', //FIXME: replace with value"; if ($control instanceof \Nette\Forms\Controls\Checkbox) { $value = 'FALSE'; } $controls .= "\t'" . $control->getName() . "' => {$value}\n"; } try { $form->onSuccess($form, $form->getValues()); $testClass->addMethod('test' . ucfirst($testMethod))->addBody("\$this->checkForm(?, ?, [\n" . $controls . '], ?);', [$destination . ':', $action, FALSE]); } catch (\Nette\Application\AbortException $exc) { $extra = $this->getResponse($service); $path = $extra ? (new \Nette\Http\Url($extra->getUrl()))->getPath() : '/'; $testClass->addMethod('test' . ucfirst($testMethod))->addBody("\$this->checkForm(?, ?, [\n" . $controls . '], ?);', [$destination . ':', $action, $path]); } catch (\Exception $exc) { //This sucks but we have to move on - failure is not an option } } $namespace = $rc->getNamespaceName(); $namespace = $namespace ? '\\' . $namespace : ''; $generatedTest = "<?php\n\nnamespace Tests{$namespace};\n\nuse Tester\\Assert;\n\n"; $depth = substr_count($namespace, '\\'); $levelsUp = str_repeat('../', $depth); $generatedTest .= "require __DIR__ . '/{$levelsUp}bootstrap.php';\n\n"; $generatedTest .= $testClass; $generatedTest .= "\n(new {$testClassName})->run();\n"; $testFileName = preg_replace('~\\\\~', DIRECTORY_SEPARATOR, get_class($service)); \Nette\Utils\FileSystem::write($outputFolder . '/' . $testFileName . '.phpt', $generatedTest); \Nette\Utils\FileSystem::createDir($outputFolder . '/_temp'); \Nette\Utils\FileSystem::write($outputFolder . '/tests.neon', <<<'NEON' # WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser! # http://nette.org/security-warning # (this is just an example) routing: routes: '/x/y[[[/<presenter>]/<action>][/<id>]]': 'Presenter:default' NEON ); } }
public function onGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Type $type, ClassType $class) { $namespace = $class->getNamespace(); // extend base class $namespace->addUse($type->getName(), null, $typeAlias); $class->addExtend($type->getName()); $class->addComment("Meta class for \\{$type->getName()}")->addComment("")->addComment("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")->addComment("!!! !!!")->addComment("!!! THIS CLASS HAS BEEN AUTO-GENERATED, DO NOT EDIT !!!")->addComment("!!! !!!")->addComment("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); // constructor $constructor = $class->addMethod("__construct"); $constructor->addComment("Constructor")->addBody("self::\$instance = \$this; // avoids cyclic dependency stack overflow"); if ($type->getConstructor()) { if ($type->getConstructor()->isPublic()) { $constructor->setVisibility("public"); } elseif ($type->getConstructor()->isProtected()) { $constructor->setVisibility("protected"); } elseif ($type->getConstructor()->isPrivate()) { $constructor->setVisibility("private"); } } else { $constructor->setVisibility("private"); } // implement base interface $namespace->addUse("Skrz\\Meta\\MetaInterface", null, $metaInterfaceAlias); $class->addImplement("Skrz\\Meta\\MetaInterface"); // getInstance() method $instance = $class->addProperty("instance"); $instance->setStatic(true); $instance->setVisibility("private"); $instance->addComment("@var {$class->getName()}"); $getInstance = $class->addMethod("getInstance"); $getInstance->setStatic(true); $getInstance->addComment("Returns instance of this meta class")->addComment("")->addComment("@return {$class->getName()}"); $getInstance->addBody("if (self::\$instance === null) {")->addBody("\tnew self(); // self::\$instance assigned in __construct")->addBody("}")->addBody("return self::\$instance;"); // create() method $create = $class->addMethod("create"); $create->setStatic(true); $create->addComment("Creates new instance of \\{$type->getName()}")->addComment("")->addComment("@throws \\InvalidArgumentException")->addComment("")->addComment("@return {$typeAlias}"); $create->addBody("switch (func_num_args()) {"); $maxArguments = 8; $constructMethod = $type->getConstructor(); for ($i = 0; $i <= $maxArguments; ++$i) { $create->addBody("\tcase {$i}:"); if ($constructMethod && $i < $constructMethod->getNumberOfRequiredParameters()) { $create->addBody("\t\tthrow new \\InvalidArgumentException('At least {$constructMethod->getNumberOfRequiredParameters()} arguments have to be supplied.');"); } else { $args = array(); for ($j = 0; $j < $i; ++$j) { $args[] = "func_get_arg({$j})"; } $create->addBody("\t\treturn new {$typeAlias}(" . implode(", ", $args) . ");"); } } $create->addBody("\tdefault:"); $create->addBody("\t\tthrow new \\InvalidArgumentException('More than {$maxArguments} arguments supplied, please be reasonable.');"); $create->addBody("}"); // reset() method $reset = $class->addMethod("reset"); $reset->setStatic(true); $reset->addComment("Resets properties of \\{$type->getName()} to default values\n")->addComment("")->addComment("@param {$typeAlias} \$object")->addComment("")->addComment("@throws \\InvalidArgumentException")->addComment("")->addComment("@return void"); $reset->addParameter("object"); $reset->addBody("if (!(\$object instanceof {$typeAlias})) {")->addBody("\tthrow new \\InvalidArgumentException('You have to pass object of class {$type->getName()}.');")->addBody("}"); foreach ($type->getProperties() as $property) { if ($property->hasAnnotation("Skrz\\Meta\\Transient")) { continue; } if ($property->isPrivate()) { throw new MetaException("Private property '{$type->getName()}::\${$property->getName()}'. " . "Either make the property protected/public if you need to process it, " . "or mark it using @Transient annotation."); } $reset->addBody("\$object->{$property->getName()} = " . var_export($property->getDefaultValue(), true) . ";"); } // hash() method $hash = $class->addMethod("hash"); $hash->setStatic(true); $hash->addComment("Computes hash of \\{$type->getName()}")->addComment("")->addComment("@param object \$object")->addComment("@param string|resource \$algoOrCtx")->addComment("@param bool \$raw")->addComment("")->addComment("@return string|void"); $hash->addParameter("object"); $hash->addParameter("algoOrCtx")->setDefaultValue("md5")->setOptional(true); $hash->addParameter("raw")->setDefaultValue(false)->setOptional(true); $hash->addBody("if (is_string(\$algoOrCtx)) {")->addBody("\t\$ctx = hash_init(\$algoOrCtx);")->addBody("} else {")->addBody("\t\$ctx = \$algoOrCtx;")->addBody("}")->addBody(""); foreach ($type->getProperties() as $property) { if ($property->hasAnnotation("Skrz\\Meta\\Transient")) { continue; } if ($property->hasAnnotation("Skrz\\Meta\\Hash")) { continue; } $objectPath = "\$object->{$property->getName()}"; $hash->addBody("if (isset({$objectPath})) {"); $hash->addBody("\thash_update(\$ctx, " . var_export($property->getName(), true) . ");"); $baseType = $property->getType(); $indent = "\t"; $before = ""; $after = ""; for ($i = 0; $baseType instanceof ArrayType; ++$i) { $arrayType = $baseType; $baseType = $arrayType->getBaseType(); $before .= "{$indent}foreach ({$objectPath} instanceof \\Traversable ? {$objectPath} : (array){$objectPath} as \$v{$i}) {\n"; $after = "{$indent}}\n" . $after; $indent .= "\t"; $objectPath = "\$v{$i}"; } if (!empty($before)) { $hash->addBody(rtrim($before)); } if ($baseType instanceof ScalarType) { $hash->addBody("{$indent}hash_update(\$ctx, (string){$objectPath});"); } elseif ($baseType instanceof Type) { $datetimeType = false; for ($t = $baseType; $t; $t = $t->getParentClass()) { if ($t->getName() === "DateTime") { $datetimeType = true; break; } } if ($datetimeType) { $hash->addBody("{$indent}hash_update(\$ctx, {$objectPath} instanceof \\DateTime ? {$objectPath}->format(\\DateTime::ISO8601) : '');"); } else { $propertyTypeMetaClassName = $spec->createMetaClassName($baseType); $namespace->addUse($propertyTypeMetaClassName, null, $propertyTypeMetaClassNameAlias); $hash->addBody("{$indent}{$propertyTypeMetaClassNameAlias}::hash({$objectPath}, \$ctx);"); } } else { throw new MetaException("Unsupported property type " . get_class($baseType) . " ({$type->getName()}::\${$property->getName()})."); } if (!empty($after)) { $hash->addBody(rtrim($after)); } $hash->addBody("}")->addBody(""); } $hash->addBody("if (is_string(\$algoOrCtx)) {")->addBody("\treturn hash_final(\$ctx, \$raw);")->addBody("} else {")->addBody("\treturn null;")->addBody("}"); }
/** * @param string $tableName * @param array $columns * @return void */ protected function generateTableGeneratedHyperRow($tableName, $columns) { $classFqn = $this->config['classes']['row']['generated']; $classFqn = Helpers::substituteClassWildcard($classFqn, $tableName); $className = Helpers::extractClassName($classFqn); $classNamespace = Helpers::extractNamespace($classFqn); $extendsFqn = $this->config['classes']['row']['base']; $extends = Helpers::formatClassName($extendsFqn, $classNamespace); $class = new ClassType($className); $class->setExtends($extends); // Add property annotations based on columns foreach ($columns as $column => $type) { if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) { $type = '\\Nette\\Utils\\DateTime'; } $class->addComment("@property-read {$type} \${$column}"); } // Generate methods.row.getter foreach ((array) $this->config['methods']['row']['getter'] as $methodTemplate) { // Generate column getters foreach ($columns as $column => $type) { if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) { $type = '\\Nette\\Utils\\DateTime'; } $methodName = Helpers::substituteMethodWildcard($methodTemplate, $column); $returnType = $type; $class->addMethod($methodName)->addBody('return $this->activeRow->?;', [$column])->addComment("@return {$returnType}"); // Add property annotation if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); if ($property != $column) { $class->addComment("@property-read {$type} \${$property}"); } } } } // Generate methods.row.ref foreach ((array) $this->config['methods']['row']['ref'] as $methodTemplate) { // Generate 'ref' methods foreach ($this->structure->getBelongsToReference($tableName) as $referencingColumn => $referencedTable) { if (is_array($this->config['tables']) && !in_array($referencedTable, $this->config['tables'])) { continue; } $result = Helpers::underscoreToCamelWithoutPrefix(Strings::replace($referencingColumn, '~_id$~'), $tableName); $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result); $returnType = $this->getTableClass('row', $referencedTable, $classNamespace); $class->addMethod($methodName)->addBody('return $this->ref(?, ?);', [$referencedTable, $referencingColumn])->addComment("@return {$returnType}"); // Add property annotations if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); $class->addComment("@property-read {$returnType} \${$property}"); } } } // Generate methods.row.related foreach ((array) $this->config['methods']['row']['related'] as $methodTemplate) { // Generate 'related' methods foreach ($this->structure->getHasManyReference($tableName) as $relatedTable => $referencingColumns) { if (is_array($this->config['tables']) && !in_array($relatedTable, $this->config['tables'])) { continue; } foreach ($referencingColumns as $referencingColumn) { // Omit longest common prefix between $relatedTable and (this) $tableName $result = Helpers::underscoreToCamelWithoutPrefix($relatedTable, $tableName); if (count($referencingColumns) > 1) { $suffix = 'As' . Helpers::underscoreToCamel(Strings::replace($referencingColumn, '~_id$~')); } else { $suffix = NULL; } $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result, $suffix); $returnType = $this->getTableClass('selection', $relatedTable, $classNamespace); $class->addMethod($methodName)->addBody('return $this->related(?, ?);', [$relatedTable, $referencingColumn])->addComment("@return {$returnType}"); // Add property annotations if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); $class->addComment("@property-read {$returnType} \${$property}"); } } } } $code = implode("\n\n", ['<?php', "/**\n * This is a generated file. DO NOT EDIT. It will be overwritten.\n */", "namespace {$classNamespace};", $class]); $dir = $this->config['dir'] . '/' . 'tables' . '/' . $tableName; $file = $dir . '/' . $className . '.php'; $this->writeIfChanged($file, $code); }