8
8
use Composer \Plugin \PluginInterface ;
9
9
use Composer \Script \Event ;
10
10
use Composer \Util \Platform ;
11
+
11
12
use olvlvl \ComposerAttributeCollector \Filter \ContentFilter ;
12
13
use olvlvl \ComposerAttributeCollector \Filter \InterfaceFilter ;
13
- use olvlvl \ComposerAttributeCollector \Filter \PathFilter ;
14
- use ReflectionException ;
15
14
16
15
use function file_put_contents ;
17
16
use function microtime ;
18
- use function spl_autoload_register ;
19
17
use function sprintf ;
20
18
21
19
use const DIRECTORY_SEPARATOR ;
26
24
final class Plugin implements PluginInterface, EventSubscriberInterface
27
25
{
28
26
public const CACHE_DIR = '.composer-attribute-collector ' ;
27
+ public const VERSION_MAJOR = 2 ;
28
+ public const VERSION_MINOR = 0 ;
29
29
30
30
/**
31
31
* @uses onPostAutoloadDump
@@ -66,62 +66,62 @@ public static function onPostAutoloadDump(Event $event): void
66
66
$ config = Config::from ($ composer );
67
67
$ io = $ event ->getIO ();
68
68
69
+ require_once $ config ->vendorDir . "/autoload.php " ;
70
+
69
71
$ start = microtime (true );
70
72
$ io ->write ('<info>Generating attributes file</info> ' );
71
- self ::dump ($ event -> getComposer (), $ config , $ io );
73
+ self ::dump ($ config , $ io );
72
74
$ elapsed = self ::renderElapsedTime ($ start );
73
75
$ io ->write ("<info>Generated attributes file in $ elapsed</info> " );
74
76
}
75
77
76
- /**
77
- * @throws ReflectionException
78
- */
79
78
public static function dump (
80
- Composer $ composer ,
81
79
Config $ config ,
82
- IOInterface $ io ,
83
- AutoloadsBuilder $ autoloadsBuilder = null ,
84
- ClassMapBuilder $ classMapBuilder = null
80
+ IOInterface $ io
85
81
): void {
82
+ //
83
+ // Scan include paths
84
+ //
85
+ $ start = microtime (true );
86
86
$ datastore = self ::buildDefaultDatastore ($ io );
87
- $ autoloadsBuilder ??= new AutoloadsBuilder ();
88
87
$ classMapGenerator = new MemoizeClassMapGenerator ($ datastore , $ io );
89
- $ classMapBuilder ??= new ClassMapBuilder ($ classMapGenerator );
90
- $ classMapFilter = new MemoizeClassMapFilter ($ datastore , $ io );
91
- $ attributeCollector = new MemoizeAttributeCollector (new ClassAttributeCollector ($ io ), $ datastore , $ io );
92
-
93
- $ start = microtime (true );
94
- $ autoloads = $ autoloadsBuilder ->buildAutoloads ($ composer );
95
- $ elapsed = self ::renderElapsedTime ($ start );
96
- $ io ->debug ("Generating attributes file: built autoloads in $ elapsed " );
97
-
98
- $ start = microtime (true );
99
- $ classMap = $ classMapBuilder ->buildClassMap ($ autoloads );
88
+ foreach ($ config ->include as $ include ) {
89
+ $ classMapGenerator ->scanPaths ($ include , $ config ->excludeRegExp );
90
+ }
91
+ $ classMap = $ classMapGenerator ->getMap ();
100
92
$ elapsed = self ::renderElapsedTime ($ start );
101
- $ io ->debug ("Generating attributes file: built class map in $ elapsed " );
102
-
103
- self ::setupAutoload ($ classMap );
93
+ $ io ->debug ("Generating attributes file: scanned paths in $ elapsed " );
104
94
95
+ //
96
+ // Filter the class map
97
+ //
105
98
$ start = microtime (true );
106
- $ filter = self ::buildFileFilter ($ config );
99
+ $ classMapFilter = new MemoizeClassMapFilter ($ datastore , $ io );
100
+ $ filter = self ::buildFileFilter ();
107
101
$ classMap = $ classMapFilter ->filter (
108
102
$ classMap ,
109
103
fn (string $ class , string $ filepath ): bool => $ filter ->filter ($ filepath , $ class , $ io )
110
104
);
111
105
$ elapsed = self ::renderElapsedTime ($ start );
112
106
$ io ->debug ("Generating attributes file: filtered class map in $ elapsed " );
113
107
108
+ //
109
+ // Collect attributes
110
+ //
114
111
$ start = microtime (true );
112
+ $ attributeCollector = new MemoizeAttributeCollector (new ClassAttributeCollector ($ io ), $ datastore , $ io );
115
113
$ collection = $ attributeCollector ->collectAttributes ($ classMap );
116
114
$ elapsed = self ::renderElapsedTime ($ start );
117
115
$ io ->debug ("Generating attributes file: collected attributes in $ elapsed " );
118
116
117
+ //
118
+ // Render attributes
119
+ //
119
120
$ start = microtime (true );
120
121
$ code = self ::render ($ collection );
122
+ file_put_contents ($ config ->attributesFile , $ code );
121
123
$ elapsed = self ::renderElapsedTime ($ start );
122
124
$ io ->debug ("Generating attributes file: rendered code in $ elapsed " );
123
-
124
- file_put_contents ($ config ->attributesFile , $ code );
125
125
}
126
126
127
127
private static function buildDefaultDatastore (IOInterface $ io ): Datastore
@@ -137,27 +137,9 @@ private static function renderElapsedTime(float $start): string
137
137
{
138
138
return sprintf ("%.03f ms " , (microtime (true ) - $ start ) * 1000 );
139
139
}
140
-
141
- /**
142
- * @param array<class-string, non-empty-string> $classMap
143
- */
144
- private static function setupAutoload (array $ classMap ): void
145
- {
146
- spl_autoload_register (static function (string $ class ) use ($ classMap ): void {
147
- $ file = $ classMap [$ class ] ?? null ;
148
- if ($ file ) {
149
- require_once $ file ;
150
- }
151
- });
152
- }
153
-
154
- private static function buildFileFilter (Config $ config ): Filter
140
+ private static function buildFileFilter (): Filter
155
141
{
156
142
return new Filter \Chain ([
157
- new PathFilter (
158
- include : $ config ->include ,
159
- exclude: $ config ->exclude
160
- ),
161
143
new ContentFilter (),
162
144
new InterfaceFilter ()
163
145
]);
0 commit comments