public function addCreateUrlGeneratorMethod(Clazz $class) { $class->addImport(new Import('Puli\\Discovery\\Api\\ResourceDiscovery')); $class->addImport(new Import('Puli\\AssetPlugin\\Api\\Factory\\UrlGeneratorFactory')); $class->addImport(new Import('Puli\\AssetPlugin\\Api\\Target\\InstallTargetCollection')); $class->addImport(new Import('Puli\\AssetPlugin\\Api\\UrlGenerator\\AssetUrlGenerator')); $class->addImport(new Import('Puli\\AssetPlugin\\UrlGenerator\\DiscoveryUrlGenerator')); $class->addImplementedInterface('UrlGeneratorFactory'); $method = new Method('createUrlGenerator'); $method->setDescription('Creates the URL generator.'); $arg = new Argument('discovery'); $arg->setTypeHint('ResourceDiscovery'); $arg->setType('ResourceDiscovery'); $arg->setDescription('The resource discovery to read from.'); $method->addArgument($arg); $method->setReturnValue(new ReturnValue('$generator', 'AssetUrlGenerator', 'The created URL generator.')); $targets = $this->targetManager->getTargets(); $targetsString = ''; foreach ($targets as $target) { $parameters = ''; foreach ($target->getParameterValues() as $name => $value) { $parameters .= sprintf("\n %s => %s,", var_export($name, true), var_export($value, true)); } if ($parameters) { $parameters .= "\n "; } $targetsString .= sprintf("\n new InstallTarget(%s, %s, %s, %s, array(%s)),", var_export($target->getName(), true), var_export($target->getInstallerName(), true), var_export($target->getLocation(), true), var_export($target->getUrlFormat(), true), $parameters); } if ($targetsString) { $class->addImport(new Import('Puli\\AssetPlugin\\Api\\Target\\InstallTarget')); $targetsString = "array({$targetsString}\n)"; } $method->addBody("\$targets = new InstallTargetCollection({$targetsString});"); if ($targetsString) { $method->addBody("\$targets->setDefaultTarget('{$targets->getDefaultTarget()->getName()}');"); } $method->addBody("\$generator = new DiscoveryUrlGenerator(\$discovery, \$targets);"); $class->addMethod($method); }
/** * @expectedException \InvalidArgumentException */ public function testSetTypeFailsIfNoString() { $this->argument->setType(1234); }
/** * Adds the createUrlGenerator() method. * * @param Clazz $class The factory class model. */ public function addCreateUrlGeneratorMethod(Clazz $class) { $class->addImport(new Import('Puli\\Discovery\\Api\\Discovery')); $class->addImport(new Import('Puli\\Manager\\Api\\Server\\ServerCollection')); $class->addImport(new Import('Puli\\UrlGenerator\\Api\\UrlGenerator')); $class->addImport(new Import('Puli\\UrlGenerator\\DiscoveryUrlGenerator')); $class->addImport(new Import('RuntimeException')); $method = new Method('createUrlGenerator'); $method->setDescription('Creates the URL generator.'); $arg = new Argument('discovery'); $arg->setTypeHint('Discovery'); $arg->setType('Discovery'); $arg->setDescription('The discovery to read from.'); $method->addArgument($arg); $method->setReturnValue(new ReturnValue('$generator', 'UrlGenerator', 'The created URL generator.')); $method->addBody(<<<EOF if (!interface_exists('Puli\\UrlGenerator\\Api\\UrlGenerator')) { throw new RuntimeException('Please install puli/url-generator to create UrlGenerator instances.'); } EOF ); $urlFormatsString = ''; foreach ($this->servers as $server) { $urlFormatsString .= sprintf("\n %s => %s,", var_export($server->getName(), true), var_export($server->getUrlFormat(), true)); } if ($urlFormatsString) { $urlFormatsString .= "\n"; } $method->addBody("\$generator = new DiscoveryUrlGenerator(\$discovery, array({$urlFormatsString}));"); $class->addMethod($method); }
public function testWriteArgumentWithType() { $arg = new Argument('arg'); $arg->setType('MyType'); $method = new Method('doSomething'); $method->addArgument($arg); $this->class->addMethod($method); $this->writer->writeClass($this->class); $expected = <<<EOF <?php class MyClass { /** * @param MyType \$arg */ public function doSomething(\$arg) { } } EOF; $this->assertFileSame($expected, $this->tempDir . '/MyClass.php'); }