Skip to content

Commit

Permalink
partials generator added
Browse files Browse the repository at this point in the history
  • Loading branch information
AbmSourav committed Jun 15, 2023
1 parent 885635a commit 4de1068
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 50 deletions.
6 changes: 1 addition & 5 deletions bin/kathamo
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
require_once __DIR__ . '../../vendor/autoload.php';

use Kathamo\App\Commands\KathamoCLI;
use Symfony\Component\Console\Application;

$app = new Application();
$app->add(new KathamoCLI);

$app->run();
new KathamoCLI();
2 changes: 1 addition & 1 deletion src/CommandManager/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Kathamo\App\CommandManager;

class Manager
class Helper
{
public function input($label)
{
Expand Down
14 changes: 0 additions & 14 deletions src/CommandManager/Template.php

This file was deleted.

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

namespace Kathamo\App\Commands;

trait CommandHelper
{
protected $input = [];
protected $root_path;
protected $target_path;

public function input($label)
{
return readline(
sprintf(" %s%s %s", "\033[1;34m", $label, "\033[0m")
);
}

protected function getConfig()
{
if (! is_file($this->root_path . "/configs/config.php")) {
throw new \Exception("Config file not found.");
}
return require $this->root_path . "/configs/config.php";
}

protected function generateFile($template)
{
$data = array_merge($this->input, $this->getConfig());
$data['namespace_suffix'] = "";
if (! empty($data['input_path'])) {
$data['namespace_suffix'] = "\\" . str_replace("/", "\\", $data['input_path']);
}

$controller_template = dirname(__DIR__) . "/templates/partials/$template";
$mustache = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES));
$file_content = $mustache->render(file_get_contents($controller_template), $data);

$this->create($data, $file_content);
}

protected function create($data, $file_content)
{
$controller_dir = $this->target_path . $data['input_path'];
if (! empty($data['input_path']) && ! is_dir($controller_dir)) {
mkdir($controller_dir);
}
if (is_dir($controller_dir)) {
$target_path = $controller_dir . "/";
}

$file = fopen(
$target_path . $data['input_class_name'] . ".php", "w"
);
fwrite($file, $file_content);
fclose($file);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
<?php
namespace Kathamo\App\CommandManager;
namespace Kathamo\App\Commands\Creation;

use Kathamo\App\Commands\CommandHelper;
use Kathamo\App\Services\CreateSkeletonFiles;
use Kathamo\App\Services\Notifier;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Scaffold extends Manager
class Scaffold extends Command
{
private $input = [];
use CommandHelper;

public function __construct()
{
$this->getInputs();
protected function configure()
{
$this->setName('create create:plugin')
->setDescription('Cli for Kathamo framework.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getInputs();

echo Notifier::notify(" Creating Kathamo framework scaffold...");
new CreateSkeletonFiles($this->input);

echo Notifier::notify("\n Done.");

return Command::SUCCESS;
}

private function getInputs()
Expand Down
28 changes: 28 additions & 0 deletions src/Commands/Creation/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Kathamo\App\Commands\Creation;

use Kathamo\App\Commands\CommandHelper;
use Kathamo\App\Services\CreateTemplateFiles;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Template extends Command
{
use CommandHelper;

private $kathamo_path = '/var/www/html/wp-content/plugins/kathamo';

protected function configure()
{
$this->setName('create create:template')
->setDescription('Cli for Kathamo framework.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
new CreateTemplateFiles($this->kathamo_path);

return Command::SUCCESS;
}
}
38 changes: 16 additions & 22 deletions src/Commands/KathamoCLI.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
<?php
namespace Kathamo\App\Commands;

use Kathamo\App\CommandManager\Scaffold;
use Kathamo\App\CommandManager\Template;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application;
use Kathamo\App\Commands\Creation\Scaffold;
use Kathamo\App\Commands\Creation\Template;
use Kathamo\App\Commands\Make\MakeController;
use Kathamo\App\Commands\Make\MakeMigration;
use Kathamo\App\Commands\Make\MakeService;

class KathamoCLI extends Command
class KathamoCLI
{
protected function configure()
public function __construct()
{
$this->setName('create')
->setDescription('Cli for Kathamo framework.')
->addOption('new')
->addOption('template');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('new')) {
new Scaffold();
}
$app = new Application();
$app->add(new Scaffold());
$app->add(new Template());

if ($input->getOption('template')) {
new Template();
}
$app->add(new MakeMigration());
$app->add(new MakeController());
$app->add(new MakeService());
$app->add(new MakeMigration());

return Command::SUCCESS;
$app->run();
}
}
37 changes: 37 additions & 0 deletions src/Commands/Make/MakeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Kathamo\App\Commands\Make;

use Kathamo\App\Commands\CommandHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MakeController extends Command
{
use CommandHelper;

protected function configure()
{
$this->setName('make make:controller')
->setDescription('New Controller for plugin')
->setAliases(['make:c']);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getInput();

return Command::SUCCESS;
}

public function getInput()
{
$this->root_path = getcwd();
$this->target_path = $this->root_path . "/app/Controllers/";
$this->input['input_class_name'] = $this->input("Controller Name:");
$this->input['input_path'] = $this->input("Path [app/Controllers/]:");

$this->generateFile('Controller_php.mustache');
}
}
68 changes: 68 additions & 0 deletions src/Commands/Make/MakeMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Kathamo\App\Commands\Make;

use Kathamo\App\Commands\CommandHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MakeMiddleware extends Command
{
use CommandHelper;

protected function configure()
{
$this->setName('make make:middleware')
->setDescription('New Middleware for plugin')
->setAliases(['make:mw']);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getInput();
$this->generateMiddleware();

return Command::SUCCESS;
}

public function getInput()
{
$this->root_path = getcwd();
$this->target_path = $this->root_path . "/app/Controllers/Middleware/";
$this->input['input_class_name'] = $this->input("Middleware Class Name:");
$this->input['middleware_key'] = $this->input("Middleware key:");
$this->input['input_path'] = $this->input("Path [app/Controllers/Middleware]:");
}

private function generateMiddleware()
{
$data = $this->input;
$config_data = $this->getConfig($this->root_path);
$middware_list = $config_data['middlewares'];
$middleware_name = $config_data['namaspace_root'] . "\App\Controllers\Middleware\\" . $data['input_class_name'];
$middware_list[$data['middleware_key']] = $middleware_name;

$this->writeConfig($middware_list);
$this->generateFile('Middleware_php.mustache');
}

private function writeConfig($data)
{
$schema = '';
foreach ($data as $key => $i) {
$schema .= "'$key' => $i::class,\n\t\t";
}

$controller_template = dirname(__DIR__) . '/templates/partials/config_php.mustache';
$mustache = new \Mustache_Engine();
$file_content = $mustache->render(
file_get_contents($controller_template),
['middleware_list' => $schema]
);

$filesystem = new Filesystem;
$filesystem->dumpFile($this->root_path . '/Configs/config.php', $file_content);
}
}
35 changes: 35 additions & 0 deletions src/Commands/Make/MakeMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kathamo\App\Commands\Make;

use Kathamo\App\Commands\CommandHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MakeMigration extends Command
{
use CommandHelper;

protected function configure()
{
$this->setName('make make:migration')
->setDescription('New Migration class for plugin')
->setAliases(['make:m']);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getInput();
$this->generateFile('Migration_php.mustache');

return Command::SUCCESS;
}

public function getInput()
{
$this->root_path = getcwd();
$this->target_path = $this->root_path . "/Database/Migrations/";
$this->input['input_class_name'] = $this->input("Migration Name:");
}
}
36 changes: 36 additions & 0 deletions src/Commands/Make/MakeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Kathamo\App\Commands\Make;

use Kathamo\App\Commands\CommandHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MakeService extends Command
{
use CommandHelper;

protected function configure()
{
$this->setName('make make:service')
->setDescription('New Service for plugin')
->setAliases(['make:s']);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getInput();
$this->generateFile('Service_php.mustache');

return Command::SUCCESS;
}

public function getInput()
{
$this->root_path = getcwd();
$this->target_path = getcwd() . "/app/Services/";
$this->input['input_class_name'] = $this->input("Service Name:");
$this->input['input_path'] = $this->input("Path [app/Services/]:");
}
}
Loading

0 comments on commit 4de1068

Please sign in to comment.