Skip to content

Instantly share code, notes, and snippets.

@Muetze42
Last active April 10, 2022 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muetze42/31c407138bb6dd12f69c0f1f9098a82b to your computer and use it in GitHub Desktop.
Save Muetze42/31c407138bb6dd12f69c0f1f9098a82b to your computer and use it in GitHub Desktop.
Extend & set up (new) Laravel App
<?php
namespace App\Console\Commands\App;
use Illuminate\Console\Command;
class CleanupSoftDeletesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cleanup:soft-deletes';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete finally soft deletes Model instances';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$files = app_path('Models/*.php');
$days = config('this.app.soft-deletes-cleanup-hours', 8760);
foreach (glob($files) as $file) {
$class = 'App\\Models\\'.basename(str_replace('.php', '', $file));
$model = app($class);
if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) {
$this->line(__('Cleanup `:model` soft deletes', ['model' => class_basename($class)]));
$model::onlyTrashed()->where('deleted_at', '<=', now()->subHours($days))->forceDelete();
}
}
return 0;
}
}
<?php // To change maintraince view
namespace App\Console\Commands\App;
use Illuminate\Foundation\Console\DownCommand as Command;
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths;
class DownCommand extends Command
{
/**
* The default view for the maintenance mode
*
* @var string
*/
protected string $defaultMaintenanceView = 'errors::503';
/**
* Get the payload to be placed in the "down" file.
*
* @return array
*/
protected function getDownFilePayload(): array
{
return [
'except' => $this->excludedPaths(),
'redirect' => $this->redirectPath(),
'retry' => $this->getRetryTime(),
'refresh' => $this->option('refresh'),
'secret' => $this->option('secret'),
'status' => (int) $this->option('status', 503),
'template' => $this->prerenderView(),
];
}
/**
* Prerender the specified view so that it can be rendered even before loading Composer.
*
* @return string
*/
protected function prerenderView(): string
{
(new RegisterErrorViewPaths)();
return view(($this->option('render') ? $this->option('render') : $this->defaultMaintenanceView), [
'retryAfter' => $this->option('retry'),
])->render();
}
}
<?php // Add CommandNotFoundException::class
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
CommandNotFoundException::class,
];
<?php
namespace App\Console\Commands\Development;
use Illuminate\Console\Command;
class IdeHelperCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ide-helper';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update `Laravel IDE Helper`';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
if (config('app.env') === 'local') {
$this->call('ide-helper:models', [
'--write' => true,
]);
$this->call('ide-helper:generate');
$this->call('ide-helper:meta');
//$this->call('ide-helper:macros');
return 0;
}
$this->warn('The `ide-helper` is only for local development environment');
return 0;
}
}
<?php // Change loggin channel ['daily']
// ...
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('{{table}}', function (Blueprint $table) {
$table->id();
$table->foreignId('{{snake0}}_{{id1column}}')->constrained('{{table0}}', 'id')->onDelete('cascade');
$table->foreignId('{{snake1}}_{{id1column}}')->constrained('{{table1}}', 'id')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('{{table}}');
}
};
<?php
namespace App\Console\Commands\Development;
use Illuminate\Support\Str;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Composer;
class PivotMigrateMakeCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:migration:pivot
{model1 : The first Model}
{model2 : The second Model}
{--path= : The location where the migration file should be created}
{--filename= : The file name with which the migration should be created}
{--id1= : The id column name of the first model}
{--id2= : The id column name of the second model}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new migration file for pivot table';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Migration';
/**
* The Composer instance.
*
* @var Composer
*/
protected Composer $composer;
protected string $path;
protected string $filename;
protected string $table;
protected string $table0;
protected string $table1;
protected string $snake0;
protected string $snake1;
protected string $id1column;
protected string $id2column;
/**
* Execute the console command.
*
* @return int
* @throws FileNotFoundException
*/
public function handle(): int
{
$this->path = $this->option('path') ?? '';
$models = [
$this->argument('model1'),
$this->argument('model2'),
];
sort($models);
$this->table0 = Str::snake(Str::pluralStudly(class_basename($models[0])));
$this->table1 = Str::snake(Str::pluralStudly(class_basename($models[1])));
$this->snake0 = Str::snake($models[0]);
$this->snake1 = Str::snake($models[1]);
$this->table = $this->snake0.'_'.$this->snake1;
$this->filename = $this->option('filename') ?? now()->format('Y_m_d_His').'_create_'.$this->table.'_pivot_table';
$this->id1column = $this->option('id1') ?? 'id';
$this->id2column = $this->option('id2') ?? 'id';
$path = $this->getFullPath();
$this->files->put($path, $this->sortImports($this->buildClass()));
$this->info($this->type.' created successfully.');
$this->composer = new Composer($this->files);
$this->composer->dumpAutoloads();
return 0;
}
/**
* Get the full path include filename
*
* @return string
*/
protected function getFullPath(): string
{
$file = str_ends_with($this->filename, '.php') ? $this->filename : $this->filename.'.php';
return $this->getMigrationPath().'/'.$file;
}
/**
* Build the class with the given name.
*
* @param string $name
* @return string
*
* @throws FileNotFoundException
*/
protected function buildClass($name = ''): string
{
$stub = $this->files->get($this->getStub());
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}
/**
* Replace the namespace for the given stub.
*
* @param string $stub
* @param string $name
* @return $this
*/
protected function replaceNamespace(&$stub, $name): PivotMigrateMakeCommand
{
$replaces = [
'{{table}}' => $this->table,
'{{snake0}}' => $this->snake0,
'{{snake1}}' => $this->snake1,
'{{table0}}' => $this->table0,
'{{table1}}' => $this->table1,
'{{id1column}}' => $this->id1column,
'{{id2column}}' => $this->id2column,
];
$stub = str_replace(array_keys($replaces), array_values($replaces), $stub);
return $this;
}
/**
* Get the migration stub
*
* @return string
*/
protected function getStub(): string
{
$file = base_path('stubs/migration.pivot.stub');
if (file_exists($file)) {
return $file;
}
exit(__('Stub not „:file“ not exist', ['file' => $file]));
}
/**
* Get migration path (either specified by '--path' option or default location).
*
* @return string
*/
protected function getMigrationPath(): string
{
return $this->path ?: $this->laravel->basePath().'/database/migrations';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment