Skip to content

Commit

Permalink
update: update some template logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 8, 2021
1 parent bc57878 commit c51197b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
4 changes: 2 additions & 2 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function init(): void
{
parent::init();

$this->dumpfile = Kite::getPath('tmp/json-load.json');
$this->dumpfile = Kite::getTmpPath('json-load.json');
}

protected function jsonRender(): JSONPretty
Expand Down Expand Up @@ -311,7 +311,7 @@ public function toClassCommand(FlagsParser $fs, Output $output): void
'tplDir' => $tplDir,
'tplFile' => $tplFile,
]));
// vdump($config);

// @user-custom/template/java-service-tpl/req-resp-dto.tpl
$gen = JsonToCode::create($type)
->setSource($json)
Expand Down
5 changes: 2 additions & 3 deletions app/Lib/Generate/JsonToJavaClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Inhere\Kite\Lib\Generate;

use function random_int;

/**
* class JsonToJavaClass
* - json(5) to java DTO class
Expand All @@ -18,7 +16,8 @@ class JsonToJavaClass extends AbstractJsonToCode
public function generate(): string
{
// todo
$this->contexts['pkgName'] = 'YOUR.PKG.NAME';
$this->contexts['package'] = 'YOUR.PKG.NAME';
$this->contexts['pkgName'] = 'PKG_NAME';
$this->contexts['className'] = 'YourClass';

return parent::generate();
Expand Down
21 changes: 20 additions & 1 deletion app/Lib/Template/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Inhere\Kite\Lib\Template;

use Inhere\Kite\Lib\Template\Contract\TemplateInterface;
use Toolkit\Stdlib\Obj;

/**
* Class AbstractTemplate
Expand All @@ -15,7 +16,25 @@ abstract class AbstractTemplate implements TemplateInterface
/**
* @var array
*/
protected $globalVars = [];
protected array $globalVars = [];

/**
* @return static
*/
public static function new(array $config = []): self
{
return new static($config);
}

/**
* Class constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
Obj::init($this, $config);
}

/**
* @return array
Expand Down
14 changes: 7 additions & 7 deletions app/Lib/Template/HtmlTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
class HtmlTemplate extends TextTemplate
{
/**
* @var string
* @var string[]
*/
protected string $viewsDir = '';
protected array $allowExt = ['.html', '.phtml', '.php'];

/**
* @var string[]
* @var string
*/
protected array $allowExt = ['.html', '.phtml', '.php'];
protected string $viewsDir = '';

/**
* manual set view files
*
* @var array
*/
protected $viewsFiles = [];
protected array $viewsFiles = [];

/**
* @param string $viewPath
Expand Down Expand Up @@ -79,7 +79,7 @@ protected function findViewFile(string $viewName): string
return $viewFile;
}

throw new InvalidArgumentException("the view file '$viewName' not found");
throw new InvalidArgumentException("no such view file: $viewName");
}

foreach ($this->allowExt as $ext) {
Expand All @@ -89,7 +89,7 @@ protected function findViewFile(string $viewName): string
}
}

throw new InvalidArgumentException("the view file '$viewName' not found");
throw new InvalidArgumentException("no such view file: $viewName");
}

/**
Expand Down
34 changes: 15 additions & 19 deletions app/Lib/Template/SimpleTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Inhere\Kite\Lib\Template;

use InvalidArgumentException;
use Toolkit\Stdlib\Obj;
use function array_merge;
use function file_exists;
use function file_get_contents;
use function sprintf;
use function str_contains;
use function strtr;

/**
Expand All @@ -19,19 +19,11 @@
class SimpleTemplate extends AbstractTemplate
{
/**
* template var format
*
* @var string
*/
protected string $varTpl = '{{%s}}';

/**
* Class constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
Obj::init($this, $config);
}
protected string $format = '{{%s}}';

/**
* @param string $tplFile
Expand All @@ -42,7 +34,7 @@ public function __construct(array $config = [])
public function renderFile(string $tplFile, array $tplVars): string
{
if (!file_exists($tplFile)) {
throw new InvalidArgumentException('the template file is not exist. file:' . $tplFile);
throw new InvalidArgumentException('No such template file:' . $tplFile);
}

$tplCode = file_get_contents($tplFile);
Expand All @@ -64,7 +56,7 @@ public function renderString(string $tplCode, array $tplVars): string

$fmtVars = [];
foreach ($tplVars as $name => $var) {
$name = sprintf($this->varTpl, (string)$name);
$name = sprintf($this->format, (string)$name);
// add
$fmtVars[$name] = $var;
}
Expand All @@ -75,16 +67,20 @@ public function renderString(string $tplCode, array $tplVars): string
/**
* @return string
*/
public function getVarTpl(): string
public function getFormat(): string
{
return $this->varTpl;
return $this->format;
}

/**
* @param string $varTpl
* @param string $format
*/
public function setVarTpl(string $varTpl): void
public function setFormat(string $format): void
{
$this->varTpl = $varTpl;
if (!str_contains($format, '%s')) {
throw new InvalidArgumentException('var format must contains %s');
}

$this->format = $format;
}
}
11 changes: 0 additions & 11 deletions app/Lib/Template/TextTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use RuntimeException;
use Throwable;
use Toolkit\FsUtil\Dir;
use Toolkit\Stdlib\Obj;
use Toolkit\Sys\Sys;
use function array_merge;
use function date;
Expand Down Expand Up @@ -48,16 +47,6 @@ class TextTemplate extends AbstractTemplate
*/
private string $tmpPhpFile = '';

/**
* Class constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
Obj::init($this, $config);
}

/**
* @param string $tplCode
* @param array $tplVars
Expand Down

0 comments on commit c51197b

Please sign in to comment.