Example #1
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);
    }
Example #2
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');
    }
 public function testRefreshFactoryClassDoesNotGenerateIfAutoGenerateDisabled()
 {
     $manager = new FactoryManagerImpl($this->context, $this->registry, $this->fakeWriter, $this->servers);
     // Older than config file -> would normally be generated
     touch($this->rootDir . '/MyFactory.php');
     sleep(1);
     touch($this->rootPackageFile->getPath());
     $this->fakeWriter->expects($this->never())->method('writeClass');
     $this->context->getConfig()->set(Config::FACTORY_AUTO_GENERATE, false);
     $manager->refreshFactoryClass();
 }
    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));
    }