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

General readability/extendability improvements #157

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions src/Actions/GenerateMigrationName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Spatie\LaravelPackageTools\Actions;

use Carbon\Carbon;
use Illuminate\Support\Str;

class GenerateMigrationName
{
public static function execute(string $migrationFileName, Carbon $now): string
{
$migrationsPath = 'migrations/'.dirname($migrationFileName).'/';
$migrationFileName = basename($migrationFileName);

$len = strlen($migrationFileName) + 4;

if (Str::contains($migrationFileName, '/')) {
$migrationsPath .= Str::of($migrationFileName)->beforeLast('/')->finish('/');
$migrationFileName = Str::of($migrationFileName)->afterLast('/');
}

foreach (glob(database_path("{$migrationsPath}*.php")) as $filename) {
if ((substr($filename, -$len) === $migrationFileName.'.php')) {
return $filename;
}
}

$timestamp = $now->format('Y_m_d_His');
$migrationFileName = Str::of($migrationFileName)->snake()->finish('.php');

return database_path($migrationsPath.$timestamp.'_'.$migrationFileName);
}
}
242 changes: 29 additions & 213 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,233 +3,54 @@
namespace Spatie\LaravelPackageTools;

use Illuminate\Support\Str;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Traits\HasAssets;
use Spatie\LaravelPackageTools\Traits\HasCommands;
use Spatie\LaravelPackageTools\Traits\HasConfigs;
use Spatie\LaravelPackageTools\Traits\HasConsoleCommands;
use Spatie\LaravelPackageTools\Traits\HasInertia;
use Spatie\LaravelPackageTools\Traits\HasInstallCommand;
use Spatie\LaravelPackageTools\Traits\HasMigrations;
use Spatie\LaravelPackageTools\Traits\HasProviders;
use Spatie\LaravelPackageTools\Traits\HasRoutes;
use Spatie\LaravelPackageTools\Traits\HasTranslations;
use Spatie\LaravelPackageTools\Traits\HasViewComponents;
use Spatie\LaravelPackageTools\Traits\HasViewComposers;
use Spatie\LaravelPackageTools\Traits\HasViews;
use Spatie\LaravelPackageTools\Traits\HasViewSharedData;

class Package
{
public string $name;

public array $configFileNames = [];

public bool $hasViews = false;

public bool $hasInertiaComponents = false;

public ?string $viewNamespace = null;

public bool $hasTranslations = false;

public bool $hasAssets = false;

public bool $discoversMigrations = false;

public ?string $migrationsPath = null;
use HasAssets;
use HasCommands;
use HasConsoleCommands;
use HasConfigs;
use HasInertia;
use HasInstallCommand;
use HasMigrations;
use HasProviders;
use HasRoutes;
use HasTranslations;
use HasViews;
use HasViewComponents;
use HasViewComposers;
use HasViewSharedData;

public bool $runsMigrations = false;

public array $migrationFileNames = [];

public array $routeFileNames = [];

public array $commands = [];

public array $consoleCommands = [];

public array $viewComponents = [];

public array $sharedViewData = [];

public array $viewComposers = [];
public string $name;

public string $basePath;

public ?string $publishableProviderName = null;

public function name(string $name): static
{
$this->name = $name;

return $this;
}

public function hasConfigFile($configFileName = null): static
{
$configFileName ??= $this->shortName();

if (! is_array($configFileName)) {
$configFileName = [$configFileName];
}

$this->configFileNames = $configFileName;

return $this;
}

public function publishesServiceProvider(string $providerName): static
{
$this->publishableProviderName = $providerName;

return $this;
}

public function hasInstallCommand($callable): static
{
$installCommand = new InstallCommand($this);

$callable($installCommand);

$this->consoleCommands[] = $installCommand;

return $this;
}

public function shortName(): string
{
return Str::after($this->name, 'laravel-');
}

public function hasViews(?string $namespace = null): static
{
$this->hasViews = true;

$this->viewNamespace = $namespace;

return $this;
}

public function hasInertiaComponents(?string $namespace = null): static
{
$this->hasInertiaComponents = true;

$this->viewNamespace = $namespace;

return $this;
}

public function hasViewComponent(string $prefix, string $viewComponentName): static
{
$this->viewComponents[$viewComponentName] = $prefix;

return $this;
}

public function hasViewComponents(string $prefix, ...$viewComponentNames): static
{
foreach ($viewComponentNames as $componentName) {
$this->viewComponents[$componentName] = $prefix;
}

return $this;
}

public function sharesDataWithAllViews(string $name, $value): static
{
$this->sharedViewData[$name] = $value;

return $this;
}

public function hasViewComposer($view, $viewComposer): static
{
if (! is_array($view)) {
$view = [$view];
}

foreach ($view as $viewName) {
$this->viewComposers[$viewName] = $viewComposer;
}

return $this;
}

public function hasTranslations(): static
{
$this->hasTranslations = true;

return $this;
}

public function hasAssets(): static
{
$this->hasAssets = true;

return $this;
}

public function discoversMigrations(bool $discoversMigrations = true, string $path = '/database/migrations'): static
{
$this->discoversMigrations = $discoversMigrations;
$this->migrationsPath = $path;

return $this;
}

public function runsMigrations(bool $runsMigrations = true): static
{
$this->runsMigrations = $runsMigrations;

return $this;
}

public function hasMigration(string $migrationFileName): static
{
$this->migrationFileNames[] = $migrationFileName;

return $this;
}

public function hasMigrations(...$migrationFileNames): static
{
$this->migrationFileNames = array_merge(
$this->migrationFileNames,
collect($migrationFileNames)->flatten()->toArray()
);

return $this;
}

public function hasCommand(string $commandClassName): static
{
$this->commands[] = $commandClassName;

return $this;
}

public function hasCommands(...$commandClassNames): static
{
$this->commands = array_merge($this->commands, collect($commandClassNames)->flatten()->toArray());

return $this;
}

public function hasConsoleCommand(string $commandClassName): static
{
$this->consoleCommands[] = $commandClassName;

return $this;
}

public function hasConsoleCommands(...$commandClassNames): static
{
$this->consoleCommands = array_merge($this->consoleCommands, collect($commandClassNames)->flatten()->toArray());

return $this;
}

public function hasRoute(string $routeFileName): static
{
$this->routeFileNames[] = $routeFileName;

return $this;
}

public function hasRoutes(...$routeFileNames): static
{
$this->routeFileNames = array_merge($this->routeFileNames, collect($routeFileNames)->flatten()->toArray());

return $this;
}

public function basePath(?string $directory = null): string
{
if ($directory === null) {
Expand All @@ -239,11 +60,6 @@ public function basePath(?string $directory = null): string
return $this->basePath . DIRECTORY_SEPARATOR . ltrim($directory, DIRECTORY_SEPARATOR);
}

public function viewNamespace(): string
{
return $this->viewNamespace ?? $this->shortName();
}

public function setBasePath(string $path): static
{
$this->basePath = $path;
Expand Down
Loading
Loading