PHP Classes

PHP Artisan Make File Location: Change the namespace of files generated by artisan

Recommend this page to a friend!
  Info   View files Documentation   View files View files (28)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 52 This week: 1All time: 10,610 This week: 560Up
Version License PHP version Categories
artisan-make-file-lo 1.0MIT/X Consortium ...7Tools, Libraries, Console, Code Gener..., P...
Description 

Author

This package can change the namespace of files generated by artisan.

It comes with a service provider for the Artisan tool that has command line options that allow to configure prefix and suffix of the namespace used in the PHP files generated by Artisan.

The command line parameters passed to the Artisan tool need to take the name of the provider supplied by this package, so those options can be enabled.

Innovation Award
PHP Programming Innovation award nominee
June 2018
Number 2


Prize: 30 Days Free Access to O'Reilly Safari Learning Platform
Artisan is a tool that comes with the Laravel framework that often is used to generate all sorts of project files.

Artisan can be extended with plugin packages also written in PHP provide additional functionality to this package.

This package provides additional options to configure the namespace of classes generated using Artisan.

Manuel Lemos
Picture of Diego R. Lima
  Performance   Level  
Name: Diego R. Lima <contact>
Classes: 1 package by
Country: Brazil Brazil
Age: 28
All time rank: 4486365 in Brazil Brazil
Week rank: 416 Up36 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 1x

Documentation

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.0.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.

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


  Files folder image Files  
File Role Description
Files folder imageconfig (1 file)
Files folder imagesrc (4 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  config  
File Role Description
  Accessible without login Plain text file amfl.php Aux. Auxiliary script

  Files folder image Files  /  src  
File Role Description
Files folder imageCommands (20 files)
  Plain text file ArtisanServiceProvider.php Class Class source
  Plain text file CommandSetup.php Class Class source
  Plain text file CommandsList.php Class Class source
  Plain text file TraitCommand.php Class Class source

  Files folder image Files  /  src  /  Commands  
File Role Description
  Plain text file ChannelMakeCommand.php Class Class source
  Plain text file ConsoleMakeCommand.php Class Class source
  Plain text file ControllerMakeCommand.php Class Class source
  Plain text file EventMakeCommand.php Class Class source
  Plain text file ExceptionMakeCommand.php Class Class source
  Plain text file FactoryMakeCommand.php Class Class source
  Plain text file JobMakeCommand.php Class Class source
  Plain text file ListenerMakeCommand.php Class Class source
  Plain text file MailMakeCommand.php Class Class source
  Plain text file MiddlewareMakeCommand.php Class Class source
  Plain text file MigrateMakeCommand.php Class Class source
  Plain text file ModelMakeCommand.php Class Class source
  Plain text file NotificationMakeCommand.php Class Class source
  Plain text file PolicyMakeCommand.php Class Class source
  Plain text file ProviderMakeCommand.php Class Class source
  Plain text file RequestMakeCommand.php Class Class source
  Plain text file ResourceMakeCommand.php Class Class source
  Plain text file RuleMakeCommand.php Class Class source
  Plain text file SeederMakeCommand.php Class Class source
  Plain text file TestMakeCommand.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:52
This week:1
All time:10,610
This week:560Up