Skip to content

Commit cb7d008

Browse files
committed
AC-15165: Add extensive logging
1 parent 36233da commit cb7d008

File tree

5 files changed

+79
-22
lines changed

5 files changed

+79
-22
lines changed

app/code/Magento/Csp/Model/Deploy/Package/Processor/PostProcessor/Integrity.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Magento\Csp\Model\SubresourceIntegrityCollector;
1515
use Magento\Deploy\Package\Processor\ProcessorInterface;
1616
use Magento\Csp\Model\SubresourceIntegrity\HashGenerator;
17+
use Magento\Framework\App\ObjectManager;
18+
use Psr\Log\LoggerInterface;
1719

1820
/**
1921
* Post-processor that generates integrity hashes after static content package deployed.
@@ -40,35 +42,47 @@ class Integrity implements ProcessorInterface
4042
*/
4143
private SubresourceIntegrityCollector $integrityCollector;
4244

45+
/**
46+
* @var LoggerInterface
47+
*/
48+
private LoggerInterface $logger;
49+
4350
/**
4451
* @param Filesystem $filesystem
4552
* @param HashGenerator $hashGenerator
4653
* @param SubresourceIntegrityFactory $integrityFactory
4754
* @param SubresourceIntegrityCollector $integrityCollector
55+
* @param LoggerInterface|null $logger
4856
*/
4957
public function __construct(
5058
Filesystem $filesystem,
5159
HashGenerator $hashGenerator,
5260
SubresourceIntegrityFactory $integrityFactory,
53-
SubresourceIntegrityCollector $integrityCollector
61+
SubresourceIntegrityCollector $integrityCollector,
62+
?LoggerInterface $logger = null
5463
) {
5564
$this->filesystem = $filesystem;
5665
$this->hashGenerator = $hashGenerator;
5766
$this->integrityFactory = $integrityFactory;
5867
$this->integrityCollector = $integrityCollector;
68+
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
5969
}
6070

