Skip to content

Commit

Permalink
docs: write docs
Browse files Browse the repository at this point in the history
  • Loading branch information
regnerisch committed May 25, 2024
1 parent 58e148c commit 7d35cec
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Universal usable `--global` flag by [@regnerisch](https://github.com/regnerisch)

### Fixed
- Access before initialization by [@regnerisch](https://github.com/regnerisch) in [#100](https://github.com/akrillia/laravel-beyond/issues/100) reported by [@napruzzese](https://github.com/napruzzese)

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Register the namespaces inside your `composer.json`:
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/",
"Application\\": "src/Application",
"Domain\\": "src/Domain"
"Domain\\": "src/Domain",
"Support\\": "src/Support"
}
},
// ...
Expand Down
8 changes: 4 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
- [`beyond:make:job`](commands/make-job.md)
- [`beyond:make:policy`](commands/make-policy.md)
- [`beyond:make:process`](commands/make-process.md)
- `beyond:make:query`
- `beyond:make:request`
- `beyond:make:resource`
- `beyond:make:rule`
- [`beyond:make:query`](commands/make-query.md)
- [`beyond:make:request`](commands/make-request.md)
- [`beyond:make:resource`](commands/make-resource.md)
- [`beyond:make:rule`](commands/make-rule.md)

### Domain
- [`beyond:make:action`](commands/make-action.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/make-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Creates a new policy.
> $request = $this->app['request'];
>
> $app = match (true) {
> $request->is('your-app-prefix/*') => 'YourApp', // YourApp is the app name inside "src/App"
> $request->is('your-app-prefix/*') => 'YourApp', // YourApp is the app name inside "src/Application"
> $request->is('your-app2-prefix/*') => 'YourAppTwo',
> default => null,
> };
Expand Down
16 changes: 16 additions & 0 deletions docs/commands/make-query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `beyond:make:query`
Creates a new Spatie query builder.

> [!IMPORTANT]
> Requires package `spatie/laravel-query-builder`
## Signature
`beyond:make:query {name} {--force}`

| Parameters | Description |
|------------|--------------------------------|
| name | The name of your query builder |

| Flags | Description |
|---------|-------------------------|
| --force | Overwrite existing file |
13 changes: 13 additions & 0 deletions docs/commands/make-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `beyond:make:request`
Creates a new request.

## Signature
`beyond:make:request {name} {--force}`

| Parameters | Description |
|------------|--------------------------|
| name | The name of your request |

| Flags | Description |
|---------|-------------------------|
| --force | Overwrite existing file |
13 changes: 13 additions & 0 deletions docs/commands/make-resource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `beyond:make:resource`
Creates a new resource.

## Signature
`beyond:make:resource {name} {--force}`

| Parameters | Description |
|------------|---------------------------|
| name | The name of your resource |

| Flags | Description |
|---------|-------------------------|
| --force | Overwrite existing file |
14 changes: 14 additions & 0 deletions docs/commands/make-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# `beyond:make:rule`
Creates a new rule.

## Signature
`beyond:make:rule {name} {--g|global} {--force}`

| Parameters | Description |
|------------|-----------------------|
| name | The name of your rule |

| Flags | Description |
|----------|----------------------------------------------|
| --global | Creates a global rule inside `Support\Rules` |
| --force | Overwrite existing file |
3 changes: 2 additions & 1 deletion src/Commands/Abstracts/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AkrilliA\LaravelBeyond\NameResolver;
use AkrilliA\LaravelBeyond\Type;
use Illuminate\Console\Command;
use Illuminate\Support\Stringable;

use function Laravel\Prompts\error;
use function Laravel\Prompts\info;
Expand Down Expand Up @@ -56,7 +57,7 @@ protected function getNameArgument(): string

public function getNameResolver(?string $name = null): NameResolver
{
return new NameResolver($this, $name ?: $this->getNameArgument());
return new NameResolver($this, new Stringable($name ?: $this->getNameArgument()));
}

protected function configure(): void
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/MakeModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace AkrilliA\LaravelBeyond\Commands;

use AkrilliA\LaravelBeyond\Commands\Abstracts\DomainCommand;
use AkrilliA\LaravelBeyond\NameResolver;
use AkrilliA\LaravelBeyond\Type;
use Illuminate\Support\Str;

final class MakeModelCommand extends DomainCommand
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MakeRuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class MakeRuleCommand extends ApplicationCommand
{
protected $signature = 'beyond:make:rule {name?} {--force}';
protected $signature = 'beyond:make:rule {name?} {--g|global} {--force}';

protected $description = 'Make a new rule';

Expand Down
69 changes: 38 additions & 31 deletions src/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ final class NameResolver

public function __construct(
private readonly BaseCommand $command,
private readonly string $name
private readonly Stringable $name
) {
$this->init();
$this->setAppOrDomain($this->name);
$this->setDirectoryAndClassName($this->name->after('.'));
$this->setNamespace();
$this->setPath();
}

public function getNamespace(): string
{
return $this->namespace;
}

public function getAppOrDomain(): string
{
return $this->appOrDomain;
}

public function getClassName(): string
{
return $this->className;
Expand All @@ -49,46 +47,28 @@ public function getPath(): string
return $this->path;
}

public function getCommandNameArgument(): string
private function isGlobal(): bool
{
return $this->appOrDomain.'.'.$this->className;
return $this->command->hasOption('global') && $this->command->option('global');
}

private function init(): void
private function setAppOrDomain(Stringable $name): void
{
$name = new Stringable($this->name);
$this->appOrDomain = $name->contains('.')
? $name->before('.')->toString()
: null;
$this->setDirectoryAndClassName($name->after('.'));

if (! $this->appOrDomain) {
$this->appOrDomain = $this->askForAppOrDomainName();
if ($this->appOrDomain || $this->isGlobal()) {
return;
}

$this->namespace = sprintf(
$this->command->getNamespaceTemplate().'%s',
$this->appOrDomain,
$this->command->getType()->getNamespace(),
$this->directory ? '\\'.$this->directory : '',
);

$this->path = sprintf(
'%s/'.$this->command->getFileNameTemplate(),
Str::ucfirst(Str::replace('\\', '/', $this->namespace)),
$this->className,
);
}

private function askForAppOrDomainName(): string
{
$cases = match (true) {
$this->command instanceof ApplicationCommand => ['app', beyond_get_choices(base_path('src/Application'))],
$this->command instanceof DomainCommand => ['domain', beyond_get_choices(base_path('src/Domain'))],
default => []
};

return suggest(
$this->appOrDomain = suggest(
sprintf('On which %s do you want to add your %s', $cases[0], $this->command->getType()->getName()),
function (string $value) use ($cases) {
return collect($cases[1])->filter(fn ($o) => Str::contains($o, $value, true))->toArray();
Expand All @@ -102,4 +82,31 @@ private function setDirectoryAndClassName(Stringable $name): void
: '';
$this->className = $name->afterLast('/')->toString();
}

private function setNamespace(): void
{
if ($this->isGlobal()) {
$this->namespace = sprintf(
'Support\\%s\\%s',
$this->command->getType()->getNamespace(),
$this->directory ? '\\'.$this->directory : '',
);
} else {
$this->namespace = sprintf(
$this->command->getNamespaceTemplate().'%s',
$this->appOrDomain,
$this->command->getType()->getNamespace(),
$this->directory ? '\\'.$this->directory : '',
);
}
}

private function setPath(): void
{
$this->path = sprintf(
'%s/'.$this->command->getFileNameTemplate(),
Str::ucfirst(Str::replace('\\', '/', $this->namespace)),
$this->className,
);
}
}
7 changes: 7 additions & 0 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ function beyond_domain_path(string $path = ''): string
}
}

if (! function_exists('beyond_support_path')) {
function beyond_support_path(string $path = ''): string
{
return beyond_path('Support/'.ltrim($path, '/'));
}
}

if (! function_exists('beyond_copy_stub')) {
/**
* @param array<string, string> $refactor
Expand Down
28 changes: 28 additions & 0 deletions tests/Commands/MakeRuleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,32 @@ public function testCanMakeRuleUsingForce(): void

$code->assertOk();
}

public function testCanMakeGlobalRule(): void
{
$this->artisan('beyond:make:rule User.UniqueUser --global');

$file = beyond_support_path('Rules/UniqueUser.php');
$contents = file_get_contents($file);

$this->assertFileExists($file);
$this->assertStringNotContainsString('{{ namespace }}', $contents);
$this->assertStringNotContainsString('{{ className }}', $contents);
}

public function testCanMakeGlobalRuleUsingForce(): void
{
$this->artisan('beyond:make:rule User.UniqueUser --global');

$file = beyond_support_path('Rules/UniqueUser.php');
$contents = file_get_contents($file);

$this->assertFileExists($file);
$this->assertStringNotContainsString('{{ namespace }}', $contents);
$this->assertStringNotContainsString('{{ className }}', $contents);

$code = $this->artisan('beyond:make:rule User.UniqueUser --global --force');

$code->assertOk();
}
}

0 comments on commit 7d35cec

Please sign in to comment.