重写/更新php文件中的变量/数组
在我最近的项目中,我使用console命令,需要根据linux标准约定执行/运行json中提到的各种操作重写/更新php文件中的变量/数组,php,json,symfony,symfony-components,symfony-console,Php,Json,Symfony,Symfony Components,Symfony Console,在我最近的项目中,我使用console命令,需要根据linux标准约定执行/运行json中提到的各种操作 what:mv(Move),type:file,dir what:mkdir(Make Directory) what:touch(Make File) what:cp(Copy), type:file,dir what:echo (Write into File), type:override,append what:sed (Find and Replace in file) para
what:mv(Move),type:file,dir
what:mkdir(Make Directory)
what:touch(Make File)
what:cp(Copy), type:file,dir
what:echo (Write into File), type:override,append
what:sed (Find and Replace in file)
param模式和linux约定几乎完全相同
当前设置(Mkdir,触摸)
Json模式(数组)
以及它分别针对mkdir和call handle函数,按照what
type(mkdir,touch)遍历所有动作和解析动作类,如MkdirOperation
<?php
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class MkdirOperation extends Operation
{
const ATTR_IN = "in";
const ATTR_NAME = "name";
public function handle()
{
$path = $this->_path();
$this->oIO->comment($path);
if ($this->oFileSystem->isAbsolutePath($path)) {
try {
$this->oFileSystem->mkdir($path);
} catch (IOExceptionInterface $e) {
echo "An error occurred while creating your directory at "
.$e->getPath();
}
$this->oIO->info("Directory created at:".$path);
}
}
private function _path()
{
return $this->oConfig->getBaseDir()
.$this->aParam[self::ATTR_IN].DIRECTORY_SEPARATOR
.$this->aParam[self::ATTR_NAME]
.DIRECTORY_SEPARATOR;
}
}
因此,基本上我想根据特定的规则更新/覆盖我提到的变量/数组,为此,我尝试在json模式中准备规则:
"actions": [
{
"what": "sed",
"in": "path/to/somefile.php",
"find": {
"type": "variable",
"value": "path"
},
"replace": {
"type": "variable",
"value": "__DIR__.'/../vendor/compiled.php';"
}
},{
"what": "put",
"value": "Path\\To\\NameSpace",
"in": "path/to/someotherfile.php",
"find": {
"type": "array",
"at": "files"
}
}
]
我正在使用的组件
- symfony/控制台
- symfony/finder
- Symfony/文件系统
- 建议以这样的方式组织规则集架构,以迭代数组中更新/重写变量或推/拉元素的所有操作并执行操作李>
- 使用php更新特定变量的值以及从数组/子数组中推/拉元素的机制
//somefile.php
$path = "/var/www/ins/"
//someotherfile.php
return [
'files' = [
'Path\\To\\NameSpace',
'Path\\To\\NameSpace'
]
];
"actions": [
{
"what": "sed",
"in": "path/to/somefile.php",
"find": {
"type": "variable",
"value": "path"
},
"replace": {
"type": "variable",
"value": "__DIR__.'/../vendor/compiled.php';"
}
},{
"what": "put",
"value": "Path\\To\\NameSpace",
"in": "path/to/someotherfile.php",
"find": {
"type": "array",
"at": "files"
}
}
]