Puli\Manager\Factory\FactoryManagerImpl::addCreateUrlGeneratorMethod PHP Method

addCreateUrlGeneratorMethod() public method

Adds the createUrlGenerator() method.
public addCreateUrlGeneratorMethod ( Puli\Manager\Api\Php\Clazz $class )
$class Puli\Manager\Api\Php\Clazz 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);
    }