Пример #1
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);
 }
 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);
 }
Пример #3
0
    /**
     * {@inheritdoc}
     */
    public function generateFactoryClass($path = null, $className = null)
    {
        Assert::nullOrStringNotEmpty($path, 'The path to the generated factory file must be a non-empty string or null. Got: %s');
        Assert::nullOrStringNotEmpty($className, 'The class name of the generated factory must be a non-empty string or null. Got: %s');
        $path = Path::makeAbsolute($path ?: $this->config->get(Config::FACTORY_OUT_FILE), $this->rootDir);
        $className = $className ?: $this->config->get(Config::FACTORY_OUT_CLASS);
        $dispatcher = $this->context->getEventDispatcher();
        $class = new Clazz($className);
        $class->setFilePath($path);
        $class->setDescription(<<<'EOF'
Creates Puli's core services.

This class was auto-generated by Puli.

IMPORTANT: Before modifying the code below, set the "factory.auto-generate"
configuration key to false:

    $ puli config factory.auto-generate false

Otherwise any modifications will be overwritten!
EOF
);
        $this->addCreateRepositoryMethod($class);
        $this->addCreateDiscoveryMethod($class);
        $this->addCreateUrlGeneratorMethod($class);
        $this->addGetModuleOrderMethod($class);
        if ($dispatcher->hasListeners(PuliEvents::GENERATE_FACTORY)) {
            $dispatcher->dispatch(PuliEvents::GENERATE_FACTORY, new GenerateFactoryEvent($class));
        }
        $this->classWriter->writeClass($class);
    }
Пример #4
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testSetFilePathFailsIfNoString()
 {
     $this->class->setFilePath(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));
    }