Example #1
0
 /**
  * Writes a {@link Clazz} instance to a file.
  *
  * The directory of the class must have been set.
  *
  * @param Clazz $class The class to write.
  */
 public function writeClass(Clazz $class)
 {
     Assert::notEmpty($class->getDirectory(), 'The directory of the written class must not be empty.');
     ob_start();
     require __DIR__ . '/../../res/template/Class.tpl.php';
     $source = "<?php\n" . ob_get_clean();
     if (!file_exists($class->getDirectory())) {
         mkdir($class->getDirectory(), 0777, true);
     }
     file_put_contents($class->getFilePath(), $source);
 }
Example #2
0
 protected function setUp()
 {
     $this->tempDir = TestUtil::makeTempDir('puli-manager', __CLASS__);
     $this->rootDir = $this->tempDir . '/root';
     $this->outputDir = $this->rootDir . '/out';
     $this->outputPath = $this->outputDir . '/generated.php';
     $this->registry = new DefaultGeneratorRegistry();
     $this->writer = new ClassWriter();
     $this->class = new Clazz('Puli\\Test\\GeneratedFactory');
     $this->class->setFilePath($this->outputPath);
     $this->method = new Method('createService');
     $this->class->addMethod($this->method);
     mkdir($this->rootDir);
     mkdir($this->outputDir);
 }
Example #3
0
    public function testWriteMethodWithDocBlockAndReturnValue()
    {
        $method = new Method('doSomething');
        $method->setDescription("The\n  Doc\n    Block");
        $method->setReturnValue(new ReturnValue('42'));
        $this->class->addMethod($method);
        $this->writer->writeClass($this->class);
        $expected = <<<EOF
<?php

class MyClass
{
    /**
     * The
     *   Doc
     *     Block
     *
     * @return mixed
     */
    public function doSomething()
    {
        return 42;
    }
}

EOF;
        $this->assertFileSame($expected, $this->tempDir . '/MyClass.php');
    }
 protected function setUp()
 {
     while (false === @mkdir($this->tempDir = sys_get_temp_dir() . '/puli-repo-manager/AbstractGeneratorTest' . rand(10000, 99999), 0777, true)) {
     }
     $this->rootDir = $this->tempDir . '/root';
     $this->outputDir = $this->rootDir . '/out';
     $this->outputPath = $this->outputDir . '/generated.php';
     $this->registry = new DefaultGeneratorRegistry();
     $this->writer = new ClassWriter();
     $this->class = new Clazz('Puli\\Test\\GeneratedFactory');
     $this->class->setFilePath($this->outputPath);
     $this->method = new Method('createService');
     $this->class->addMethod($this->method);
     mkdir($this->rootDir);
     mkdir($this->outputDir);
 }
 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);
 }
Example #6
0
 /**
  * Adds the getModuleOrder() method.
  *
  * @param Clazz $class The factory class model.
  */
 public function addGetModuleOrderMethod(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('getModuleOrder');
     $method->setDescription("Returns the order in which the installed modules should be loaded\naccording to the override statements.");
     $method->setReturnValue(new ReturnValue('$order', 'string[]', 'The sorted module names.'));
     $moduleOrderString = '';
     if (count($this->modules) > 0) {
         $overrideGraph = DependencyGraph::forModules($this->modules);
         foreach ($overrideGraph->getSortedModuleNames() as $moduleName) {
             $moduleOrderString .= sprintf("\n    %s,", var_export($moduleName, true));
         }
         $moduleOrderString .= "\n";
     }
     $method->addBody("\$order = array({$moduleOrderString});");
     $class->addMethod($method);
 }
    /**
     * Adds the createUrlGenerator() method.
     *
     * @param Clazz $class The factory class model.
     */
    public function addCreateUrlGeneratorMethod(Clazz $class)
    {
        $class->addImport(new Import('Puli\\Discovery\\Api\\ResourceDiscovery'));
        $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('ResourceDiscovery');
        $arg->setType('ResourceDiscovery');
        $arg->setDescription('The resource 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);
    }
Example #8
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testSetDescriptionFailsIfNoString()
 {
     $this->class->setDescription(1234);
 }
    public function testAddCreateUrlGeneratorMethodWithoutTargets()
    {
        $targets = new InstallTargetCollection(array());
        $this->targetManager->expects($this->any())->method('getTargets')->willReturn($targets);
        $class = new Clazz('Puli\\MyFactory');
        $class->addImport(new Import('Puli\\Factory\\PuliFactory'));
        $class->addImplementedInterface('PuliFactory');
        $class->setFilePath($this->tempFile);
        $this->generator->addCreateUrlGeneratorMethod($class);
        $writer = new ClassWriter();
        $writer->writeClass($class);
        $expected = <<<EOF
<?php

namespace Puli;

use Puli\\AssetPlugin\\Api\\Factory\\UrlGeneratorFactory;
use Puli\\AssetPlugin\\Api\\Target\\InstallTargetCollection;
use Puli\\AssetPlugin\\Api\\UrlGenerator\\AssetUrlGenerator;
use Puli\\AssetPlugin\\UrlGenerator\\DiscoveryUrlGenerator;
use Puli\\Discovery\\Api\\ResourceDiscovery;
use Puli\\Factory\\PuliFactory;

class MyFactory implements PuliFactory, UrlGeneratorFactory
{
    /**
     * Creates the URL generator.
     *
     * @param ResourceDiscovery \$discovery The resource discovery to read from.
     *
     * @return AssetUrlGenerator The created URL generator.
     */
    public function createUrlGenerator(ResourceDiscovery \$discovery)
    {
        \$targets = new InstallTargetCollection();
        \$generator = new DiscoveryUrlGenerator(\$discovery, \$targets);

        return \$generator;
    }
}

EOF;
        $this->assertSame($expected, file_get_contents($this->tempFile));
    }