Skip to content

Commit 602bd0b

Browse files
committed
AC-15165: Improve SRI Collector design.
Save files after each package area is deployed.
1 parent cb7d008 commit 602bd0b

File tree

5 files changed

+29
-72
lines changed

5 files changed

+29
-72
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Csp\Model\SubresourceIntegrityFactory;
1313
use Magento\Framework\App\Filesystem\DirectoryList;
1414
use Magento\Csp\Model\SubresourceIntegrityCollector;
15+
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
1516
use Magento\Deploy\Package\Processor\ProcessorInterface;
1617
use Magento\Csp\Model\SubresourceIntegrity\HashGenerator;
1718
use Magento\Framework\App\ObjectManager;
@@ -42,6 +43,11 @@ class Integrity implements ProcessorInterface
4243
*/
4344
private SubresourceIntegrityCollector $integrityCollector;
4445

46+
/**
47+
* @var SubresourceIntegrityRepositoryPool
48+
*/
49+
private SubresourceIntegrityRepositoryPool $repositoryPool;
50+
4551
/**
4652
* @var LoggerInterface
4753
*/
@@ -53,36 +59,35 @@ class Integrity implements ProcessorInterface
5359
* @param SubresourceIntegrityFactory $integrityFactory
5460
* @param SubresourceIntegrityCollector $integrityCollector
5561
* @param LoggerInterface|null $logger
62+
* @param SubresourceIntegrityRepositoryPool|null $repositoryPool
5663
*/
5764
public function __construct(
5865
Filesystem $filesystem,
5966
HashGenerator $hashGenerator,
6067
SubresourceIntegrityFactory $integrityFactory,
6168
SubresourceIntegrityCollector $integrityCollector,
62-
?LoggerInterface $logger = null
69+
?LoggerInterface $logger = null,
70+
?SubresourceIntegrityRepositoryPool $repositoryPool = null
6371
) {
6472
$this->filesystem = $filesystem;
6573
$this->hashGenerator = $hashGenerator;
6674
$this->integrityFactory = $integrityFactory;
6775
$this->integrityCollector = $integrityCollector;
6876
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
77+
$this->repositoryPool = $repositoryPool ?? ObjectManager::getInstance()->get(SubresourceIntegrityRepositoryPool::class);
6978
}
7079

