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

Skip to content

Commit c761332

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

File tree

3 files changed

+157
-0
lines changed
< 10000 input type="text" aria-label="Filter files…" placeholder="Filter files…" aria-describedby=":R35dab:" data-component="input" class="prc-components-Input-Ic-y8" value=""/>

3 files changed

+157
-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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
19+
class DebugAssetsMapperCommandTest extends TestCase
20+
{
21+
public function testCommandDumpsInformation()
22+
{
23+
$application = new Application(new AssetMapperTestAppKernel('test', true));
24+
25+
$command = $application->find('debug:assetmap');
26+
$tester = new CommandTester($command);
27+
$res = $tester->execute([]);
28+
$this->assertSame(0, $res);
29+
30+
$this->assertStringContainsString('dir1', $tester->getDisplay());
31+
$this->assertStringContainsString('subdir/file6.js', $tester->getDisplay());
32+
$this->assertStringContainsString('dir2/subdir/file6.js', $tester->getDisplay());
33+
}
34+
}

0 commit comments

Comments
 (0)
0