コード例 #1
0
 public function handleList(Args $args, IO $io)
 {
     /** @var AssetMapping[][] $mappingsByTarget */
     $mappingsByTarget = array();
     /** @var InstallTarget[] $targets */
     $targets = array();
     $nonExistingTargets = array();
     // Assemble mappings and validate targets
     foreach ($this->assetManager->getAssetMappings() as $mapping) {
         $targetName = $mapping->getTargetName();
         if (!isset($mappingsByTarget[$targetName])) {
             $mappingsByTarget[$targetName] = array();
             if ($this->targetManager->hasTarget($targetName)) {
                 $targets[$targetName] = $this->targetManager->getTarget($targetName);
             } else {
                 $nonExistingTargets[$targetName] = true;
             }
         }
         $mappingsByTarget[$targetName][] = $mapping;
     }
     if (!$mappingsByTarget) {
         $io->writeLine('No assets are mapped. Use "puli asset map <path> <web-path>" to map assets.');
         return 0;
     }
     if (count($targets) > 0) {
         $io->writeLine('The following web assets are currently enabled:');
         $io->writeLine('');
         foreach ($targets as $targetName => $target) {
             $targetTitle = 'Target <bu>' . $targetName . '</bu>';
             if ($targetName === InstallTarget::DEFAULT_TARGET) {
                 $targetTitle .= ' (alias of: <bu>' . $target->getName() . '</bu>)';
             }
             $io->writeLine("    <b>{$targetTitle}</b>");
             $io->writeLine("    Location:   <c2>{$target->getLocation()}</c2>");
             $io->writeLine("    Installer:  {$target->getInstallerName()}");
             $io->writeLine("    URL Format: <c1>{$target->getUrlFormat()}</c1>");
             $io->writeLine('');
             $this->printMappingTable($io, $mappingsByTarget[$targetName]);
             $io->writeLine('');
         }
         $io->writeLine('Use "puli asset install" to install the assets in their targets.');
     }
     if (count($targets) > 0 && count($nonExistingTargets) > 0) {
         $io->writeLine('');
     }
     if (count($nonExistingTargets) > 0) {
         $io->writeLine('The following web assets are disabled since their target does not exist.');
         $io->writeLine('');
         foreach ($nonExistingTargets as $targetName => $_) {
             $io->writeLine("    <b>Target <bu>{$targetName}</bu></b>");
             $io->writeLine('');
             $this->printMappingTable($io, $mappingsByTarget[$targetName], false);
             $io->writeLine('');
         }
         $io->writeLine('Use "puli target add <target> <location>" to add a target.');
     }
     return 0;
 }
コード例 #2
0
    public function testListMappingsOfNonExistingDefaultTarget()
    {
        $this->targetManager->expects($this->any())->method('hasTarget')->willReturnMap(array(array('local', false), array('remote', false), array(InstallTarget::DEFAULT_TARGET, false)));
        $this->targetManager->expects($this->never())->method('getTarget');
        $this->assetManager->expects($this->once())->method('getAssetMappings')->willReturn(array(new AssetMapping('/app/public', 'local', '/', Uuid::fromString(self::UUID1)), new AssetMapping('/acme/blog/public', 'remote', '/blog', Uuid::fromString(self::UUID2)), new AssetMapping('/acme/profiler/public', 'local', '/profiler', Uuid::fromString(self::UUID3)), new AssetMapping('/acme/admin/public', InstallTarget::DEFAULT_TARGET, '/admin', Uuid::fromString(self::UUID4))));
        $args = self::$listCommand->parseArgs(new StringArgs(''));
        $expected = <<<EOF
The following web assets are disabled since their target does not exist.

    Target local

        e81b32 /app/public           /
        49cfdf /acme/profiler/public /profiler

    Target remote

        33dbec /acme/blog/public /blog

    Target default

        8c64be /acme/admin/public /admin

Use "puli target add <target> <location>" to add a target.

EOF;
        $this->assertSame(0, $this->handler->handleList($args, $this->io));
        $this->assertSame($expected, $this->io->fetchOutput());
        $this->assertEmpty($this->io->fetchErrors());
    }
コード例 #3
0
 public function testGetDefaultTarget()
 {
     $target = new InstallTarget('local', 'symlink', 'public_html');
     $this->targetManager->expects($this->once())->method('getDefaultTarget')->willReturn($target);
     $args = self::$getDefaultCommand->parseArgs(new StringArgs(''));
     $this->assertSame(0, $this->handler->handleGetDefault($args, $this->io));
     $this->assertSame("local\n", $this->io->fetchOutput());
     $this->assertEmpty($this->io->fetchErrors());
 }
コード例 #4
0
 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);
 }
コード例 #5
0
    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));
    }
コード例 #6
0
 public function handleGetDefault(Args $args, IO $io)
 {
     $io->writeLine($this->targetManager->getDefaultTarget()->getName());
     return 0;
 }