CastMakeCommand.php
TLDR
This file contains the CastMakeCommand
class, which is a command to generate a new custom Eloquent cast class. It extends the GeneratorCommand
class and is used to create cast classes for Eloquent models.
Classes
CastMakeCommand
The CastMakeCommand
class is a command that generates a new custom Eloquent cast class. It extends the GeneratorCommand
class, which is a base class for generating various types of files in Laravel. The CastMakeCommand
class is used to create cast classes for Eloquent models.
The class has the following properties:
-
$name
: The console command name, which is set to'make:cast'
. -
$description
: The console command description, which is set to'Create a new custom Eloquent cast class'
. -
$type
: The type of class being generated, which is set to'Cast'
.
The class has the following methods:
-
getStub()
: Returns the stub file for the generator. If the--inbound
option is provided, it returns the path to the'cast.inbound.stub'
stub file; otherwise, it returns the path to the'cast.stub'
stub file. -
resolveStubPath($stub)
: Resolves the fully-qualified path to the stub file. If a custom path exists for the specified stub, it returns the custom path; otherwise, it returns the path to the stub file within the current directory. -
getDefaultNamespace($rootNamespace)
: Returns the default namespace for the generated class. It appends'\Casts'
to the root namespace. -
getOptions()
: Returns the console command options. It defines the following options:-
'force'
: A boolean option (-f
or--force
) to create the class even if the cast already exists. -
'inbound'
: A boolean option (--inbound
) to generate an inbound cast class.
-
<?php
namespace Illuminate\Foundation\Console;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
#[AsCommand(name: 'make:cast')]
class CastMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:cast';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new custom Eloquent cast class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Cast';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->option('inbound')
? $this->resolveStubPath('/stubs/cast.inbound.stub')
: $this->resolveStubPath('/stubs/cast.stub');
}
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Casts';
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getOptions()
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the cast already exists'],
['inbound', null, InputOption::VALUE_NONE, 'Generate an inbound cast class'],
];
}
}