6171
/**
6272
* @inheritdoc
6373
*/
6474
public function process(Package $package, array $options): bool
6575
{
76+
$this->logger->info('Integrity PostProcessor: Starting package "' . $package->getPath() . '" (PID: ' . getmypid() . ')');
77+
6678
$staticDir = $this->filesystem->getDirectoryRead(
6779
DirectoryList::ROOT
6880
);
6981

82+
$jsFiles = 0;
7083
foreach ($package->getFiles() as $file) {
7184
if ($file->getExtension() == "js") {
85+
$jsFiles++;
7286
$integrity = $this->integrityFactory->create(
7387
[
7488
"data" => [
@@ -81,9 +95,11 @@ public function process(Package $package, array $options): bool
8195
);
8296

8397
$this->integrityCollector->collect($integrity);
98+
$this->logger->info('Integrity PostProcessor: Collected "' . $file->getDeployedFilePath() . '" (PID: ' . getmypid() . ')');
8499
}
85100
}
86101

102+
$this->logger->info('Integrity PostProcessor: Completed package "' . $package->getPath() . '" - ' . $jsFiles . ' JS files processed (PID: ' . getmypid() . ')');
87103
return true;
88104
}
89105
}

app/code/Magento/Csp/Model/SubresourceIntegrityCollector.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@
1212

1313
/**
1414
* Collector of Integrity objects.
15-
*
16-
* Uses static storage to persist data across ObjectManager instances
17-
* during area emulation in static content deployment.
1815
*/
1916
class SubresourceIntegrityCollector
2017
{
2118
/**
22-
* Global storage that persists across ObjectManager instances
2319
* @var array
2420
*/
25-
private static array $globalData = [];
21+
private array $data = [];
2622

2723
/**
2824
* @var LoggerInterface
@@ -35,7 +31,7 @@ class SubresourceIntegrityCollector
3531
public function __construct(?LoggerInterface $logger = null) {
3632
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
3733

38-
$this->logger->info('SRI Collector: Initialized with ' . count(self::$globalData) . ' objects (global storage)');
34+
$this->logger->info('SRI Collector: Initialized (PID: ' . getmypid() . ')');
3935
}
4036

4137
/**
@@ -47,8 +43,8 @@ public function __construct(?LoggerInterface $logger = null) {
4743
*/
4844
public function collect(SubresourceIntegrity $integrity): void
4945
{
50-
self::$globalData[] = $integrity;
51-
$this->logger->info('SRI Collector: Collected object, total: ' . count(self::$globalData));
46+
$this->data[] = $integrity;
47+
$this->logger->info('SRI Collector: Collected "' . $integrity->getPath() . '" - Total: ' . count($this->data) . ' (PID: ' . getmypid() . ')');
5248
}
5349

5450
/**
@@ -58,9 +54,9 @@ public function collect(SubresourceIntegrity $integrity): void
5854
*/
5955
public function release(): array
6056
{
61-
$count = count(self::$globalData);
62-
$this->logger->info('SRI Collector: Releasing ' . $count . ' objects');
63-
return self::$globalData;
57+
$count = count($this->data);
58+
$this->logger->info('SRI Collector: Releasing ' . $count . ' objects (PID: ' . getmypid() . ')');
59+
return $this->data;
6460
}
6561

6662
/**
@@ -70,8 +66,8 @@ public function release(): array
7066
*/
7167
public function clear(): void
7268
{
73-
$count = count(self::$globalData);
74-
self::$globalData = [];
75-
$this->logger->info('SRI Collector: Cleared ' . $count . ' objects');
69+
$count = count($this->data);
70+
$this->data = [];
71+
$this->logger->info('SRI Collector: Cleared ' . $count . ' objects (PID: ' . getmypid() . ')');
7672
}
7773
}

app/code/Magento/Csp/Plugin/GenerateAssetIntegrity.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Csp\Model\SubresourceIntegrityFactory;
1313
use Magento\Csp\Model\SubresourceIntegrityCollector;
1414
use Magento\Csp\Model\SubresourceIntegrity\HashGenerator;
15+
use Magento\Framework\App\ObjectManager;
16+
use Psr\Log\LoggerInterface;
1517

1618
/**
1719
* Plugin to add asset integrity value after static content deploy.
@@ -40,19 +42,27 @@ class GenerateAssetIntegrity
4042
*/
4143
private SubresourceIntegrityCollector $integrityCollector;
4244

45+
/**
46+
* @var LoggerInterface
47+
*/
48+
private LoggerInterface $logger;
49+
4350
/**
4451
* @param HashGenerator $hashGenerator
4552
* @param SubresourceIntegrityFactory $integrityFactory
4653
* @param SubresourceIntegrityCollector $integrityCollector
54+
* @param LoggerInterface|null $logger
4755
*/
4856
public function __construct(
4957
HashGenerator $hashGenerator,
5058
SubresourceIntegrityFactory $integrityFactory,
51-
SubresourceIntegrityCollector $integrityCollector
59+
SubresourceIntegrityCollector $integrityCollector,
60+
?LoggerInterface $logger = null
5261
) {
5362
$this->hashGenerator = $hashGenerator;
5463
$this->integrityFactory = $integrityFactory;
5564
$this->integrityCollector = $integrityCollector;
65+
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
5666
}
5767

5868
/**
@@ -70,6 +80,8 @@ public function afterCreateRequireJsConfigAsset(
7080
File $result
7181
): File {
7282
if (PHP_SAPI == 'cli') {
83+
$this->logger->info('GenerateAssetIntegrity: Called for "' . $result->getPath() . '" (PID: ' . getmypid() . ')');
84+
7385
if (in_array($result->getContentType(), self::CONTENT_TYPES)) {
7486
$integrity = $this->integrityFactory->create(
7587
[
@@ -83,6 +95,7 @@ public function afterCreateRequireJsConfigAsset(
8395
);
8496

8597
$this->integrityCollector->collect($integrity);
98+
$this->logger->info('GenerateAssetIntegrity: Collected "' . $result->getPath() . '" (PID: ' . getmypid() . ')');
8699
}
87100
}
88101

app/code/Magento/Csp/Plugin/GenerateBundleAssetIntegrity.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use Magento\Csp\Model\SubresourceIntegrityFactory;
1313
use Magento\Deploy\Service\Bundle;
1414
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\App\ObjectManager;
1516
use Magento\Framework\Exception\FileSystemException;
1617
use Magento\Framework\Filesystem;
1718
use Magento\Framework\Filesystem\Io\File;
19+
use Psr\Log\LoggerInterface;
1820

1921
class GenerateBundleAssetIntegrity
2022
{
@@ -43,25 +45,33 @@ class GenerateBundleAssetIntegrity
4345
*/
4446
private File $fileIo;
4547

48+
/**
49+
* @var LoggerInterface
50+
*/
51+
private LoggerInterface $logger;
52+
4653
/**
4754
* @param HashGenerator $hashGenerator
4855
* @param SubresourceIntegrityFactory $integrityFactory
4956
* @param SubresourceIntegrityCollector $integrityCollector
5057
* @param Filesystem $filesystem
5158
* @param File $fileIo
59+
* @param LoggerInterface|null $logger
5260
*/
5361
public function __construct(
5462
HashGenerator $hashGenerator,
5563
SubresourceIntegrityFactory $integrityFactory,
5664
SubresourceIntegrityCollector $integrityCollector,
5765
Filesystem $filesystem,
5866
File $fileIo,
67+
?LoggerInterface $logger = null
5968
) {
6069
$this->hashGenerator = $hashGenerator;
6170
$this->integrityFactory = $integrityFactory;
6271
$this->integrityCollector = $integrityCollector;
6372
$this->filesystem = $filesystem;
6473
$this->fileIo = $fileIo;
74+
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
6575
}
6676

6777
/**
@@ -79,24 +89,32 @@ public function __construct(
7989
public function afterDeploy(Bundle $subject, ?string $result, string $area, string $theme, string $locale)
8090
{
8191
if (PHP_SAPI == 'cli') {
92+
$this->logger->info('GenerateBundleAssetIntegrity: Called for area=' . $area . ', theme=' . $theme . ', locale=' . $locale . ' (PID: ' . getmypid() . ')');
93+
8294
$pubStaticDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
8395
$files = $pubStaticDir->search(
8496
$area ."/" . $theme . "/" . $locale . "/" . Bundle::BUNDLE_JS_DIR . "/*.js"
8597
);
98+
99+
$this->logger->info('GenerateBundleAssetIntegrity: Found ' . count($files) . ' bundle files (PID: ' . getmypid() . ')');
100+
86101
foreach ($files as $file) {
102+
$bundlePath = $area . '/' . $theme . '/' . $locale .
103+
"/" . Bundle::BUNDLE_JS_DIR . '/' . $this->fileIo->getPathInfo($file)['basename'];
104+
87105
$integrity = $this->integrityFactory->create(
88106
[
89107
"data" => [
90108
'hash' => $this->hashGenerator->generate(
91109
$pubStaticDir->readFile($file)
92110
),
93-
'path' => $area . '/' . $theme . '/' . $locale .
94-
"/" . Bundle::BUNDLE_JS_DIR . '/' . $this->fileIo->getPathInfo($file)['basename']
111+
'path' => $bundlePath
95112
]
96113
]
97114
);
98115

99116
$this->integrityCollector->collect($integrity);
117+
$this->logger->info('GenerateBundleAssetIntegrity: Collected "' . $bundlePath . '" (PID: ' . getmypid() . ')');
100118
}
101119
}
102120
}

app/code/Magento/Csp/Plugin/StoreAssetIntegrityHashes.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,31 @@ public function afterDeploy(
5454
mixed $result,
5555
array $options
5656
): void {
57+
$this->logger->info('SRI Store: Starting deployment storage (PID: ' . getmypid() . ')');
58+
5759
$bunches = [];
60+
$integrityHashes = $this->integrityCollector->release();
61+
62+
$this->logger->info('SRI Store: Released ' . count($integrityHashes) . ' objects from collector (PID: ' . getmypid() . ')');
5863

59-
foreach ($this->integrityCollector->release() as $integrity) {
64+
foreach ($integrityHashes as $integrity) {
6065
$area = explode("/", $integrity->getPath())[0];
61-
6266
$bunches[$area][] = $integrity;
6367
}
6468

69+
$this->logger->info('SRI Store: Grouped into areas: ' . implode(', ', array_map(function($area, $bunch) {
70+
return $area . '(' . count($bunch) . ')';
71+
}, array_keys($bunches), $bunches)) . ' (PID: ' . getmypid() . ')');
72+
6573
foreach ($bunches as $area => $bunch) {
66-
$this->integrityRepositoryPool->get($area)
67-
->saveBunch($bunch);
74+
try {
75+
$this->integrityRepositoryPool->get($area)->saveBunch($bunch);
76+
$this->logger->info('SRI Store: ✓ Saved ' . count($bunch) . ' objects for ' . $area . ' (PID: ' . getmypid() . ')');
77+
} catch (\Exception $e) {
78+
$this->logger->error('SRI Store: ✗ Failed saving ' . $area . ': ' . $e->getMessage() . ' (PID: ' . getmypid() . ')');
79+
}
6880
}
81+
82+
$this->logger->info('SRI Store: Deployment storage complete (PID: ' . getmypid() . ')');
6983
}
7084
}

0 commit comments

Comments
 (0)