Skip to content

Commit

Permalink
Initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Feb 2, 2022
0 parents commit 48ef676
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.php]
ident_size = 4

[*.md]
ident_size = 2
trim_trailing_whitespace = false

[*.json]
ident_size = 2

[{.gitignore,.gitkeep,.editorconfig}]
ident_size = 2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor/
composer.lock
.phpcs.xml
phpcs.xml
.vscode
.phpunit.result.cache
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

All notable changes to `alleyinteractive/composer-wordpress-autoloader` will be
documented in this file.

## [Unreleased]

## 0.1.0

- Initial release.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Composer WordPress Autoloader

[![Latest Version on Packagist](https://img.shields.io/packagist/v/alleyinteractive/composer-wordpress-autoloader.svg?style=flat-square)](https://packagist.org/packages/alleyinteractive/composer-wordpress-autoloader)
[![Tests](https://github.com/alleyinteractive/composer-wordpress-autoloader/actions/workflows/tests.yml/badge.svg)](https://github.com/alleyinteractive/composer-wordpress-autoloader/actions/workflows/tests.yml)

Autoload WordPress files configured via Composer that support the [Wordpress
Coding
Standards](https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/)
using
[alleyinteractive/wordpress-autoloader](https://github.com/alleyinteractive/wordpress-autoloader).

## Installation

You can install the package via composer:

```bash
composer require alleyinteractive/composer-wordpress-autoloader
```

## Usage

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

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.


### Automatically Injecting WordPress Autoloader

Composer WordPress Autoloader can also automatically inject the WordPress
autoloader into your `vendor/autoload.php` file you're already loading. This is
disabled by default.

```json
{
"extra": {
"wordpress-autoloader": {
"inject": true
}
}
}
```

Once enabled, you can load `vendor/autoload.php` like you do normally and have
your WordPress files automatically loaded.

## Testing

```bash
composer test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "alleyinteractive/composer-wordpress-autoloader",
"type": "composer-plugin",
"description": "Autoload files using WordPress File Conventions",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Alley Interactive",
"email": "[email protected]"
},
{
"name": "Sean Fisher",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4",
"alleyinteractive/wordpress-autoloader": "^0.1",
"composer-plugin-api": "^2.0",
"squizlabs/php_codesniffer": "^2.0 || ^3.0 || ^4.0"
},
"require-dev": {
"composer/composer": "^2.0",
"phpunit/phpunit": "^8.5.8 || ^9.3.3"
},
"extra": {
"class": "ComposerWordPressAutoloader\\Plugin"
},
"autoload": {
"psr-4": {
"ComposerWordPressAutoloader\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"lint": "@phpcs",
"lint:fix": "@phpcbf",
"phpcbf": "phpcbf --standard=./phpcs.xml.dist .",
"phpcs": "phpcs --standard=./phpcs.xml.dist .",
"phpunit": "vendor/bin/phpunit",
"test": "@phpunit"
}
}
21 changes: 21 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="phpcodesniffer-composer-installer">
<description>Coding standards for Composer WordPress Autoload Plugin</description>

<arg name="extensions" value="php" />
<!-- Show sniff codes in all reports, and progress when running -->
<arg value="sp" />
<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="." />

<file>.</file>
<exclude-pattern>*/.github/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>tests/includes/*</exclude-pattern>

<rule ref="PSR12">
<!-- Constant visibility can not be declared (yet) as the minimum supported PHP version is 5.3
and constant visibility was only introduced in PHP 7.1. -->
<exclude name="PSR12.Properties.ConstantVisibility.NotFound" />
</rule>
</ruleset>
15 changes: 15 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<phpunit
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="alleyinteractive/composer-wordpress-autoload">
<file>tests/AutoloaderTest.php</file>
<!-- <directory>tests</directory> -->
</testsuite>
</testsuites>
</phpunit>
44 changes: 44 additions & 0 deletions src/AutoloadFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace ComposerWordPressAutoloader;

use Alley_Interactive\Autoloader\Autoloader;

class AutoloadFactory
{
/**
* Generate an autoloader from a set of rules.
*
* @param array<string, array<string>> $rules Array of rules.
* @return array<Autoloader>
*/
public static function generateFromRules(array $rules, string $baseDir)
{
$loaders = [];

foreach ($rules as $namespace => $paths) {
$loaders = array_merge(
$loaders,
array_map(
fn ($path) => Autoloader::generate($namespace, $baseDir . '/' . $path),
$paths,
),
);
}

return $loaders;
}

/**
* Register autoloaders from rules.
*
* @param array<string, string> $rules Array of rules.
* @return void
*/
public static function registerFromRules(array $rules, string $baseDir)
{
foreach (static::generateFromRules($rules, $baseDir) as $autoloader) {
$autoloader->register();
}
}
}
31 changes: 31 additions & 0 deletions src/AutoloadGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ComposerWordPressAutoloader;

use Composer\Autoload\AutoloadGenerator as ComposerAutoloadGenerator;
use Composer\Package\PackageInterface;

/**
* Composer Autoload Generator
*/
class AutoloadGenerator extends ComposerAutoloadGenerator
{
/**
* Compiles an ordered list of namespace => path mappings
*
* @param array $packageMap
* @param PackageInterface $rootPackage
* @param boolean $filteredDevPackages
* @return array
*/
public function parseAutoloads(array $packageMap, PackageInterface $rootPackage, $filteredDevPackages = false)
{
if ($filteredDevPackages) {
$packageMap = $this->filterPackageMap($packageMap, $rootPackage);
}

return [
'wordpress' => $this->parseAutoloadsType($packageMap, 'wordpress', $rootPackage),
];
}
}
Loading

0 comments on commit 48ef676

Please sign in to comment.