Skip to content

Commit ca67a62

Browse files
authored
Decouple from IOInterface (#34)
1 parent 990adbc commit ca67a62

21 files changed

+172
-78
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.composer-attribute-collector
22
.phpunit.result.cache
3+
build
34
composer.lock
45
vendor

src/ClassAttributeCollector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace olvlvl\ComposerAttributeCollector;
44

55
use Attribute;
6-
use Composer\IO\IOInterface;
76
use ReflectionAttribute;
87
use ReflectionClass;
98
use ReflectionException;
@@ -14,7 +13,7 @@
1413
class ClassAttributeCollector
1514
{
1615
public function __construct(
17-
private IOInterface $io,
16+
private Logger $log,
1817
) {
1918
}
2019

@@ -45,7 +44,7 @@ public function collectAttributes(string $class): array
4544
continue;
4645
}
4746

48-
$this->io->debug("Found attribute {$attribute->getName()} on $class");
47+
$this->log->debug("Found attribute {$attribute->getName()} on $class");
4948

5049
$classAttributes[] = new TransientTargetClass(
5150
$attribute->getName(),
@@ -63,7 +62,7 @@ public function collectAttributes(string $class): array
6362

6463
$method = $methodReflection->name;
6564

66-
$this->io->debug("Found attribute {$attribute->getName()} on $class::$method");
65+
$this->log->debug("Found attribute {$attribute->getName()} on $class::$method");
6766

6867
$methodAttributes[] = new TransientTargetMethod(
6968
$attribute->getName(),
@@ -84,7 +83,7 @@ public function collectAttributes(string $class): array
8483
$property = $propertyReflection->name;
8584
assert($property !== '');
8685

87-
$this->io->debug("Found attribute {$attribute->getName()} on $class::$property");
86+
$this->log->debug("Found attribute {$attribute->getName()} on $class::$property");
8887

8988
$propertyAttributes[] = new TransientTargetProperty(
9089
$attribute->getName(),

src/Datastore/FileDatastore.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace olvlvl\ComposerAttributeCollector\Datastore;
44

5-
use Composer\IO\IOInterface;
65
use olvlvl\ComposerAttributeCollector\Datastore;
6+
use olvlvl\ComposerAttributeCollector\Logger;
77
use olvlvl\ComposerAttributeCollector\Plugin;
88

99
use function file_exists;
@@ -29,7 +29,7 @@ final class FileDatastore implements Datastore
2929
*/
3030
public function __construct(
3131
private string $dir,
32-
private IOInterface $io,
32+
private Logger $log,
3333
) {
3434
if (!is_dir($dir)) {
3535
mkdir($dir);
@@ -70,7 +70,7 @@ private function safeGet(string $filename): array
7070
set_error_handler(function (int $errno, string $errstr) use (&$errored, $filename): bool {
7171
$errored = true;
7272

73-
$this->io->warning("Unable to unserialize cache item $filename: $errstr");
73+
$this->log->warning("Unable to unserialize cache item $filename: $errstr");
7474

7575
return true;
7676
});

src/Filter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace olvlvl\ComposerAttributeCollector;
44

5-
use Composer\IO\IOInterface;
6-
75
/**
86
* @internal
97
*/
@@ -12,5 +10,5 @@ interface Filter
1210
/**
1311
* @param class-string $class
1412
*/
15-
public function filter(string $filepath, string $class, IOInterface $io): bool;
13+
public function filter(string $filepath, string $class, Logger $log): bool;
1614
}

src/Filter/Chain.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace olvlvl\ComposerAttributeCollector\Filter;
44

5-
use Composer\IO\IOInterface;
65
use olvlvl\ComposerAttributeCollector\Filter;
6+
use olvlvl\ComposerAttributeCollector\Logger;
77

88
final class Chain implements Filter
99
{
@@ -15,10 +15,10 @@ public function __construct(
1515
) {
1616
}
1717

18-
public function filter(string $filepath, string $class, IOInterface $io): bool
18+
public function filter(string $filepath, string $class, Logger $log): bool
1919
{
2020
foreach ($this->filters as $filter) {
21-
if ($filter->filter($filepath, $class, $io) === false) {
21+
if ($filter->filter($filepath, $class, $log) === false) {
2222
return false;
2323
}
2424
}

src/Filter/ContentFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace olvlvl\ComposerAttributeCollector\Filter;
44

5-
use Composer\IO\IOInterface;
65
use olvlvl\ComposerAttributeCollector\Filter;
6+
use olvlvl\ComposerAttributeCollector\Logger;
77

88
use function assert;
99
use function file_get_contents;
@@ -13,7 +13,7 @@
1313

1414
final class ContentFilter implements Filter
1515
{
16-
public function filter(string $filepath, string $class, IOInterface $io): bool
16+
public function filter(string $filepath, string $class, Logger $log): bool
1717
{
1818
$content = file_get_contents($filepath);
1919

@@ -26,7 +26,7 @@ public function filter(string $filepath, string $class, IOInterface $io): bool
2626

2727
// Hint of an attribute class.
2828
if (preg_match('/#\[\\\?Attribute[\]\(]/', $content)) {
29-
$io->debug("Discarding '$class' because it looks like an attribute");
29+
$log->debug("Discarding '$class' because it looks like an attribute");
3030
return false;
3131
}
3232

src/Filter/InterfaceFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
namespace olvlvl\ComposerAttributeCollector\Filter;
44

5-
use Composer\IO\IOInterface;
65
use olvlvl\ComposerAttributeCollector\Filter;
6+
use olvlvl\ComposerAttributeCollector\Logger;
77
use Throwable;
88

99
use function interface_exists;
1010

1111
final class InterfaceFilter implements Filter
1212
{
13-
public function filter(string $filepath, string $class, IOInterface $io): bool
13+
public function filter(string $filepath, string $class, Logger $log): bool
1414
{
1515
try {
1616
if (interface_exists($class)) {
1717
return false;
1818
}
1919
} catch (Throwable $e) {
20-
$io->warning("Discarding '$class' because an error occurred during loading: {$e->getMessage()}");
20+
$log->warning("Discarding '$class' because an error occurred during loading: {$e->getMessage()}");
2121

2222
return false;
2323
}

src/Logger.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace olvlvl\ComposerAttributeCollector;
4+
5+
interface Logger
6+
{
7+
public function debug(string|\Stringable $message): void;
8+
public function warning(string|\Stringable $message): void;
9+
public function error(string|\Stringable $message): void;
10+
}

src/Logger/ComposerLogger.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace olvlvl\ComposerAttributeCollector\Logger;
4+
5+
use Composer\IO\IOInterface;
6+
use olvlvl\ComposerAttributeCollector\Logger;
7+
8+
final class ComposerLogger implements Logger
9+
{
10+
public function __construct(
11+
private IOInterface $io
12+
) {
13+
}
14+
15+
public function debug(\Stringable|string $message): void
16+
{
17+
$this->io->debug($message);
18+
}
19+
20+
public function warning(\Stringable|string $message): void
21+
{
22+
$this->io->warning($message);
23+
}
24+
25+
public function error(\Stringable|string $message): void
26+
{
27+
$this->io->error($message);
28+
}
29+
}

src/MemoizeAttributeCollector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace olvlvl\ComposerAttributeCollector;
44

5-
use Composer\IO\IOInterface;
65
use Throwable;
76

87
use function array_filter;
@@ -35,7 +34,7 @@ class MemoizeAttributeCollector
3534
public function __construct(
3635
private ClassAttributeCollector $classAttributeCollector,
3736
private Datastore $datastore,
38-
private IOInterface $io,
37+
private Logger $log,
3938
) {
4039
/** @phpstan-ignore-next-line */
4140
$this->state = $this->datastore->get(self::KEY);
@@ -66,9 +65,9 @@ public function collectAttributes(array $classMap): TransientCollection
6665
if ($timestamp < $mtime) {
6766
if ($timestamp) {
6867
$diff = $mtime - $timestamp;
69-
$this->io->debug("Refresh attributes of class '$class' in '$filepath' ($diff sec ago)");
68+
$this->log->debug("Refresh attributes of class '$class' in '$filepath' ($diff sec ago)");
7069
} else {
71-
$this->io->debug("Collect attributes of class '$class' in '$filepath'");
70+
$this->log->debug("Collect attributes of class '$class' in '$filepath'");
7271
}
7372

7473
try {
@@ -78,7 +77,7 @@ public function collectAttributes(array $classMap): TransientCollection
7877
$propertyAttributes,
7978
] = $classAttributeCollector->collectAttributes($class);
8079
} catch (Throwable $e) {
81-
$this->io->error(
80+
$this->log->error(
8281
"Attribute collection failed for $class: {$e->getMessage()}",
8382
);
8483
}

0 commit comments

Comments
 (0)