Skip to content

Commit

Permalink
Add autoloader option
Browse files Browse the repository at this point in the history
  • Loading branch information
CamdenSegal committed Apr 20, 2015
1 parent d279793 commit ee51916
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
24 changes: 21 additions & 3 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ module.exports = yeoman.generators.Base.extend({
default: function( prompts ) {
return this._.underscored( prompts.name );
}.bind(this)
}, {
type: 'list',
name: 'autoloader',
message: 'Use Autoloader',
choices: ['Composer', 'Basic', 'None']
}];

this.prompt(prompts, function (props) {
Expand All @@ -110,6 +115,7 @@ module.exports = yeoman.generators.Base.extend({
this.classname = this._wpClassify( props.classname );
this.prefix = this._.underscored( props.prefix );
this.year = new Date().getFullYear();
this.autoloader = props.autoloader;

done();
}.bind(this));
Expand All @@ -133,10 +139,22 @@ module.exports = yeoman.generators.Base.extend({

configs: function() {
this.fs.copyTpl(
this.templatePath('*.json'),
this.destinationPath( '/'),
this.templatePath('bower.json'),
this.destinationPath('/bower.json'),
this
);
this.fs.copyTpl(
this.templatePath('package.json'),
this.destinationPath('/package.json'),
this
);
if ( this.autoloader == 'Composer' ) {
this.fs.copyTpl(
this.templatePath('composer.json'),
this.destinationPath('/composer.json'),
this
);
}
this.fs.copy(
this.templatePath('Gruntfile.js'),
this.destinationPath( '/Gruntfile.js')
Expand Down Expand Up @@ -201,7 +219,7 @@ module.exports = yeoman.generators.Base.extend({
skipInstall: this.options['skip-install']
});

if ( ! this.options['skip-install'] ) {
if ( this.autoloader == 'Composer' && ! this.options['skip-install'] ) {
this.spawnCommand('composer', ['install']);
}
}
Expand Down
37 changes: 36 additions & 1 deletion app/templates/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,32 @@
* Built using generator-plugin-wp
*/

<% if ( autoloader == 'Basic' ) { %>
/**
* Autoloads files with classes when needed
* @since 0.1.0
* @param string $class_name Name of the class being requested
*/
function <%= prefix %>_autoload_classes( $class_name ) {
if ( class_exists( $class_name, false ) || false === stripos( $class_name, '<%= classname %>_' ) ) {
return;
}
$filename = strtolower( str_ireplace(
array( '<%= classname %>_', '_' ),
array( '', '-' ),
$class_name
) );

<%= classname %>::include_file( $filename );
}
spl_autoload_register( '<%= prefix %>_autoload_classes' );
<% } else if ( autoloader == 'Composer' ) { %>
// User composer autoload.
require 'vendor/autoload_52.php';
<% } else { %>
// Include additional php files here
// require 'includes/admin.php';
<% } %>

/**
* Main initiation class
Expand Down Expand Up @@ -165,8 +189,19 @@ public function __get( $field ) {
default:
throw new Exception( 'Invalid '. __CLASS__ .' property: ' . $field );
}
}
}<% if ( autoloader == 'Basic' ) { %>

/**
* Include a file from the includes directory
* @since <%= version %>
* @param string $filename Name of the file to be included
*/
public static function include_file( $filename ) {
$file = self::dir( 'includes/'. $filename .'.php' );
if ( file_exists( $file ) ) {
return include_once( $file );
}
}<% } %>
}

// init our class
Expand Down

0 comments on commit ee51916

Please sign in to comment.