diego-rlima / artisan-make-file-location

Ability to change the namespace/location of the files generated by the "artisan make" commands.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

diego-rlima/artisan-make-file-location

Ability to change the namespace/location of the files generated by the "artisan make" commands.

Requirements

This package requires Laravel 5.4 or later and PHP 7.1.0 or later.

Installation

$ composer require diego-rlima/artisan-make-file-location

For Laravel 5.4, you must register the service provider of this package. Add the code below in the providers section of your config/app.php file.

DRL\AMFL\ArtisanServiceProvider::class,

Using

You can use all "artisan make" commands as usual. But now, you can add the options --prefix and --suffix to change the namespace of your files.

Note: For files that do not have namespace (like migrations), only the prefix can be used.

Prefix

$ php artisan make:controller ProductController --prefix=Units\\Products\\Controllers

This will output the file with the namespace App\Units\Products\Controllers.

Suffix

$ php artisan make:controller ProductController --suffix=Products

This will output the file with the namespace App\Http\Controllers\Products.

Both Prefix and Suffix

$ php artisan make:controller ProductController --prefix=Units --suffix=Products

This will output the file with the namespace App\Units\Controllers\Products.

Customizing

The package is configured to be compatible with the Laravel standard, but allowing you to set prefixes and suffixes. However, you can replace the Laravel pattern with your own.

Publish the config using the following command:

$ php artisan vendor:publish --provider="DRL\AMFL\ArtisanServiceProvider"

Now you have a config/amfl.php file. The settings are divided between files that have namespaces and files that they do not have.

return [
    /*
    |--------------------------------------------------------------------------
    | Files namespaces
    |--------------------------------------------------------------------------
    */
    // List of all files with namespace. Eg.:
    'controller' => '{root}\{prefix|default:Http}\Controllers\{suffix}',
    'test' => '{root}\{prefix}\{type}\{suffix}',

    /*
    |--------------------------------------------------------------------------
    | Files locations
    |--------------------------------------------------------------------------
    */
    // List of all files without namespace. Eg.:
    'seeder' => '{root}/{prefix}/seeds/{name}.php',
];

For files with namespace, the {root} normally will be replaced by the "App" namespace. Of curse, {prefix} and {suffix} will be replaced by the prefix and suffix you choose.

In the test files, {root} will be changed to "Tests" namespace. These files also have namespace variation. The {type} will be changed to "Unit" or "Feature".

For files without namespace, the {root} will be replaced by the root directory of application. The {prefix} and {name} will be replaced by the prefix and the file name, respectively.

Important: If you are going to create the migrations in another folder, make sure the folder is already created, or the migration will not be created. This is due to the way the original command was written.

Making prefixes/suffixes required

All you have to do is add |required to the file configuration. Eg.:

return [
    // Now you will always have to enter a prefix when creating a Model.
    'model' => '{root}\{prefix|required}',

    // The same for the notification suffix.
    'notification' => '{root}\{prefix}\Notifications\{suffix|required}',
];

Defining a default value for prefixes/suffixes

Just add |default:YouDefaultValue to the file configuration. Eg.:

return [
    // Now, if you do not set a prefix, the default value will be used.
    'model' => '{root}\{prefix|default:Models}',

    // The same for the suffix.
    'rule' => '{root}\{prefix}\Rules\{suffix|default:Admin}',
];

Note: Default values will not be applied if prefix/suffix is required.

Extending other commands

Do you want to use prefixes / suffixes in your commands or third-party commands? Just do it!

Let's assume that your command class looks like the following:

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class FooBarMakeCommand extends GeneratorCommand
{
    protected $name = 'make:foobar';

    protected $description = 'Create a new bar';

    protected $type = 'FooBar';

    protected function getStub()
    {
        return __DIR__ . '/stubs/bar.stub';
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Foo\Bar';
    }
}

You need to change the class so that it can retrieve the pattern we set. It will be similar to:

use DRL\AMFL\TraitCommand;

class FooBarMakeCommand extends GeneratorCommand
{
    // You will use trait.
    use TraitCommand;

    // You will use the amflCustomNamespace() method to retrieve the correct namespace and return it in the getDefaultNamespace() method.
    protected function getDefaultNamespace($rootNamespace)
    {
        return $this->amflCustomNamespace($rootNamespace);
    }

    // You will create the amflInit() method and load the correct settings.
    protected function amflInit()
    {
        $this->amflCommandSetup('foobar'); // "foobar" must be the configuration key within your "config/amfl.php" file;
    }

    // Code omitted
}

If your command will generate a file without namespace, the code must change a bit. It should look like:

use DRL\AMFL\TraitCommand;

class FooBarMakeCommand extends GeneratorCommand
{
    use TraitCommand;

    // You will use the amflCustomPath() method to retrieve the correct path and return it in the getPath() method.
    protected function getPath($name)
    {
        $rootPath = $this->laravel->basePath();

        return return $this->amflCustomPath($rootPath, $name);
    }

    protected function amflInit()
    {
        $this->amflCommandSetup('foobar');
    }
}

In your AppServiceProvider, extend the list of commands.

use DRL\AMFL\CommandsList;
use App\Console\Commands\FooBarMakeCommand;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // The first argument must be the name of the command. The second is a function that receives the $app variable and returns an instance of the command class.
        CommandsList::extend('command.foobar.make', function ($app) {
            return new FooBarMakeCommand($app['files']);
        });
    }
}

Put the pattern configuration of the command inside your "config/amfl.php" file.

return [
    // For files with namespace.
    'foobar' => '{root}\{prefix}\Foo\{suffix}',

    // For files without namespace.
    'foobar' => '{root}/{prefix}/Foo/{name}.php',
];

List of commands supported

Command Supports prefix Supports suffix Min. Laravel Version
make:channel yes yes 5.6
make:command yes yes 5.4
make:controller yes yes 5.4
make:event yes yes 5.4
make:exception yes yes 5.6
make:factory yes no 5.6
make:job yes yes 5.4
make:listener yes yes 5.4
make:mail yes yes 5.4
make:middleware yes yes 5.4
make:migration yes no 5.4
make:model yes no 5.4
make:notification yes yes 5.4
make:policy yes yes 5.4
make:provider yes yes 5.4
make:request yes yes 5.4
make:resource yes yes 5.6
make:rule yes yes 5.6
make:seeder yes no 5.4
make:test yes yes 5.4

About

Ability to change the namespace/location of the files generated by the "artisan make" commands.

License:MIT License


Languages

Language:PHP 100.0%