Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from drajathasan/development
Browse files Browse the repository at this point in the history
Improve : Plugin feature
  • Loading branch information
drajathasan authored Jun 3, 2021
2 parents a8dd19f + 7f58441 commit 12f1818
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 27 deletions.
3 changes: 0 additions & 3 deletions src/SLiMSTarsius/Docgenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public static function usage()
echo "\n\e[32m --plugin:create \033[0m\tMembuat kerangka dasar plugin (mode non-hook)";
echo "\n\e[32m --plugin:list \033[0m\tMenampilkan plugin tersedia";
echo "\n\e[32m --plugin:info \033[0m\tMenampilkan informasi plugin";
echo "\n\e[32m --plugin:enable \033[0m\tMengaktifkan plugin (fitur mendatang)";
echo "\n\e[32m --plugin:disable \033[0m\tMeng-non-aktifkan plugin (fitur mendatang)";
echo "\n\e[32m --plugin:delete \033[0m\tMenghapus plugin (fitur mendatang)";
/* Module */
echo "\n\e[36m --module \033[0m";
echo "\n\e[32m --module:create \033[0m\tMembuat kerangka dasar module (fitur mendatang)";
Expand Down
43 changes: 43 additions & 0 deletions src/SLiMSTarsius/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @author Drajat Hasan
* @email [[email protected]]
* @create date 2021-03-28 10:34:09
* @modify date 2021-03-28 10:34:09
* @desc [description]
*/

namespace SLiMSTarsius;

use SLiMSTarsius\Migrationext\{AddAction};

class Migration
{
private $type;
private $optionsData;

public function __construct()
{

}

public function run()
{
$Options = [
'kolom' => [
'type' => 'varchar',
'constraint' => 20,
'null' => true,
'default' => 'Pintar'
],
'kolom2' => [
'type' => 'int',
'constraint' => 20,
'null' => false,
'default' => 10
]
];

echo (AddAction::process('backup_log', AddAction::alter($Options)))."\n";
}
}
56 changes: 56 additions & 0 deletions src/SLiMSTarsius/Migrationext/AddAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace SLiMSTarsius\Migrationext;

class AddAction
{
public static function alter($arrayData)
{
$rawQuery = [];
foreach ($arrayData as $column => $options) {
$rawQuery[$column] = [];
$rawQuery[$column][0] = 'ADD `' . $column . '` ';
foreach ($options as $option => $value) {
switch ($option) {
case 'type':
$withContraint = '';
if ((isset($options['constraint']) && !in_array($options['type'], ['datetime','text','longtext','mediumtext',''])))
{
$withContraint = '(' .(int)$options['constraint']. ')';
}
$rawQuery[$column][1] = str_replace("'", "\'", $value) . $withContraint.' ';
break;
case 'null':
$rawQuery[$column][2] = 'NOT NULL ';
if ($value)
{
$rawQuery[$column][2] = 'NULL ';
}
case 'default':
$rawQuery[$column][3] = 'DEFAULT \'' . str_replace("'", "\'", $value) . '\' ';
break;
case 'increment':
$rawQuery[$column][4] = 'AUTO_INCREMENT UNIQUE ';
break;
case 'after':
$rawQuery[$column][5] = 'AFTER `' . str_replace("'", "\'", $value) .'` ';
break;
}
}
}

return ['ALTER TABLE ', $rawQuery];
}

public static function process($tableName, $raw)
{
$rawQuery = $raw[0] . ' `'. $tableName .'` ';
foreach ($raw[1] as $value) {
$rawQuery .= trim(implode('', $value)). ', ';
}

$rawQuery = substr_replace($rawQuery, ';', -2);

echo $rawQuery;
}
}
1 change: 0 additions & 1 deletion src/SLiMSTarsius/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function compile()
}
unset($this->arguments[$id]);
}

return $this;
}
}
140 changes: 118 additions & 22 deletions src/SLiMSTarsius/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,30 @@ class Plugin
{
public $env;
public $interactiveResponse;
public $template = 'index-plugin';

public function __construct($env)
/**
* Contructor
*
* @param string $env
* @return @return Contructor
*/
public function __construct(string $env)
{
@date_default_timezone_set('Asia/Jakarta');
$this->env = $env;
}

public function create($dest, $pluginName)
/**
* Create Plugin
*
* @param string $dest
* @param string $pluginName
* @return void
*/
public function create(string $dest, array $parameter)
{
// setup interactive question
$interactiveMap = ['plugin_uri' => 'Plugin URI (Alamat website)',
'description' => 'Description',
'version' => 'Version (Minimal gunakan semantic versioning)',
Expand All @@ -42,26 +57,54 @@ public function create($dest, $pluginName)
'label_menu' => 'Teks yang muncul di Menu?'
];

$destinantion = ($this->env === 'development_src')?$dest.'/tests/plugins/':$dest.'/plugins/';
$template = ($this->env === 'development_src')?$dest.'/tests/template/':$dest.'/vendor/drajat/slims-tarsius/tests/template/';
// set destination
$destinantionDirectory = ($this->env === 'development_src')?$dest.'/tests/plugins/':$dest.'/plugins/';
// setup template directory
$templateDirectory = ($this->env === 'development_src')?$dest.'/tests/template/':$dest.'/vendor/drajat/slims-tarsius/tests/template/';

if (count($pluginName) > 1)
// set up custom parameter
if (isset($parameter[1]) && preg_match('/\--[a-z]+=/i', $parameter[1]))
{
$pattern = explode('=', trim($parameter[1], '-'));

if (property_exists($this, $pattern[0]) && file_exists($templateDirectory.'/'.$pattern[1].'.Template'))
{
// set propperty
$this->{$pattern[0]} = $pattern[1];
// kill other parameter
unset($parameter[1]);
}
else
{
dg::failedMsg("Template {pointMsg} tidak ada.", $pattern[1]);
}
}

if (count($parameter) > 1)
{
dg::failedMsg("{pointMsg}", 'Hanya bisa membuat 1 plugin dalam 1 perintah!');
}

$pluginName = $pluginName[0];
$pluginName = $parameter[0];

// set message
echo "\nMembuat plugin \e[36m$pluginName\033[0m\n\n";
// get information, create sampe data and make plugin
$this->makeSampleData()
->makeInteractive($interactiveMap)
->makePlugin($pluginName, $destinantion, $template);
->makePlugin($pluginName, $destinantionDirectory, $templateDirectory);
}

public function list($dir, $parameter)
/**
* List plugin
*
* @param string $dir
* @param array $parameter
* @return void
*/
public function list(string $dir, array $parameter)
{
// class check
if (class_exists('\\SLiMS\\DB'))
{
// get database instance
Expand All @@ -71,6 +114,7 @@ public function list($dir, $parameter)
exit("\n");
}

// set criteria
$criteria = '';
if ($parameter[0] !== 'all')
{
Expand All @@ -81,12 +125,14 @@ public function list($dir, $parameter)
// get data
$runQuery = $database->query('select * from plugins'.$criteria);

// check row
if ($runQuery->num_rows > 0)
{
$listActive = [];
$listDisctive = [];
while ($data = $runQuery->fetch_assoc())
{
// check path
if (file_exists($data['path']))
{
// slicing
Expand All @@ -98,21 +144,37 @@ public function list($dir, $parameter)
$listActive[] = [$data['id'], '/'.implode('/', $slicePath).'/' ,$plugin];
}
}
// set list data
$heading = " No ID\t\t\t\t\tNama Plugin\t\tPath\n";
dg::list('Berikut daftar plugin aktif', $listActive, $heading);
exit;
}
// set info
dg::info('Info', [['TIdak ada data yang tersedia.']]);
exit;

}
// set error
dg::failedMsg("{pointMsg} tidak ada", 'Namespace \\SLiMS\\DB');
}

public function info($dir, $parameter)
/**
* Get Plugin Information
*
* @param string $dir
* @param array $parameter
* @return void
*/
public function info(string $dir, array $parameter)
{
if (class_exists('\\SLiMS\\DB'))
{
// get database instance
$database = \SLiMS\DB::getInstance('mysqli');
// check connection
if (mysqli_connect_error()) {
exit("\n");
if (mysqli_connect_errno()) {
// set error
dg::failedMsg("Database : {pointMsg}", mysqli_connect_error());
}

$criteria = '';
Expand All @@ -132,7 +194,6 @@ public function info($dir, $parameter)
{
if (file_exists($data['path']))
{

// parsing plugin data -> took from lib/Plugins.php
$file_open = fopen($data['path'], 'r');
$raw_data = fread($file_open, 8192);
Expand All @@ -148,10 +209,23 @@ public function info($dir, $parameter)
dg::info('Detail plugin '.$data['id'], $info);
}
}
exit;
}
// set info
dg::info('Info', [['TIdak ada data yang tersedia.']]);
exit;
}
// set error
dg::failedMsg("{pointMsg} tidak ada", 'Namespace \\SLiMS\\DB');
exit;
}


/**
* Making Sample Data
*
* @return object
*/
private function makeSampleData()
{
if (class_exists('\\SLiMS\\DB'))
Expand Down Expand Up @@ -182,10 +256,18 @@ private function makeSampleData()
return $this;
}

private function makeInteractive($label)
/**
* Interactive Question
*
* @param array $label
* @return void
*/
private function makeInteractive(array $label)
{
// check label
if (is_array($label))
{
// loop
foreach ($label as $key => $question) {
echo "\e[1m$question plugin?\033[0m [tuliskan] ";
$this->interactiveResponse[$key] = trim(fgets(STDIN));
Expand All @@ -199,17 +281,31 @@ private function makeInteractive($label)
}
}

private function makePlugin($pluginName, $destDir, $template)
/**
* Make plugin
*
* @param string $pluginName
* @param string $destDir
* @param string $template
* @return void
*/
private function makePlugin(string $pluginName, string $destDir, string $templateDir)
{
if (!is_dir($destDir.$pluginName))
// mpdify string
$fixPluginName = ucwords(str_replace('_', ' ',trim($pluginName, '"\' ')));
$dirPlugin = strtolower(str_replace(' ', '_', $fixPluginName));

// check dir
if (!is_dir($destDir.$dirPlugin))
{
if (mkdir($destDir.$pluginName, 0755, true))
// Make directory
if (mkdir($destDir.$dirPlugin, 0755, true))
{
// get file template
$dotPlugin = file_get_contents($template.'dot-plugin.Template');
$indexPlugin = file_get_contents($template.'index-plugin.Template');
$dotPlugin = file_get_contents($templateDir.'dot-plugin.Template');
$indexPlugin = file_get_contents($templateDir.$this->template.'.Template');
// mutation
$this->interactiveResponse['plugin_name'] = strtolower(str_replace(' ', '_', $pluginName));
$this->interactiveResponse['plugin_name'] = $fixPluginName;
$this->interactiveResponse['date_created'] = date('Y-m-d H:i:s');

foreach ($this->interactiveResponse as $key => $value) {
Expand All @@ -221,16 +317,16 @@ private function makePlugin($pluginName, $destDir, $template)
else
{
// remove directory
rmdir($destDir.$pluginName);
rmdir($destDir.$dirPlugin);
// set message
dg::failedMsg("Parameter {pointMsg} tidak boleh kosong!", $key);
}
}

try {
// set file
$dotPluginFIle = file_put_contents($destDir.$pluginName.'/'.strtolower(str_replace(' ', '_', $pluginName)).'.plugin.php', $dotPlugin);
$indexPluginFIle = file_put_contents($destDir.$pluginName.'/index.php', $indexPlugin);
$dotPluginFIle = file_put_contents($destDir.$dirPlugin.'/'.strtolower(str_replace(' ', '_', $pluginName)).'.plugin.php', $dotPlugin);
$indexPluginFIle = file_put_contents($destDir.$dirPlugin.'/index.php', $indexPlugin);

if ($dotPlugin && $indexPlugin)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SLiMSTarsius/Tarmagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// for production, comment if on development process
@ini_set('display_errors', false);
@error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
@error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);

class Tarmagick
{
Expand Down

0 comments on commit 12f1818

Please sign in to comment.