Skip to content

Commit

Permalink
Merge pull request #2 from alleyinteractive/test-exception
Browse files Browse the repository at this point in the history
Use relative paths when generating autoloader
  • Loading branch information
srtfisher authored Feb 16, 2022
2 parents edebc04 + 0a79827 commit f01074e
Show file tree
Hide file tree
Showing 22 changed files with 507 additions and 111 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.editorconfig export-ignore
9 changes: 5 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ jobs:

- name: Install test dependencies
run: |
cd tests/includes
cd tests/fixtures/root
composer install
- name: Composer Validate
run: composer validate --strict

- name: Execute tests
run: |
composer run lint
composer run test
run: composer run test
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ documented in this file.

## [Unreleased]

## 0.2.0

- Updates autoloader to use non-hard-coded paths.
- Adds support for dependencies to autoload files as well, fixes [#3](https://github.com/alleyinteractive/composer-wordpress-autoloader/issues/3).

## 0.1.0

- Initial release.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Coding
Standards](https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/)
using
[alleyinteractive/wordpress-autoloader](https://github.com/alleyinteractive/wordpress-autoloader).
Will load the autoloaded classes defined in your package and all autoloaded
classes in your dependencies.

## Installation

Expand Down Expand Up @@ -38,6 +40,28 @@ Once added a `vendor/wordpress-autoload.php` file will be created. You can load
that in place of `vendor/autoload.php` (it will load that for you) to load you
WordPress and Composer dependencies.

### Use Inside Packages Published to Packagist

Packages published to Packagist are required to be valid and have a
`composer.json` that passed a `composer validate`. Composer does not consider
`wordpress` to be a valid value inside of the `autoload` or `autoload-dev`
property. To allow packages to register autoloading in a valid format, you can
use the following format:

```json
{
"extra": {
"wordpress-autoloader": {
"autoload": {
"My_Plugin_Namespace\\": "src/",
},
"autoload-dev": {
"My_Plugin_Namespace\\Tests\\": "tests/",
}
}
}
}
```

### Automatically Injecting WordPress Autoloader

Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
"lint:fix": "@phpcbf",
"phpcbf": "phpcbf --standard=./phpcs.xml.dist .",
"phpcs": "phpcs --standard=./phpcs.xml.dist .",
"phpunit": "vendor/bin/phpunit",
"test": "@phpunit"
"phpunit": "phpunit",
"test": [
"@phpcs",
"@phpunit"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<file>.</file>
<exclude-pattern>*/.github/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>tests/includes/*</exclude-pattern>
<exclude-pattern>tests/fixtures/*</exclude-pattern>

<rule ref="PSR12">
<!-- Constant visibility can not be declared (yet) as the minimum supported PHP version is 5.3
Expand Down
8 changes: 4 additions & 4 deletions src/AutoloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class AutoloadFactory
* @param array<string, array<string>> $rules Array of rules.
* @return array<Autoloader>
*/
public static function generateFromRules(array $rules, string $baseDir)
public static function generateFromRules(array $rules)
{
$loaders = [];

foreach ($rules as $namespace => $paths) {
$loaders = array_merge(
$loaders,
array_map(
fn ($path) => Autoloader::generate($namespace, $baseDir . '/' . $path),
fn ($path) => new Autoloader($namespace, $path),
$paths,
),
);
Expand All @@ -35,9 +35,9 @@ public static function generateFromRules(array $rules, string $baseDir)
* @param array<string, string> $rules Array of rules.
* @return void
*/
public static function registerFromRules(array $rules, string $baseDir)
public static function registerFromRules(array $rules)
{
foreach (static::generateFromRules($rules, $baseDir) as $autoloader) {
foreach (static::generateFromRules($rules) as $autoloader) {
$autoloader->register();
}
}
Expand Down
Loading

0 comments on commit f01074e

Please sign in to comment.