123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace catcher\generate\build;
- use catcher\CatchAdmin;
- use catcher\facade\FileSystem;
- use catcher\generate\build\classes\Classes;
- use PhpParser\BuilderFactory;
- use PhpParser\PrettyPrinter\Standard;
- class CatchBuild
- {
- protected $astBuilder;
- protected $outPath;
- protected $filename;
- public function __construct()
- {
- $this->astBuilder = app(BuilderFactory::class);
- }
- /**
- * 命名空间
- *
- * @time 2020年11月19日
- * @param string $namespace
- * @return $this
- */
- public function namespace(string $namespace)
- {
- $this->astBuilder = $this->astBuilder->namespace($namespace);
- return $this;
- }
- /**
- * use 方法体
- *
- * @time 2020年11月19日
- * @param $use
- * @return $this
- */
- public function use($use)
- {
- $this->astBuilder->addStmt($use);
- return $this;
- }
- /**
- * class 模版
- *
- * @time 2020年11月19日
- * @param Classes $class
- * @param \Closure $function
- * @return $this
- */
- public function class(Classes $class, \Closure $function)
- {
- $function($class);
- $this->astBuilder->addStmt($class->build());
- return $this;
- }
- /**
- * 条件
- *
- * @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;
- }
- /**
- * 获取内容
- *
- * @time 2020年11月19日
- * @return string
- */
- public function getContent()
- {
- $stmts = array($this->astBuilder->getNode());
- $prettyPrinter = new Standard();
- 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;
- }
- }
|