-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Jagepard/wip
Wip
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace App\Ship\Commands; | ||
|
||
use Rudra\Cli\ConsoleFacade as Cli; | ||
use Rudra\Container\Facades\Rudra; | ||
|
||
class CreateObserverCommand | ||
{ | ||
/** | ||
* Creates a file with Seed data | ||
* ----------------------------- | ||
* Создает файл с данными Seed | ||
*/ | ||
public function actionIndex() | ||
{ | ||
Cli::printer("Enter observer name: ", "magneta"); | ||
$prefix = str_replace(PHP_EOL, "", Cli::reader()); | ||
$className = ucfirst($prefix) . 'Observer'; | ||
|
||
Cli::printer("Enter container: ", "magneta"); | ||
$container = ucfirst(str_replace(PHP_EOL, "", Cli::reader())); | ||
|
||
if (!empty($container)) { | ||
|
||
$this->writeFile( | ||
[str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Observers/"), "{$className}.php"], | ||
$this->createClass($className, $container) | ||
); | ||
|
||
} else { | ||
$this->actionIndex(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @param string $container | ||
* @return string | ||
* | ||
* Creates class data | ||
* ------------------ | ||
* Создает данные класса | ||
*/ | ||
private function createClass(string $className, string $container) | ||
{ | ||
return <<<EOT | ||
<?php | ||
namespace App\Containers\\{$container}\Observers; | ||
use Rudra\EventDispatcher\ObserverInterface; | ||
class {$className} implements ObserverInterface | ||
{ | ||
public function onEvent() | ||
{ | ||
} | ||
} | ||
EOT; | ||
} | ||
|
||
/** | ||
* @param $path | ||
* @param $callable | ||
* | ||
* Writes data to a file | ||
* --------------------- | ||
* Записывает данные в файл | ||
*/ | ||
private function writeFile(array $path, string $data) | ||
{ | ||
if (!is_dir($path[0])) { | ||
mkdir($path[0], 0755, true); | ||
} | ||
|
||
$fullPath = $path[0] . $path[1]; | ||
|
||
if (!file_exists($fullPath)) { | ||
Cli::printer("The file ", "light_green"); | ||
Cli::printer($fullPath, "light_green"); | ||
Cli::printer(" was created" . PHP_EOL, "light_green"); | ||
file_put_contents($fullPath, $data); | ||
} else { | ||
Cli::printer("The file $fullPath is already exists" . PHP_EOL, "light_yellow"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters