Skip to content

Commit 10bcbea

Browse files
committed
Tidy documentation
1 parent 3222232 commit 10bcbea

16 files changed

+76
-145
lines changed

README.md

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
[![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)
55
[![Downloads](https://img.shields.io/packagist/dt/olvlvl/composer-attribute-collector.svg)](https://packagist.org/packages/olvlvl/composer-attribute-collector)
66

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.
1212

1313

1414

@@ -21,6 +21,10 @@ to _discover_ attribute targets in a codebase—for known targets you can use re
2121
- A single interface to get attribute targets: classes, methods, and properties
2222
- Can cache discoveries to speed up consecutive runs.
2323

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+
2428

2529

2630
#### Usage
@@ -77,26 +81,13 @@ var_dump($attributes->propertyAttributes);
7781

7882
## Getting started
7983

84+
Here are a few steps to get you started.
8085

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
9487

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.
10091

10192
```json
10293
{
@@ -110,12 +101,38 @@ Check the [Configuration options](#configuration) for more details.
110101
}
111102
```
112103

104+
Check the [Configuration options](#configuration) for more details.
105+
113106

114107

115-
### Autoloading
108+
### 2\. Install the plugin
116109

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:
119136

120137
```json
121138
{
@@ -131,10 +148,14 @@ prefer using Composer's autoloading feature:
131148

132149
## Configuration
133150

151+
Here are a few ways you can configure the plugin.
152+
153+
154+
134155
### Including paths or files ([root-only][])
135156

136157
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.
138159

139160
The specified paths are relative to the `composer.json` file, and the `{vendor}` placeholder is
140161
replaced with the path to the vendor folder.
@@ -178,48 +199,24 @@ set the environment variable `COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE` to `1`, `y
178199
Cache items are persisted in the `.composer-attribute-collector` directory, you might want to add
179200
it to your `.gitignore` file.
180201

202+
```shell
203+
COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=1 composer dump-autoload
204+
```
205+
181206

182207

183208
## Test drive with the Symfony Demo
184209

185210
You can try the plugin with a fresh installation of the [Symfony Demo Application](https://github.com/symfony/demo).
186211

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).
214214
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.
221217

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:
223220

224221
```php
225222
<?php
@@ -248,8 +245,6 @@ action: App\Controller\BlogController#commentNew, path: /comment/{postSlug}/new
248245
action: App\Controller\BlogController#search, path: /search
249246
```
250247

251-
The demo application configured with the plugin is [available on GitHub](https://github.com/olvlvl/composer-attribute-collector-usecase-symfony).
252-
253248

254249

255250
## Frequently Asked Questions
@@ -261,15 +256,15 @@ to Composer to find classes. Anything that works with Composer should work with
261256

262257
**Can I use the plugin during development?**
263258

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.
267262

268263
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.
273268

274269

275270

src/Attributes.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<?php
22

3-
/*
4-
* (c) Olivier Laviale <[email protected]>
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
9-
103
namespace olvlvl\ComposerAttributeCollector;
114

125
use Closure;

src/ClassAttributeCollector.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use ReflectionAttribute;
88
use ReflectionClass;
99
use ReflectionException;
10-
use Throwable;
1110

1211
/**
1312
* @internal
@@ -27,8 +26,6 @@ public function __construct(
2726
* array<TransientTargetMethod>,
2827
* array<TransientTargetProperty>,
2928
* }
30-
* Where `0` is an array of class attributes, `1` is an array of method attributes,
31-
* and `2` is an array of property attributes.
3229
*
3330
* @throws ReflectionException
3431
*/

src/Collection.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<?php
22

3-
/*
4-
* (c) Olivier Laviale <[email protected]>
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
9-
103
namespace olvlvl\ComposerAttributeCollector;
114

125
use RuntimeException;

src/Config.php

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

33
namespace olvlvl\ComposerAttributeCollector;
44

5-
use Composer\Composer;
65
use Composer\Factory;
76
use Composer\PartialComposer;
87
use Composer\Util\Platform;
@@ -91,9 +90,9 @@ public static function resolveVendorDir(PartialComposer $composer): string
9190
* @param non-empty-string $attributesFile
9291
* Absolute path to the `attributes.php` file.
9392
* @param non-empty-string[] $include
94-
* Paths that should be included to attributes collection.
93+
* Paths that should be included in the attribute collection.
9594
* @param non-empty-string[] $exclude
96-
* Paths that should be excluded from attributes collection.
95+
* Paths that should be excluded from the attribute collection.
9796
* @param bool $useCache
9897
* Whether a cache should be used during the process.
9998
*/

src/Filter/Chain.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<?php
22

3-
/*
4-
* (c) Olivier Laviale <[email protected]>
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
9-
103
namespace olvlvl\ComposerAttributeCollector\Filter;
114

125
use Composer\IO\IOInterface;

src/Filter/ContentFilter.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<?php
22

3-
/*
4-
* (c) Olivier Laviale <[email protected]>
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
9-
103
namespace olvlvl\ComposerAttributeCollector\Filter;
114

125
use Composer\IO\IOInterface;

src/Filter/InterfaceFilter.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<?php
22

3-
/*
4-
* (c) Olivier Laviale <[email protected]>
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
9-
103
namespace olvlvl\ComposerAttributeCollector\Filter;
114

125
use Composer\IO\IOInterface;

src/MemoizeAttributeCollector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public function __construct(
4545
/**
4646
* @param array<class-string, string> $classMap
4747
* Where _key_ is a class and _value_ its pathname.
48-
*
49-
* @throws ReflectionException
5048
*/
5149
public function collectAttributes(array $classMap): TransientCollection
5250
{
@@ -95,7 +93,7 @@ public function collectAttributes(array $classMap): TransientCollection
9593
}
9694

9795
/**
98-
* Classes might have been removed, we need to filter according to the classes found.
96+
* Classes might have been removed, we need to filter entries according to the classes found.
9997
*/
10098
$this->state = array_filter(
10199
$this->state,

src/MemoizeClassMapFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function filter(array $classMap, Closure $filter): array
7070
}
7171

7272
/**
73-
* Paths might have been removed, we need to filter according to the paths found.
73+
* Paths might have been removed, we need to filter entries according to the paths found.
7474
*/
7575
$this->state = array_filter(
7676
$this->state,

0 commit comments

Comments
 (0)