Code Monkey home page Code Monkey logo

wp-namespace-autoloader's Introduction

wp-namespace-autoloader

A PHP autoloader class that follows the WordPress coding standards 2.0 applying PSR-4 specification and optionally supports proposed 3.0

Description

Namespaces and autoloaders are cool and help organizing your code. With these features you don't have to worry about including and requiring php files manually ever again and your code gets organized in folders.

This is a PSR-4 autoloader implementation following WordPress naming conventions 2.0 and proposed WordPress naming conventions 3.0

It means I'm doing these things:

  • Converting classes filenames to lowercase
  • Replacing underscores on class filenames by hyphens
  • Prepending 'class-' before the final class name
  • [Optional]: Prepending 'interface-' before the final interface name
  • [Optional]: Prepending 'trait-' before the final trait name

Note

  • Required PHP Version is PHP 5.4

Installation

You just have to require it just like a composer default dependency. You may have to use preferred-install as dist so you will be able to commit files as .git files will not be created

"require": {	
	"pablo-sg-pacheco/wp-namespace-autoloader": "dev-master"
},
"config": {
	"preferred-install": "dist"
}

Usage

Firstly, load the composer dependencies like you are used to

<?php
require __DIR__ . '/vendor/autoload.php';

Now you have to initialize it and you are good to go

<?php
use Pablo_Pacheco\WP_Namespace_Autoloader\WP_Namespace_Autoloader;
$autoloader = new WP_Namespace_Autoloader( array(    
	'directory'          => __DIR__,       // Directory of your project. It can be your theme or plugin. Defaults to __DIR__ (probably your best bet). 	
	'namespace_prefix'   => 'My_Project',  // Main namespace of your project. E.g My_Project\Admin\Tests should be My_Project. Defaults to the namespace of the instantiating file.	
	'classes_dir'        => 'src',         // (optional). It is where your namespaced classes are located inside your project. If your classes are in the root level, leave this empty. If they are located on 'src' folder, write 'src' here 
	'prepend_class'      => true,          // (optional). Default true, prepends class- before the final class name 
	'prepend_interface'  => true,          // (optional). Default false, prepends interface- before the final interface name 
	'prepend_trait'      => true,          // (optional). Default false, prepends trait- before the final trait name 
) );
$autoloader->init();

Now comes the cool part! If you have a simple class located on your_projct_root_folder\Admin_Pages\class-main-page.php like this, you can instantiate it and it's going to work

<?php
namespace My_Project\Admin_Pages;
class Main_Page{
}

Or if you have a simple interface located on your_projct_root_folder\Admin_Pages\interface-init.php

<?php
namespace My_Project\Admin_Pages;
interface Init {
}

And you have a simple class implementing that interface located on your_projct_root_folder\Admin_Pages\class-main-page.php you can instantiate it and it's going to work

<?php
namespace My_Project\Admin_Pages;
class Main_Page implements Init {
}

Or if you have a simple trait located on your_projct_root_folder\Admin_Pages\trait-my-trait.php

<?php
namespace My_Project\Admin_Pages;
trait My_Trait {
}

And you have a simple class using that trait located on your_projct_root_folder\Admin_Pages\class-main-page.php you can instantiate it and it's going to work

<?php
namespace My_Project\Admin_Pages;
class Main_Page {
    use My_Trait
}

Parameters

Parameter Default value Description
directory null Path of your project. Probably use __DIR__ here
namespace_prefix null Namespace prefix of your project
classes_dir array( '.', 'vendor' ) Relative path of the directory containing all your classes. Accepts string or array of strings. Defaults to directory parameter and the vendor subdirectory. (optional).
lowercase array('file') If you want to lowercase just the file or folders too. It accepts an array with two possible values: 'file', 'folders'.
underscore_to_hyphen array('file') If you want to convert underscores to hyphens. It accepts an array with two possible values: 'file', 'folders'.
prepend_class true If you want to prepend 'class-' before files
prepend_interface false If you want to prepend 'interface-' before files
prepend_trait false If you want to prepend 'trait-' before files

This class has parameters that make it flexible enough to fit any kind of project.

Examples

  • Lowercases all folders using lowercase => array('file','folders')
  • Converts underscores to hyphens on folders too with underscore_to_hyphen => array('file','folders')
  • Doesn't prepend class before file with prepend_class => false

wp-namespace-autoloader's People

Contributors

brianhenryie avatar hemmesdev avatar pablo-sg-pacheco avatar szepeviktor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

wp-namespace-autoloader's Issues

Can we arrange our classes in multiple directories?

In the classes_dir parameter, we have to specify the directory where our classes are located which we want to use for autoloading. For example, you said that if the classes are in the 'src' directory we have to specify 'src', and if the classes are in the root directory, nothing needs to be set.

My question is if we have split our classes into different directories, for example:
In root_dirctory/admin/includes (it holds admin classes) and root_directory/public/includes (it holds public classes)

In the above scenario, what do we have to specify in the classes_dir parameter? Should we leave it empty or should we write the name of both directories that holds our classes like:

'namespace_prefix' => 'My_Project',
'classes_dir' => [ 'admin', 'public' ]

Looking forward to your instructions on it.

PHPUnit Code Coverage

I tried this on a project but my PHPUnit code coverage began reporting 0% code coverage. Once I used require_once for each class, it began working again. Is there a sample project I can follow so I know I have it configured correctly.

A little request

Hi, first of all thank you for this class.

I need to know if you can use it to manage multiple directories.

In practice a coding similar to the following:

includes/my_classes and admin/includes/my__other_classes and public/includes/my_other_classes

and so on

Thanks in advance
Adriano

Please tag releases

Currently Composer errors out

Could not find a version of package pablo-sg-pacheco/wp-namespace-autoloader matching your minimum-stability (stable).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.