September, 04 2020
Recently, I released Extended Artisan Commands, a mini package for generating plain PHP classes, traits and interfaces. This package is useful for generating action classes, services, repositories, contracts, traits e.t.c.
In this tutorial, I will walk you through how this package works and how you can use it during development.
This package is solely for development so its highly recommended to install it as a dev package.
composer require stephenjude/extended-artisan-commands --dev
In this package we have two ways of generating classes. The first is the make:class
and the other is make:abstract-class
. Let's take a closer look at these two commands.
Make Class Command
The make:class
command generates plan PHP class. For example, lets generate an email forwarder service class.
php artisan make:class Services/EmailForwarderService
This will generate an EmailForwarderService
class under the App/Services
namespace.
// app/Services/EmailForwarderService.php
<?php
namespace App\Services;
class EmailForwarderService
{
}
Make Abstract Class Command
The make:abstract-class
command generates an abstract PHP class. For example, lets generate an abstract email forwarder class. inside our app Services folder.
php artisan make:abstract-class Services/AbstractEmailForwarder
This will generate an AbstractEmailForwarder
PHP abstract class under the App/Services
namespace.
// app/Services/AbstractEmailForwarder.php
<?php
namespace App\Services;
abstract class AbstractEmailForwarder
{
}
You can generate PHP interfaces which is referred to as contracts in the Laravel community. The default namespace for interfaces is App/Contracts
but you can change this in the package config file.
Lets create an email forwarder contract(interface):
php artisan make:interface EmailForwarder
This will generate an EmailForwarder
PHP interface under the App/Contracts
namespace.
// app/Contracts/EmailForwarder.php
<?php
namespace App\Contracts;
interface EmailForwarder{
}
This package generates PHP traits under the App/Traits
namespace. You can also change this in the package config file.
Let's generate a file upload trait.
php artisan make:trait FileUpload
This will generate a FileUpload PHP trait under the App/Traits
namespace.
// app/Traits/FileUpload.php
<?php
namespace App\Traits;
trait FileUpload
{
}
This package provides two kind of options. The first is the --force
option which applies to all the commands provided in this package. The --force
option is used to generate a class and replace any existing file present.
Force example, assuming we are generating a FileUpload
trait and we want to replace already existing FileUpload
trait, we will pass the --force
option to the make:trait
command like this:
php artisan make:trait FileUpload --force
Make Class Command Options
The make:class
command has for options that can be applied to it. Let me walk you through them.
--interface
OR -i
This options tells the package to also generate an interface while generating our new class.
--trait
OR -t
This will generate a trait.
--abstract
OR -c
This will generate an abstract class.
--all
OR -a
This will generate an interface, a trait and an abstract class while generating our new class.
The package comes with configuration that sets default namespaces for the files being generated. You can publish and make changes to the config file so that it suits your project.
php artisan vendor:publish --provider="Stephenjude\ExtendedArtisanCommands\ExtendedArtisanCommandsServiceProvider" --tag="config"
Here is the default configuration file.
return [
/*
|--------------------------------------------------------------------------
| Default Class Namespace
|--------------------------------------------------------------------------
|
| Here you can configure the default namespace for
| the make:class command.
|
*/
'class_namespace' => '',
/*
|--------------------------------------------------------------------------
| Default Abstract Class Namespace
|--------------------------------------------------------------------------
|
| Here you can configure the default namespace for
| the make:abstract-class command.
|
*/
'abstract_class_namespace' => '',
/*
|--------------------------------------------------------------------------
| Default Interface Namespace
|--------------------------------------------------------------------------
|
| Here you can configure the default namespace for
| the make:interface command.
|
*/
'interface_namespace' => '\Contracts',
/*
|--------------------------------------------------------------------------
| Default Trait Namespace
|--------------------------------------------------------------------------
|
| Here you can configure the default namespace for
| the make:trait command.
|
*/
'trait_namespace' => '\Traits',
];
I made a video demo on how this package works. You can check it out here.
I hope you enjoy the goodies in this package. You can find this package here on GitHub and don't forget to give it a star, it means a lot to me!
Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here
Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now