Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add make command for custom exceptions #21483

Merged
merged 2 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/Illuminate/Foundation/Console/ExceptionMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class ExceptionMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:exception';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new custom exception class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Exception';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($this->option('render')) {
return $this->option('report')
? __DIR__.'/stubs/exception-render-report.stub'
: __DIR__.'/stubs/exception-render.stub';
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of this else, because it'll return early here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right, I was following the code in other MakeCommands. I'll fix it as soon as possible.

Thank you.

return $this->option('report')
? __DIR__.'/stubs/exception-report.stub'
: __DIR__.'/stubs/exception.stub';
}
}

/**
* Determine if the class already exists.
*
* @param string $rawName
* @return bool
*/
protected function alreadyExists($rawName)
{
return class_exists($this->rootNamespace().'Exceptions\\'.$rawName);
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Exceptions';
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['render', null, InputOption::VALUE_NONE, 'Create the exception with an empty render method.'],
['report', null, InputOption::VALUE_NONE, 'Create the exception with an empty report method.'],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace DummyNamespace;

use Exception;

class DummyClass extends Exception
{

/**
* Render the exception into an HTTP response.
*
* @param \Illuminate\Http\Request
* @return \Illuminate\Http\Response
*/
public function render($request)
{
//
}

/**
* Report the exception.
*
* @return void
*/
public function report()
{
//
}
}
19 changes: 19 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/exception-render.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace DummyNamespace;

use Exception;

class DummyClass extends Exception
{

/**
* Render an exception into a response.
*
* @param \Illuminate\Http\Request $request
*/
public function render($request)
{
//
}
}
19 changes: 19 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/exception-report.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace DummyNamespace;

use Exception;

class DummyClass extends Exception
{

/**
* Report the exception.
*
* @return void
*/
public function report()
{
//
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/exception.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace DummyNamespace;

use Exception;

class DummyClass extends Exception
{

}
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Illuminate\Foundation\Console\ResourceMakeCommand;
use Illuminate\Foundation\Console\ClearCompiledCommand;
use Illuminate\Foundation\Console\EventGenerateCommand;
use Illuminate\Foundation\Console\ExceptionMakeCommand;
use Illuminate\Foundation\Console\VendorPublishCommand;
use Illuminate\Console\Scheduling\ScheduleFinishCommand;
use Illuminate\Database\Console\Seeds\SeederMakeCommand;
Expand Down Expand Up @@ -132,6 +133,7 @@ class ArtisanServiceProvider extends ServiceProvider
'ControllerMake' => 'command.controller.make',
'EventGenerate' => 'command.event.generate',
'EventMake' => 'command.event.make',
'ExceptionMake' => 'command.exception.make',
'FactoryMake' => 'command.factory.make',
'JobMake' => 'command.job.make',
'ListenerMake' => 'command.listener.make',
Expand Down Expand Up @@ -374,6 +376,18 @@ protected function registerEnvironmentCommand()
});
}

/**
* Register the command.
*
* @return void
*/
protected function registerExceptionMakeCommand()
{
$this->app->singleton('command.exception.make', function ($app) {
return new ExceptionMakeCommand($app['files']);
});
}

/**
* Register the command.
*
Expand Down