7180
/**
7281
* @inheritdoc
7382
*/
7483
public function process(Package $package, array $options): bool
7584
{
76-
$this->logger->info('Integrity PostProcessor: Starting package "' . $package->getPath() . '" (PID: ' . getmypid() . ')');
77-
7885
$staticDir = $this->filesystem->getDirectoryRead(
7986
DirectoryList::ROOT
8087
);
8188

82-
$jsFiles = 0;
8389
foreach ($package->getFiles() as $file) {
84-
if ($file->getExtension() == "js") {
85-
$jsFiles++;
90+
if (strtolower($file->getExtension()) === "js") {
8691
$integrity = $this->integrityFactory->create(
8792
[
8893
"data" => [
@@ -95,11 +100,24 @@ public function process(Package $package, array $options): bool
95100
);
96101

97102
$this->integrityCollector->collect($integrity);
98-
$this->logger->info('Integrity PostProcessor: Collected "' . $file->getDeployedFilePath() . '" (PID: ' . getmypid() . ')');
99103
}
100104
}
101105

102-
$this->logger->info('Integrity PostProcessor: Completed package "' . $package->getPath() . '" - ' . $jsFiles . ' JS files processed (PID: ' . getmypid() . ')');
106+
// Save collected data directly to repository before process exits
107+
$collectedData = $this->integrityCollector->release();
108+
if (!empty($collectedData)) {
109+
$area = explode('/', $package->getPath())[0];
110+
try {
111+
$this->repositoryPool->get($area)->saveBunch($collectedData);
112+
} catch (\Exception $e) {
113+
$this->logger->error('Integrity PostProcessor: Failed saving to ' . $area . ' repository: ' . $e->getMessage());
114+
}
115+
116+
// Clear collector for next package (if any)
117+
$this->integrityCollector->clear();
118+
}
119+
103120
return true;
104121
}
105122
}
123+

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
namespace Magento\Csp\Model;
99

10-
use Magento\Framework\App\ObjectManager;
11-
use Psr\Log\LoggerInterface;
12-
1310
/**
1411
* Collector of Integrity objects.
1512
*/
@@ -20,20 +17,6 @@ class SubresourceIntegrityCollector
2017
*/
2118
private array $data = [];
2219

23-
/**
24-
* @var LoggerInterface
25-
*/
26-
private LoggerInterface $logger;
27-
28-
/**
29-
* @param LoggerInterface|null $logger
30-
*/
31-
public function __construct(?LoggerInterface $logger = null) {
32-
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
33-
34-
$this->logger->info('SRI Collector: Initialized (PID: ' . getmypid() . ')');
35-
}
36-
3720
/**
3821
* Collects given Integrity object.
3922
*
@@ -44,7 +27,6 @@ public function __construct(?LoggerInterface $logger = null) {
4427
public function collect(SubresourceIntegrity $integrity): void
4528
{
4629
$this->data[] = $integrity;
47-
$this->logger->info('SRI Collector: Collected "' . $integrity->getPath() . '" - Total: ' . count($this->data) . ' (PID: ' . getmypid() . ')');
4830
}
4931

5032
/**
@@ -54,8 +36,6 @@ public function collect(SubresourceIntegrity $integrity): void
5436
*/
5537
public function release(): array
5638
{
57-
$count = count($this->data);
58-
$this->logger->info('SRI Collector: Releasing ' . $count . ' objects (PID: ' . getmypid() . ')');
5939
return $this->data;
6040
}
6141

@@ -66,8 +46,6 @@ public function release(): array
6646
*/
6747
public function clear(): void
6848
{
69-
$count = count($this->data);
7049
$this->data = [];
71-
$this->logger->info('SRI Collector: Cleared ' . $count . ' objects (PID: ' . getmypid() . ')');
7250
}
7351
}

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
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;
1715

1816
/**
1917
* Plugin to add asset integrity value after static content deploy.
@@ -42,27 +40,19 @@ class GenerateAssetIntegrity
4240
*/
4341
private SubresourceIntegrityCollector $integrityCollector;
4442

45-
/**
46-
* @var LoggerInterface
47-
*/
48-
private LoggerInterface $logger;
49-
5043
/**
5144
* @param HashGenerator $hashGenerator
5245
* @param SubresourceIntegrityFactory $integrityFactory
5346
* @param SubresourceIntegrityCollector $integrityCollector
54-
* @param LoggerInterface|null $logger
5547
*/
5648
public function __construct(
5749
HashGenerator $hashGenerator,
5850
SubresourceIntegrityFactory $integrityFactory,
59-
SubresourceIntegrityCollector $integrityCollector,
60-
?LoggerInterface $logger = null
51+
SubresourceIntegrityCollector $integrityCollector
6152
) {
6253
$this->hashGenerator = $hashGenerator;
6354
$this->integrityFactory = $integrityFactory;
6455
$this->integrityCollector = $integrityCollector;
65-
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
6656
}
6757

6858
/**
@@ -80,8 +70,6 @@ public function afterCreateRequireJsConfigAsset(
8070
File $result
8171
): File {
8272
if (PHP_SAPI == 'cli') {
83-
$this->logger->info('GenerateAssetIntegrity: Called for "' . $result->getPath() . '" (PID: ' . getmypid() . ')');
84-
8573
if (in_array($result->getContentType(), self::CONTENT_TYPES)) {
8674
$integrity = $this->integrityFactory->create(
8775
[
@@ -95,7 +83,6 @@ public function afterCreateRequireJsConfigAsset(
9583
);
9684

9785
$this->integrityCollector->collect($integrity);
98-
$this->logger->info('GenerateAssetIntegrity: Collected "' . $result->getPath() . '" (PID: ' . getmypid() . ')');
9986
}
10087
}
10188

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
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;
1615
use Magento\Framework\Exception\FileSystemException;
1716
use Magento\Framework\Filesystem;
1817
use Magento\Framework\Filesystem\Io\File;
19-
use Psr\Log\LoggerInterface;
2018

2119
class GenerateBundleAssetIntegrity
2220
{
@@ -45,33 +43,25 @@ class GenerateBundleAssetIntegrity
4543
*/
4644
private File $fileIo;
4745

48-
/**
49-
* @var LoggerInterface
50-
*/
51-
private LoggerInterface $logger;
52-
5346
/**
5447
* @param HashGenerator $hashGenerator
5548
* @param SubresourceIntegrityFactory $integrityFactory
5649
* @param SubresourceIntegrityCollector $integrityCollector
5750
* @param Filesystem $filesystem
5851
* @param File $fileIo
59-
* @param LoggerInterface|null $logger
6052
*/
6153
public function __construct(
6254
HashGenerator $hashGenerator,
6355
SubresourceIntegrityFactory $integrityFactory,
6456
SubresourceIntegrityCollector $integrityCollector,
6557
Filesystem $filesystem,
66-
File $fileIo,
67-
?LoggerInterface $logger = null
58+
File $fileIo
6859
) {
6960
$this->hashGenerator = $hashGenerator;
7061
$this->integrityFactory = $integrityFactory;
7162
$this->integrityCollector = $integrityCollector;
7263
$this->filesystem = $filesystem;
7364
$this->fileIo = $fileIo;
74-
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
7565
}
7666

7767
/**
@@ -89,15 +79,11 @@ public function __construct(
8979
public function afterDeploy(Bundle $subject, ?string $result, string $area, string $theme, string $locale)
9080
{
9181
if (PHP_SAPI == 'cli') {
92-
$this->logger->info('GenerateBundleAssetIntegrity: Called for area=' . $area . ', theme=' . $theme . ', locale=' . $locale . ' (PID: ' . getmypid() . ')');
93-
9482
$pubStaticDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
9583
$files = $pubStaticDir->search(
9684
$area ."/" . $theme . "/" . $locale . "/" . Bundle::BUNDLE_JS_DIR . "/*.js"
9785
);
9886

99-
$this->logger->info('GenerateBundleAssetIntegrity: Found ' . count($files) . ' bundle files (PID: ' . getmypid() . ')');
100-
10187
foreach ($files as $file) {
10288
$bundlePath = $area . '/' . $theme . '/' . $locale .
10389
"/" . Bundle::BUNDLE_JS_DIR . '/' . $this->fileIo->getPathInfo($file)['basename'];
@@ -114,7 +100,6 @@ public function afterDeploy(Bundle $subject, ?string $result, string $area, stri
114100
);
115101

116102
$this->integrityCollector->collect($integrity);
117-
$this->logger->info('GenerateBundleAssetIntegrity: Collected "' . $bundlePath . '" (PID: ' . getmypid() . ')');
118103
}
119104
}
120105
}

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

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

6460
foreach ($integrityHashes as $integrity) {
6561
$area = explode("/", $integrity->getPath())[0];
6662
$bunches[$area][] = $integrity;
6763
}
6864

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-
7365
foreach ($bunches as $area => $bunch) {
7466
try {
7567
$this->integrityRepositoryPool->get($area)->saveBunch($bunch);
76-
$this->logger->info('SRI Store: ✓ Saved ' . count($bunch) . ' objects for ' . $area . ' (PID: ' . getmypid() . ')');
7768
} catch (\Exception $e) {
78-
$this->logger->error('SRI Store: Failed saving ' . $area . ': ' . $e->getMessage() . ' (PID: ' . getmypid() . ')');
69+
$this->logger->error('SRI Store: Failed saving ' . $area . ': ' . $e->getMessage());
7970
}
8071
}
81-
82-
$this->logger->info('SRI Store: Deployment storage complete (PID: ' . getmypid() . ')');
8372
}
8473
}

0 commit comments

Comments
 (0)