|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\AssetMapper\Command; |
| 13 | + |
| 14 | +use Psr\EventDispatcher\EventDispatcherInterface; |
| 15 | +use Symfony\Component\AssetMapper\AssetMapper; |
| 16 | +use Symfony\Component\AssetMapper\AssetMapperInterface; |
| 17 | +use Symfony\Component\AssetMapper\AssetMapperRepository; |
| 18 | +use Symfony\Component\AssetMapper\Event\AssetMapperCompileEvent; |
| 19 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapManager; |
| 20 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 23 | +use Symfony\Component\Console\Input\InputInterface; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 26 | +use Symfony\Component\Filesystem\Filesystem; |
| 27 | + |
| 28 | +/** |
| 29 | + * Outputs all the assets in the asset mapper. |
| 30 | + * |
| 31 | + * @experimental |
| 32 | + * |
| 33 | + * @author Ryan Weaver <ryan@symfonycasts.com> |
| 34 | + */ |
| 35 | +#[AsCommand(name: 'debug:assetmap', description: 'Outputs all mapped assets.')] |
| 36 | +final class DebugAssetMapperCommand extends Command |
| 37 | +{ |
| 38 | + private bool $didShortenPaths = false; |
| 39 | + |
| 40 | + public function __construct( |
| 41 | + private readonly AssetMapperInterface $assetMapper, |
| 42 | + private readonly AssetMapperRepository $assetMapperRepository, |
| 43 | + private readonly string $projectDir, |
| 44 | + ) { |
| 45 | + parent::__construct(); |
| 46 | + } |
| 47 | + |
| 48 | + protected function configure(): void |
| 49 | + { |
| 50 | + $this |
| 51 | + ->addOption('full', null, null, 'Whether to show the full paths') |
| 52 | + ->setHelp(<<<'EOT' |
| 53 | +The <info>%command.name%</info> command outputs all of the assets in |
| 54 | +asset mapper for debugging purposes! |
| 55 | +EOT |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 60 | + { |
| 61 | + $io = new SymfonyStyle($input, $output); |
| 62 | + |
| 63 | + $allAssets = $this->assetMapper->allAssets(); |
| 64 | + |
| 65 | + $pathRows = []; |
| 66 | + foreach ($this->assetMapperRepository->allDirectories() as $path => $namespace) { |
| 67 | + $path = $this->relativizePath($path); |
| 68 | + if (!$input->getOption('full')) { |
| 69 | + $path = $this->shortenPath($path); |
| 70 | + } |
| 71 | + |
| 72 | + $pathRows[] = [$path, $namespace]; |
| 73 | + } |
| 74 | + $io->section('Asset Mapper Paths'); |
| 75 | + $io->table(['Path', 'Namespace prefix'], $pathRows); |
| 76 | + |
| 77 | + $rows = []; |
| 78 | + foreach ($allAssets as $asset) { |
| 79 | + $logicalPath = $asset->logicalPath; |
| 80 | + $sourcePath = $this->relativizePath($asset->getSourcePath()); |
| 81 | + |
| 82 | + if (!$input->getOption('full')) { |
| 83 | + $logicalPath = $this->shortenPath($logicalPath); |
| 84 | + $sourcePath = $this->shortenPath($sourcePath); |
| 85 | + } |
| 86 | + |
| 87 | + $rows[] = [ |
| 88 | + $logicalPath, |
| 89 | + $sourcePath, |
| 90 | + ]; |
| 91 | + } |
| 92 | + $io->section('Mapped Assets'); |
| 93 | + $io->table(['Logical Path', 'Filesystem Path'], $rows); |
| 94 | + |
| 95 | + if ($this->didShortenPaths) { |
| 96 | + $io->note('To see the full paths, re-run with the --full.'); |
| 97 | + } |
| 98 | + |
| 99 | + return self::SUCCESS; |
| 100 | + } |
| 101 | + |
| 102 | + private function relativizePath(string $path): string |
| 103 | + { |
| 104 | + return str_replace($this->projectDir.'/', '', $path); |
| 105 | + } |
| 106 | + |
| 107 | + private function shortenPath($path): string |
| 108 | + { |
| 109 | + $limit = 50; |
| 110 | + |
| 111 | + if (strlen($path) <= $limit) { |
| 112 | + return $path; |
| 113 | + } |
| 114 | + |
| 115 | + $this->didShortenPaths = true; |
| 116 | + $limit = floor(($limit - 3) / 2); |
| 117 | + |
| 118 | + return substr($path, 0, $limit).'...'.substr($path, -$limit); |
| 119 | + } |
| 120 | +} |
0 commit comments