Skip to content

Commit 2e01bf9

Browse files
committed
refactor: Adiciona método getService na classe MigrateCommand e melhora a resolução de serviços do container
1 parent fc9747a commit 2e01bf9

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/Commands/MigrateCommand.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
*/
1717
class MigrateCommand extends BaseCommand
1818
{
19+
/**
20+
* Instância da aplicação ou container.
21+
*
22+
* @var null|object
23+
*/
24+
protected $app;
25+
1926
/**
2027
* Executa o comando principal para migrações.
2128
*
@@ -30,6 +37,25 @@ public function handle(): int
3037
return $this->migrate();
3138
}
3239

40+
/**
41+
* Resolve um serviço do container PSR-11 ou via app().
42+
*/
43+
protected function getService(string $id): mixed
44+
{
45+
// PSR-11: se existir $this->app e for container
46+
if (property_exists($this, 'app') && is_object($this->app)) {
47+
$container = $this->app;
48+
if (method_exists($container, 'has') && $container->has($id)) {
49+
return method_exists($container, 'get') ? $container->get($id) : null;
50+
}
51+
}
52+
// Fallback para helper global app()
53+
if (function_exists('app')) {
54+
return app($id);
55+
}
56+
throw new \RuntimeException("Service '{$id}' not found in container or app().");
57+
}
58+
3359
/**
3460
* Executa as migrações pendentes.
3561
*
@@ -40,14 +66,7 @@ private function migrate(): int
4066
$this->info('Running migrations...');
4167

4268
try {
43-
if (function_exists('app')) {
44-
$migrator = app('cycle.migrator');
45-
} else {
46-
$this->error('Application container not available');
47-
48-
return 1;
49-
}
50-
69+
$migrator = $this->getService('cycle.migrator');
5170
$migrator->run();
5271
$this->info('Migrations executed successfully.');
5372

@@ -69,14 +88,7 @@ private function rollback(): int
6988
$this->info('Rolling back last migration...');
7089

7190
try {
72-
if (function_exists('app')) {
73-
$migrator = app('cycle.migrator');
74-
} else {
75-
$this->error('Application container not available');
76-
77-
return 1;
78-
}
79-
91+
$migrator = $this->getService('cycle.migrator');
8092
$migrator->run();
8193
$this->info('Migration rolled back successfully.');
8294

src/Exceptions/EntityNotFoundException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class EntityNotFoundException extends CycleORMException
66
{
77
private string $entityClass;
8+
89
private int|string $identifier;
910

1011
public function __construct(

0 commit comments

Comments
 (0)