Skip to content

Commit

Permalink
feat: add commands & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
regnerisch committed May 25, 2024
1 parent bed01fa commit 41b5ca6
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"require-dev": {
"phpunit/phpunit": "^10.3",
"orchestra/testbench": "^8.5|^9.0",
"orchestra/testbench": "^8.23|^9.1",
"laravel/pint": "^1.10|^2.0",
"spatie/laravel-query-builder": "^5.2",
"spatie/laravel-data": "^3.7|^4.0",
Expand Down
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Documentation

## Commands
### Application
- [`beyond:make:command`](commands/make-command.md)
Expand All @@ -21,7 +20,9 @@
- `beyond:make:listener`
- `beyond:make:model`
- `beyond:make:observer`
- [`beyond:make:scope`](commands/make-scope.md)

### Support
- [`beyond:make:cast`](commands/make-cast.md)
- [`beyond:make:provider`](commands/make-provider.md)
- [`beyond:make:rule`](commands/make-rule.md)
13 changes: 13 additions & 0 deletions docs/commands/make-cast.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `beyond:make:cast`
Creates a new cast.

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

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

| Flags | Description |
|----------|-------------------------|
| --force | Overwrite existing file |
2 changes: 1 addition & 1 deletion docs/commands/make-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Creates a new service provider.

> [!NOTE]
> Within the register method, you should **only bind things into the service container**.
> Because of this, service containers are **not** created under `Application/{App}/Providers`
> Because of this, service providers are **not** created under `Application/{App}/Providers`.
> We want to avoid the feeling of registering things per application. You always register things
> for your entire Laravel project.
Expand Down
14 changes: 14 additions & 0 deletions docs/commands/make-scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# `beyond:make:scope`
Creates a new scope.

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

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

| Flags | Description |
|----------|-------------------------------------------|
| --global | Creates the scope inside `Support\Scopes` |
| --force | Overwrite existing file |
23 changes: 23 additions & 0 deletions src/Commands/MakeCastCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace AkrilliA\LaravelBeyond\Commands;

use AkrilliA\LaravelBeyond\Commands\Abstracts\SupportCommand;
use AkrilliA\LaravelBeyond\Type;

final class MakeCastCommand extends SupportCommand
{
protected $signature = 'beyond:make:cast {name} {--force}';

protected $description = 'Make a new cast';

protected function getStub(): string
{
return 'cast.stub';
}

public function getType(): Type
{
return new Type('Cast');
}
}
2 changes: 1 addition & 1 deletion src/Commands/MakeResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class MakeResourceCommand extends ApplicationCommand
{
protected $signature = 'beyond:make:resource {name?} {--collection} {--force}';
protected $signature = 'beyond:make:resource {name} {--collection} {--force}';

protected $description = 'Make a new resource';

Expand Down
3 changes: 1 addition & 2 deletions src/Commands/MakeRuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace AkrilliA\LaravelBeyond\Commands;

use AkrilliA\LaravelBeyond\Commands\Abstracts\ApplicationCommand;
use AkrilliA\LaravelBeyond\Commands\Abstracts\SupportCommand;
use AkrilliA\LaravelBeyond\Type;

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

protected $description = 'Make a new rule';

Expand Down
23 changes: 23 additions & 0 deletions src/Commands/MakeScopeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace AkrilliA\LaravelBeyond\Commands;

use AkrilliA\LaravelBeyond\Commands\Abstracts\DomainCommand;
use AkrilliA\LaravelBeyond\Type;

final class MakeScopeCommand extends DomainCommand
{
protected $signature = 'beyond:make:scope {name} {--g|global} {--force}';

protected $description = 'Make a new scope';

protected function getStub(): string
{
return 'scope.stub';
}

public function getType(): Type
{
return new Type('Scope');
}
}
1 change: 0 additions & 1 deletion src/Commands/MakeServiceProviderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace AkrilliA\LaravelBeyond\Commands;

use AkrilliA\LaravelBeyond\Commands\Abstracts\ApplicationCommand;
use AkrilliA\LaravelBeyond\Commands\Abstracts\SupportCommand;
use AkrilliA\LaravelBeyond\Type;

Expand Down
10 changes: 6 additions & 4 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@
use AkrilliA\LaravelBeyond\Actions\RefactorFileAction;
use Illuminate\Filesystem\Filesystem;

use function Illuminate\Filesystem\join_paths;

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

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

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

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

Expand Down
29 changes: 29 additions & 0 deletions stubs/cast.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace {{ namespace }};

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

class {{ className }} implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}

/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}
}
18 changes: 18 additions & 0 deletions stubs/scope.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class {{ className }} implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
//
}
}
8 changes: 8 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

class BaseTest extends TestCase
{
public function testPathHelpers(): void
{
$this->assertEquals(base_path('src'), beyond_path());
$this->assertEquals(beyond_path('Application'), beyond_app_path());
$this->assertEquals(beyond_path('Domain'), beyond_domain_path());
$this->assertEquals(beyond_path('Support'), beyond_support_path());
}

public function testCanMakeClassWithDirectory(): void
{
$this->artisan('beyond:make:action User.Admin/UserStoreAction');
Expand Down
36 changes: 36 additions & 0 deletions tests/Commands/MakeCastCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tests\Commands;

use Tests\TestCase;

class MakeCastCommandTest extends TestCase
{
public function testCanMakeCast(): void
{
$this->artisan('beyond:make:cast TimezoneCast');

$file = beyond_support_path('Casts/TimezoneCast.php');
$contents = file_get_contents($file);

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

public function testCanMakeCastUsingForce(): void
{
$this->artisan('beyond:make:cast TimezoneCast');

$file = beyond_support_path('Casts/TimezoneCast.php');
$contents = file_get_contents($file);

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

$code = $this->artisan('beyond:make:cast TimezoneCast --force');

$code->assertOk();
}
}
36 changes: 36 additions & 0 deletions tests/Commands/MakeScopeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tests\Commands;

use Tests\TestCase;

class MakeScopeCommandTest extends TestCase
{
public function testCanMakeScope(): void
{
$this->artisan('beyond:make:scope User.ActiveScope');

$file = beyond_domain_path('User/Scopes/ActiveScope.php');
$contents = file_get_contents($file);

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

public function testCanMakeScopeUsingForce(): void
{
$this->artisan('beyond:make:scope User.ActiveScope');

$file = beyond_domain_path('User/Scopes/ActiveScope.php');
$contents = file_get_contents($file);

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

$code = $this->artisan('beyond:make:scope User.ActiveScope --force');

$code->assertOk();
}
}

0 comments on commit 41b5ca6

Please sign in to comment.