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