Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重写/更新php文件中的变量/数组_Php_Json_Symfony_Symfony Components_Symfony Console - Fatal编程技术网

重写/更新php文件中的变量/数组

重写/更新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

在我最近的项目中,我使用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)
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"
          }
        }
      ]