8000 [AssetMapper] Adding debug:assetmap command · symfony/symfony@fbb5675 · GitHub
[go: up one dir, main page]

Skip to content

Commit fbb5675

Browse files
committed
[AssetMapper] Adding debug:assetmap command
1 parent 22a932e commit fbb5675

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\AssetMapper\AssetMapperInterface;
1818
use Symfony\Component\AssetMapper\AssetMapperRepository;
1919
use Symfony\Component\AssetMapper\Command\AssetMapperCompileCommand;
20+
use Symfony\Component\AssetMapper\Command\DebugAssetMapperCommand;
2021
use Symfony\Component\AssetMapper\Command\ImportMapExportCommand;
2122
use Symfony\Component\AssetMapper\Command\ImportMapRemoveCommand;
2223
use Symfony\Component\AssetMapper\Command\ImportMapRequireCommand;
@@ -70,6 +71,14 @@
7071
])
7172
->tag('console.command')
7273

74+
->set('asset_mapper.command.debug', DebugAssetMapperCommand::class)
75+
->args([
76+
service('asset_mapper'),
77+
service('asset_mapper.repository'),
78+
param('kernel.project_dir'),
79+
])
80+
->tag('console.command')
81+
7382
->set('asset_mapper_compiler', AssetMapperCompiler::class)
7483
->args([
7584
tagged_iterator('asset_mapper.compiler'),
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 Command;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\Console\Application;
16+
use Symfony\Component\AssetMapper\Tests\fixtures\AssetMapperTestAppKernel;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Component\Filesystem\Filesystem;
19+
use Symfony\Component\Finder\Finder;
20+
21+
class DebugAssetsMapperCommandTest extends TestCase
22+
{
23+
public function testCommandDumpsInformation()
24+
{
25+
$application = new Application(new AssetMapperTestAppKernel('test', true));
26+
27+
$command = $application->find('debug:assetmap');
28+
$tester = new CommandTester($command);
29+
$res = $tester->execute([]);
30+
$this->assertSame(0, $res);
31+
32+
$this->assertStringContainsString('dir1', $tester->getDisplay());
33+
$this->assertStringContainsString('subdir/file6.js', $tester->getDisplay());
34+
$this->assertStringContainsString('dir2/subdir/file6.js', $tester->getDisplay());
35+
}
36+
}

0 commit comments

Comments
 (0)
0