141 lines
2.5 KiB
PHP
Raw Normal View History

2020-11-19 09:42:54 +08:00
<?php
namespace catcher\generate\build;
2020-11-19 17:31:31 +08:00
use catcher\CatchAdmin;
use catcher\facade\FileSystem;
2020-11-19 09:42:54 +08:00
use catcher\generate\build\classes\Classes;
use PhpParser\BuilderFactory;
use PhpParser\PrettyPrinter\Standard;
2020-11-19 17:31:31 +08:00
class CatchBuild
2020-11-19 09:42:54 +08:00
{
protected $astBuilder;
2020-11-19 17:31:31 +08:00
protected $outPath;
protected $filename;
2020-11-19 09:42:54 +08:00
public function __construct()
{
$this->astBuilder = app(BuilderFactory::class);
}
2020-11-19 17:31:31 +08:00
/**
* 命名空间
*
* @time 2020年11月19日
* @param string $namespace
* @return $this
*/
2020-11-19 09:42:54 +08:00
public function namespace(string $namespace)
{
2020-11-19 17:31:31 +08:00
$this->astBuilder = $this->astBuilder->namespace($namespace);
2020-11-19 09:42:54 +08:00
return $this;
}
2020-11-19 17:31:31 +08:00
/**
* use 方法体
*
* @time 2020年11月19日
* @param $use
* @return $this
*/
2020-11-19 09:42:54 +08:00
public function use($use)
{
$this->astBuilder->addStmt($use);
return $this;
}
2020-11-19 17:31:31 +08:00
/**
* class 模版
*
* @time 2020年11月19日
* @param Classes $class
* @param \Closure $function
* @return $this
*/
2020-11-19 09:42:54 +08:00
public function class(Classes $class, \Closure $function)
{
$function($class);
$this->astBuilder->addStmt($class->build());
return $this;
}
2020-11-19 17:31:31 +08:00
/**
* 条件
*
* @time 2020年11月19日
* @param $condition
* @param \Closure $closure
* @return $this
*/
public function when($condition, \Closure $closure)
{
if ($condition && $closure instanceof \Closure) {
$closure($this);
}
return $this;
}
2020-11-19 09:42:54 +08:00
2020-11-19 17:31:31 +08:00
/**
* 获取内容
*
* @time 2020年11月19日
* @return string
*/
public function getContent()
2020-11-19 09:42:54 +08:00
{
$stmts = array($this->astBuilder->getNode());
2020-11-19 17:31:31 +08:00
2020-11-19 09:42:54 +08:00
$prettyPrinter = new Standard();
2020-11-19 17:31:31 +08:00
return $prettyPrinter->prettyPrintFile($stmts);
}
/**
* 输出
*
* @time 2020年11月19日
* @return string
*/
public function output()
{
return FileSystem::put($this->outPath . $this->filename, $this->getContent());
}
/**
* 输出 Path
*
* @time 2020年11月19日
* @param $path
* @return $this
*/
public function path($path)
{
CatchAdmin::makeDirectory($path);
$this->outPath = $path;
return $this;
}
/**
* 设置文件名
*
* @time 2020年11月19日
* @param $name
* @return mixed
*/
public function filename($name)
{
$this->filename = $name;
return $this;
2020-11-19 09:42:54 +08:00
}
}