Skip to content

Commit a5a7445

Browse files
committed
No cache by default
1 parent 81bb220 commit a5a7445

File tree

7 files changed

+49
-11
lines changed

7 files changed

+49
-11
lines changed

src/Config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Composer\Factory;
66
use Composer\PartialComposer;
7+
use Composer\Util\Platform;
78
use InvalidArgumentException;
89
use RuntimeException;
910

1011
use function array_map;
1112
use function dirname;
13+
use function filter_var;
1214
use function implode;
1315
use function is_string;
1416
use function preg_quote;
@@ -28,6 +30,7 @@ final class Config
2830
public const EXTRA = 'composer-attribute-collector';
2931
public const EXTRA_INCLUDE = 'include';
3032
public const EXTRA_EXCLUDE = 'exclude';
33+
public const ENV_USE_CACHE = 'COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE';
3134

3235
/**
3336
* If a path starts with this placeholder, it is replaced with the absolute path to the vendor directory.
@@ -57,11 +60,14 @@ public static function from(PartialComposer $composer): self
5760
$include = self::expandPaths($extra[self::EXTRA_INCLUDE] ?? [], $vendorDir, $rootDir);
5861
$exclude = self::expandPaths($extra[self::EXTRA_EXCLUDE] ?? [], $vendorDir, $rootDir);
5962

63+
$useCache = filter_var(Platform::getEnv(self::ENV_USE_CACHE), FILTER_VALIDATE_BOOL);
64+
6065
return new self(
6166
$vendorDir,
6267
attributesFile: "$vendorDir/attributes.php",
6368
include: $include,
6469
exclude: $exclude,
70+
useCache: $useCache,
6571
);
6672
}
6773

@@ -78,12 +84,15 @@ public static function from(PartialComposer $composer): self
7884
* Paths that should be included to attributes collection.
7985
* @param non-empty-string[] $exclude
8086
* Paths that should be excluded from attributes collection.
87+
* @param bool $useCache
88+
* Whether a cache should be used during the process.
8189
*/
8290
public function __construct(
8391
public string $vendorDir,
8492
public string $attributesFile,
8593
public array $include,
8694
public array $exclude,
95+
public bool $useCache,
8796
) {
8897
$this->excludeRegExp = count($exclude) ? self::compileExclude($this->exclude) : null;
8998
}

src/FileDatastore.php renamed to src/Datastore/FileDatastore.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
namespace olvlvl\ComposerAttributeCollector;
3+
namespace olvlvl\ComposerAttributeCollector\Datastore;
44

55
use Composer\IO\IOInterface;
6+
use olvlvl\ComposerAttributeCollector\Datastore;
7+
use olvlvl\ComposerAttributeCollector\Plugin;
68

79
use function file_exists;
810
use function file_get_contents;

src/Datastore/RuntimeDatastore.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace olvlvl\ComposerAttributeCollector\Datastore;
4+
5+
use olvlvl\ComposerAttributeCollector\Datastore;
6+
7+
final class RuntimeDatastore implements Datastore
8+
{
9+
/**
10+
* @var array<string, array<int|string, mixed>>
11+
*/
12+
private array $datastore = [];
13+
14+
public function get(string $key): array
15+
{
16+
return $this->datastore[$key] ?? [];
17+
}
18+
19+
public function set(string $key, array $data): void
20+
{
21+
$this->datastore[$key] = $data;
22+
}
23+
}

src/Plugin.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
use Composer\Plugin\PluginInterface;
99
use Composer\Script\Event;
1010
use Composer\Util\Platform;
11-
11+
use olvlvl\ComposerAttributeCollector\Datastore\FileDatastore;
12+
use olvlvl\ComposerAttributeCollector\Datastore\RuntimeDatastore;
1213
use olvlvl\ComposerAttributeCollector\Filter\ContentFilter;
1314
use olvlvl\ComposerAttributeCollector\Filter\InterfaceFilter;
1415

@@ -75,15 +76,13 @@ public static function onPostAutoloadDump(Event $event): void
7576
$io->write("<info>Generated attributes file in $elapsed</info>");
7677
}
7778

78-
public static function dump(
79-
Config $config,
80-
IOInterface $io
81-
): void {
79+
public static function dump(Config $config, IOInterface $io): void
80+
{
8281
//
8382
// Scan include paths
8483
//
8584
$start = microtime(true);
86-
$datastore = self::buildDefaultDatastore($io);
85+
$datastore = self::buildDefaultDatastore($config, $io);
8786
$classMapGenerator = new MemoizeClassMapGenerator($datastore, $io);
8887
foreach ($config->include as $include) {
8988
$classMapGenerator->scanPaths($include, $config->excludeRegExp);
@@ -124,8 +123,12 @@ public static function dump(
124123
$io->debug("Generating attributes file: rendered code in $elapsed");
125124
}
126125

127-
private static function buildDefaultDatastore(IOInterface $io): Datastore
126+
private static function buildDefaultDatastore(Config $config, IOInterface $io): Datastore
128127
{
128+
if (!$config->useCache) {
129+
return new RuntimeDatastore();
130+
}
131+
129132
$basePath = Platform::getCwd();
130133

131134
assert($basePath !== '');
@@ -137,6 +140,7 @@ private static function renderElapsedTime(float $start): string
137140
{
138141
return sprintf("%.03f ms", (microtime(true) - $start) * 1000);
139142
}
143+
140144
private static function buildFileFilter(): Filter
141145
{
142146
return new Filter\Chain([

tests/ClassMapBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Composer\IO\NullIO;
1313
use olvlvl\ComposerAttributeCollector\ClassMapBuilder;
14-
use olvlvl\ComposerAttributeCollector\FileDatastore;
14+
use olvlvl\ComposerAttributeCollector\Datastore\FileDatastore;
1515
use olvlvl\ComposerAttributeCollector\MemoizeClassMapGenerator;
1616
use PHPUnit\Framework\TestCase;
1717

tests/FileDatastoreTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace tests\olvlvl\ComposerAttributeCollector;
44

55
use Composer\IO\IOInterface;
6-
use olvlvl\ComposerAttributeCollector\FileDatastore;
6+
use olvlvl\ComposerAttributeCollector\Datastore\FileDatastore;
77
use olvlvl\ComposerAttributeCollector\Plugin;
88
use PHPUnit\Framework\MockObject\MockObject;
99
use PHPUnit\Framework\TestCase;

tests/MemoizeClassMapGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace tests\olvlvl\ComposerAttributeCollector;
44

55
use Composer\IO\NullIO;
6-
use olvlvl\ComposerAttributeCollector\FileDatastore;
6+
use olvlvl\ComposerAttributeCollector\Datastore\FileDatastore;
77
use olvlvl\ComposerAttributeCollector\MemoizeClassMapGenerator;
88
use PHPUnit\Framework\TestCase;
99

0 commit comments

Comments
 (0)