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

Add Make Observer Command #1623

Merged
merged 2 commits into from
Oct 18, 2023
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
Prev Previous commit
[feat] Add observer command
  • Loading branch information
hanieas committed Oct 3, 2023
commit 80fdb23a456c95250d9ae8743436cc1745d0ed03
48 changes: 40 additions & 8 deletions src/Commands/ObserverMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class ObserverMakeCommand extends GeneratorCommand
{
Expand Down Expand Up @@ -55,27 +54,61 @@ protected function getTemplateContents()
{
$module = $this->laravel['modules']->findOrFail($this->getModuleName());
return (new Stub('/observer.stub', [
'NAMESPACE' => $this->getClassNamespace($module).'\Observers',
'CLASS' => $this->getClass(),
// 'NAMESPACEMODEL' => $this->getClass()
]))->render();
'NAMESPACE' => $this->getClassNamespace($module) . '\Observers',
'NAME' => $this->getModelName(),
'MODEL_NAMESPACE' => $this->getModelNamespace(),
'NAME_VARIABLE' => $this->getModelVariable(),
]))->render();
}

/**
* Get model namespace.
*
* @return string
*/
public function getModelNamespace(): string
{
$path = $this->laravel['modules']->config('paths.generator.model.path', 'Entities');

$path = str_replace('/', '\\', $path);

return $this->laravel['modules']->config('namespace') . '\\' . $this->laravel['modules']->findOrFail($this->getModuleName()) . '\\' . $path;
}

/**
* @return mixed|string
*/
private function getModelName()
{
return Str::studly($this->argument('name'));
}

/**
* @return mixed|string
*/
private function getModelVariable() : string
{
return '$'.Str::lower($this->argument('name'));
}

/**
* @return mixed
*/
protected function getDestinationFilePath()
{
$path = $this->laravel['modules']->getModulePath($this->getModuleName());

$observerPath = GenerateConfigReader::read('observer');
return $path . $observerPath->getPath() . '/' . $this->getFileName() . '.php';

return $path . $observerPath->getPath() . '/' . $this->getFileName();
}

/**
* @return string
*/
private function getFileName()
{
return Str::studly($this->argument('name'));
return Str::studly($this->argument('name')) . 'Observer.php';
}


Expand All @@ -87,5 +120,4 @@ public function handle(): int

return 0;
}

}
42 changes: 41 additions & 1 deletion src/Commands/stubs/observer.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,47 @@

namespace $NAMESPACE$;

class $CLASS$
use $MODEL_NAMESPACE$\$NAME$;

class $NAME$Observer
{
/**
* Handle the $NAME$ "created" event.
*/
public function created($NAME$ $NAME_VARIABLE$): void
{
//
}

/**
* Handle the $NAME$ "updated" event.
*/
public function updated($NAME$ $NAME_VARIABLE$): void
{
//
}

/**
* Handle the $NAME$ "deleted" event.
*/
public function deleted($NAME$ $NAME_VARIABLE$): void
{
//
}

/**
* Handle the $NAME$ "restored" event.
*/
public function restored($NAME$ $NAME_VARIABLE$): void
{
//
}

/**
* Handle the $NAME$ "force deleted" event.
*/
public function forceDeleted($NAME$ $NAME_VARIABLE$): void
{
//
}
}
1 change: 1 addition & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function getEnvironmentSetUp($app)
'migration' => ['path' => 'Database/Migrations', 'generate' => true],
'factory' => ['path' => 'Database/factories', 'generate' => true],
'model' => ['path' => 'Entities', 'generate' => true],
'observer' => ['path' => 'Observers', 'generate' => true],
'repository' => ['path' => 'Repositories', 'generate' => true],
'seeder' => ['path' => 'Database/Seeders', 'generate' => true],
'controller' => ['path' => 'Http/Controllers', 'generate' => true],
Expand Down
47 changes: 47 additions & 0 deletions tests/Commands/ObserverMakeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Nwidart\Modules\Tests\Commands;

use Nwidart\Modules\Contracts\RepositoryInterface;
use Nwidart\Modules\Tests\BaseTestCase;
use Spatie\Snapshots\MatchesSnapshots;

class ObserverMakeCommandTest extends BaseTestCase
{
use MatchesSnapshots;
/**
* @var \Illuminate\Filesystem\Filesystem
*/
private $finder;
/**
* @var string
*/
private $modulePath;

public function setUp(): void
{
parent::setUp();
$this->modulePath = base_path('modules/Blog');
$this->finder = $this->app['files'];
$this->artisan('module:make', ['name' => ['Blog']]);
}

public function tearDown(): void
{
$this->app[RepositoryInterface::class]->delete('Blog');
parent::tearDown();
}

/** @test */
public function it_makes_observer()
{
$code = $this->artisan('module:make-observer', ['name' => 'Post', 'module' => 'Blog']);

$observerFile = $this->modulePath . '/Observers/PostObserver.php';
// dd($observerFile);

$this->assertTrue(is_file($observerFile), 'Observer file was not created.');
$this->assertMatchesSnapshot($this->finder->get($observerFile));
$this->assertSame(0, $code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php return '<?php

namespace Modules\Blog\Observers;

use Modules\Blog\Entities\Post;

class PostObserver
{
/**
* Handle the Post "created" event.
*/
public function created(Post $post): void
{
//
}

/**
* Handle the Post "updated" event.
*/
public function updated(Post $post): void
{
//
}

/**
* Handle the Post "deleted" event.
*/
public function deleted(Post $post): void
{
//
}

/**
* Handle the Post "restored" event.
*/
public function restored(Post $post): void
{
//
}

/**
* Handle the Post "force deleted" event.
*/
public function forceDeleted(Post $post): void
{
//
}
}

';
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Modules\Blog\Observers;

use Modules\Blog\Entities\Post;

class PostObserver
{
/**
* Handle the Post "created" event.
*/
public function created(Post $post): void
{
//
}

/**
* Handle the Post "updated" event.
*/
public function updated(Post $post): void
{
//
}

/**
* Handle the Post "deleted" event.
*/
public function deleted(Post $post): void
{
//
}

/**
* Handle the Post "restored" event.
*/
public function restored(Post $post): void
{
//
}

/**
* Handle the Post "force deleted" event.
*/
public function forceDeleted(Post $post): void
{
//
}
}