4
4
[ ![ Code Coverage] ( https://coveralls.io/repos/github/olvlvl/composer-attribute-collector/badge.svg?branch=main )] ( https://coveralls.io/r/olvlvl/composer-attribute-collector?branch=main )
5
5
[ ![ Downloads] ( https://img.shields.io/packagist/dt/olvlvl/composer-attribute-collector.svg )] ( https://packagist.org/packages/olvlvl/composer-attribute-collector )
6
6
7
- composer-attribute-collector is a plugin for [ Composer] [ ] . Its ambition is to provide a convenient
8
- way—and near zero cost—to retrieve targets of PHP 8 attributes. After the autoloader has been
9
- dumped, the plugin collects attribute targets and generates a static file. Later, these targets can
10
- be retrieved through a convenient interface, without reflection. The plugin is useful when you need
11
- to _ discover_ attribute targets in a codebase—for known targets you can use reflection.
7
+ ** composer-attribute-collector** is a plugin for [ Composer] [ ] . Its ambition is to provide a
8
+ convenient way—and near zero cost—to retrieve targets of PHP 8 attributes. After the autoloader has
9
+ been dumped, the plugin collects attribute targets and generates a static file. These targets can be
10
+ retrieved through a convenient interface, without reflection. The plugin is useful when you need to
11
+ _ discover_ attribute targets in a codebase—for known targets you can use reflection.
12
12
13
13
14
14
@@ -21,6 +21,10 @@ to _discover_ attribute targets in a codebase—for known targets you can use re
21
21
- A single interface to get attribute targets: classes, methods, and properties
22
22
- Can cache discoveries to speed up consecutive runs.
23
23
24
+ > [ !NOTE]
25
+ > Currently, the plugin supports class, method, and property targets.
26
+ > You're welcome to [ contribute] ( CONTRIBUTING.md ) if you're interested in expending its support.
27
+
24
28
25
29
26
30
#### Usage
@@ -77,26 +81,13 @@ var_dump($attributes->propertyAttributes);
77
81
78
82
## Getting started
79
83
84
+ Here are a few steps to get you started.
80
85
81
- ### Installation
82
-
83
- ``` shell
84
- composer require olvlvl/composer-attribute-collector
85
- ```
86
-
87
- The plugin is currently experimental and its interface subject to change. At the moment, it only
88
- supports class, method, and property targets. Please [ contribute] ( CONTRIBUTING.md ) if you're interested in
89
- shaping its future.
90
-
91
-
92
-
93
- ### Sample configuration
86
+ ### 1\. Configure the plugin
94
87
95
- The plugin only inspects paths and files specified in the configuration with the direction
96
- ` include ` . That's usually your "src" directory. Add this section to your ` composer.json ` file to
97
- enable the generation of the attributes file on autoload dump.
98
-
99
- Check the [ Configuration options] ( #configuration ) for more details.
88
+ The plugin only inspects paths and files specified in the configuration with the ` include ` property.
89
+ That is usually your "src" directory. Add this section to your ` composer.json ` file to enable the
90
+ generation of the "attributes" file when the autoloader is dumped.
100
91
101
92
``` json
102
93
{
@@ -110,12 +101,38 @@ Check the [Configuration options](#configuration) for more details.
110
101
}
111
102
```
112
103
104
+ Check the [ Configuration options] ( #configuration ) for more details.
105
+
113
106
114
107
115
- ### Autoloading
108
+ ### 2 \. Install the plugin
116
109
117
- You can require the attributes file using ` require_once 'vendor/attributes.php'; ` but you might
118
- prefer using Composer's autoloading feature:
110
+ Use [ Composer] [ ] to install the plugin.
111
+ You will be asked if you trust the plugin and wish to activate it, select ` y ` to proceed.
112
+
113
+ ``` shell
114
+ composer require olvlvl/composer-attribute-collector
115
+ ```
116
+
117
+ You should see log messages similar to this:
118
+
119
+ ```
120
+ Generating autoload files
121
+ Generating attributes file
122
+ Generated attributes file in 9.137 ms
123
+ Generated autoload files
124
+ ```
125
+
126
+ > [ !TIP]
127
+ > See the [ Frequently Asked Questions] ( #frequently-asked-questions ) section
128
+ > to automatically refresh the "attributes" file during development.
129
+
130
+
131
+
132
+ ### 3\. Autoload the "attributes" file
133
+
134
+ You can require the "attributes" file using ` require_once 'vendor/attributes.php'; ` but you might
135
+ prefer to use Composer's autoloading feature:
119
136
120
137
``` json
121
138
{
@@ -131,10 +148,14 @@ prefer using Composer's autoloading feature:
131
148
132
149
## Configuration
133
150
151
+ Here are a few ways you can configure the plugin.
152
+
153
+
154
+
134
155
### Including paths or files ([ root-only] [ ] )
135
156
136
157
Use the ` include ` property to define the paths or files to inspect for attributes. Without this
137
- property, the attributes file will be empty.
158
+ property, the " attributes" file will be empty.
138
159
139
160
The specified paths are relative to the ` composer.json ` file, and the ` {vendor} ` placeholder is
140
161
replaced with the path to the vendor folder.
@@ -178,48 +199,24 @@ set the environment variable `COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE` to `1`, `y
178
199
Cache items are persisted in the ` .composer-attribute-collector ` directory, you might want to add
179
200
it to your ` .gitignore ` file.
180
201
202
+ ``` shell
203
+ COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=1 composer dump-autoload
204
+ ```
205
+
181
206
182
207
183
208
## Test drive with the Symfony Demo
184
209
185
210
You can try the plugin with a fresh installation of the [ Symfony Demo Application] ( https://github.com/symfony/demo ) .
186
211
187
- Add the ` composer-attribute-collector ` node to ` extra ` and the autoload item to the ` composer.json ` file:
188
-
189
- ``` json
190
- {
191
- "autoload" : {
192
- "files" : [
193
- " vendor/attributes.php"
194
- ]
195
- },
196
- "extra" : {
197
- "composer-attribute-collector" : {
198
- "include" : [
199
- " src"
200
- ]
201
- }
202
- }
203
- }
204
- ```
205
-
206
- Use Composer to install the plugin. You'll be asked if you trust the plugin and wish to activate it.
207
- If you wish to continue, choose ` y ` .
208
-
209
- ``` shell
210
- composer require olvlvl/composer-attribute-collector
211
- ```
212
-
213
- You should see log messages similar to this:
212
+ > [ !TIP]
213
+ > The demo application configured with the plugin is [ available on GitHub] ( https://github.com/olvlvl/composer-attribute-collector-usecase-symfony ) .
214
214
215
- ```
216
- Generating autoload files
217
- Generating attributes file
218
- Generated attributes file in 9.137 ms
219
- Generated autoload files
220
- ```
215
+ See the [ Getting started] ( #getting-started ) section to set up the plugin. If all went well, the file
216
+ ` vendor/attributes.php ` should be available.
221
217
222
- The plugin should have generated the file ` vendor/attributes.php ` . Let's see if we can get the controller methods tagged as routes. Create a PHP file with the following content and run it:
218
+ Now, you can try to get the controller methods tagged as routes. Create a PHP file with the
219
+ following content and run it:
223
220
224
221
``` php
225
222
<?php
@@ -248,8 +245,6 @@ action: App\Controller\BlogController#commentNew, path: /comment/{postSlug}/new
248
245
action: App\Controller\BlogController#search, path: /search
249
246
```
250
247
251
- The demo application configured with the plugin is [ available on GitHub] ( https://github.com/olvlvl/composer-attribute-collector-usecase-symfony ) .
252
-
253
248
254
249
255
250
## Frequently Asked Questions
@@ -261,15 +256,15 @@ to Composer to find classes. Anything that works with Composer should work with
261
256
262
257
** Can I use the plugin during development?**
263
258
264
- Yes, you can use the plugin during development, but keep in mind the attributes file is only
265
- generated after the autoloader is dumped. If you modify attributes you'll have to run
266
- ` composer dump ` to refresh the attributes file.
259
+ Yes, you can use the plugin during development, but keep in mind the " attributes" file is only
260
+ generated after the autoloader is dumped. If you modify attributes you will have to run
261
+ ` composer dump-autoload ` to refresh the " attributes" file.
267
262
268
263
As a workaround you could have watchers on the directories that contain classes with attributes to
269
- run ` XDEBUG_MODE=off composer dump ` when you make changes. [ PhpStorm offers file watchers ] [ phpstorm-watchers ] .
270
- You could also use [ spatie/file-system-watcher] [ ] , it only requires PHP. If the plugin is too slow
271
- for your liking, try running the command with ` COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=yes ` , it will
272
- enable caching and speed up consecutive runs.
264
+ run ` XDEBUG_MODE=off composer dump-autoload ` when you make changes. [ PhpStorm offers file
265
+ watchers ] [ phpstorm-watchers ] . You could also use [ spatie/file-system-watcher] [ ] , it only requires
266
+ PHP. If the plugin is too slow for your liking, try running the command with
267
+ ` COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=yes ` , it will enable caching and speed up consecutive runs.
273
268
274
269
275
270
0 commit comments