Skip to content

Commit

Permalink
Add initial files.
Browse files Browse the repository at this point in the history
  • Loading branch information
justintadlock committed Jun 13, 2019
0 parents commit ae4f7aa
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
@@ -0,0 +1,8 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
14 changes: 14 additions & 0 deletions .gitattributes
@@ -0,0 +1,14 @@
# Exclude from release archives.

/.editorconfig export-ignore
/.git export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/changelog.md export-ignore
/composer.lock export-ignore
/contributing.md export-ignore

# Handle line endings.
# See https://help.github.com/articles/dealing-with-line-endings/

* text eol=lf
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
composer.lock
/vendor
35 changes: 35 additions & 0 deletions readme.md
@@ -0,0 +1,35 @@
# WPTRT Autoload

A PSR-4 autoloader for WordPress themes. Primarily, this repository exists for theme authors who want to use autoloading but aren't yet on something such as Composer.

Any classes loaded via this autoloader must follow the [PSR-4: Autoloading](https://www.php-fig.org/psr/psr-4/) standard for naming their namespaces, classes, and directories.

## Usage

Here's a real-world example of loading the [WPTRT Customize Pro](https://github.com/WPTRT/wptrt-customize-pro) package:

```php
// Include the Loader class.
require_once( get_theme_file_path( 'path/to/wptrt-autoload/src/Loader.php' ) );

// Create a new instance of the Loader class.
$themeslug_loader = new \WPTRT\Autoload\Loader();

// Add (one or multiple) namespaces and their paths.
$themeslug_loader->add( 'WPTRT\\CustomizePro\\', get_theme_file_path( 'path/to/wptrt-customize-pro/src' ) );

// Register all loaders.
$themeslug_loader->register();
```

### Loader::add() method

Primarily, theme authors would utilize the `add()` method to add a loader. You can call `add()` multiple times to register multiple loaders.

```php
$themeslug_loader->add( $prefix, $path, $prepend = false );
```

* `$prefix` - This should be the namespace of the project. Make sure to escape backslashes like `\\` instead of a single `\`.
* `$path` - This should be the absolute path to the source code of where the classes are housed.
* `$prepend` - Whether to prepend or append a particular loader to the autoload queue. `false` by default.
120 changes: 120 additions & 0 deletions src/Loader.php
@@ -0,0 +1,120 @@
<?php
/**
* Autoloader Class.
*
* A basic PSR-4 autoloader for theme developers.
*
* @author WPTRT <themes@wordpress.org>
* @copyright 2019 WPTRT
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0-or-later
* @link https://github.com/WPTRT/wptrt-autoload
*/

namespace WPTRT\Autoload;

class Loader {

/**
* Array of loaders.
*
* @since 1.0.0
* @access protected
* @var array
*/
protected $loaders = [];

/**
* Adds a new prefix and path to load.
*
* @since 1.0.0
* @access public
* @param string $prefix Namespace prefix.
* @param string $path Absolute path where to look for classes.
* @param bool $prepend Whether to prepend the autoloader to the queue.
* @return void
*/
public function add( $prefix, $path, $prepend = false ) {

$this->loaders[ $prefix ] = [
'prefix' => $prefix,
'path' => $path,
'prepend' => $prepend
];
}

/**
* Removes a loader by prefix.
*
* @since 1.0.0
* @access public
* @param string $prefix Namespace prefix.
* @return void
*/
public function remove( $prefix ) {

if ( $this->has( $prefix ) ) {
unset( $this->loaders[ $prefix ] );
}
}

/**
* Checks if a loader is already added.
*
* @since 1.0.0
* @access public
* @param string $prefix Namespace prefix.
* @return bool
*/
public function has( $prefix ) {
return isset( $this->loaders[ $prefix ] );
}

/**
* Registers all loaders.
*
* @since 1.0.0
* @access public
* @return void
*/
public function register() {

foreach ( $this->loaders as $loader ) {

spl_autoload_register( function( $class ) use ( $loader ) {

$this->load( $class, $loader['prefix'], $loader['path'] );

}, true, $loader->prepend );
}
}

/**
* Loads a class if it's within the given namespace.
*
* @since 1.0.0
* @access public
* @param string $class Fully-qualified class name.
* @param string $prefix Namespace prefix.
* @param string $path Absolute path where to look for classes.
* @return void
*/
protected function load( $class, $prefix, $path ) {

// Bail if the class is not in our namespace.
if ( 0 !== strpos( $class, $prefix ) ) {
return;
}

// Remove the prefix from the class name.
$class = str_replace( $prefix, '', $class );

// Build the filename.
$file = realpath( $path );
$file = $file . DIRECTORY_SEPARATOR . str_replace( '\\', DIRECTORY_SEPARATOR, $class ) . '.php';

// If the file exists for the class name, load it.
if ( file_exists( $file ) ) {
include( $file );
}
}
}

0 comments on commit ae4f7aa

Please sign in to comment.