2

In my recent project, working on console command where I need to perform/run various action mentioned in json based on the linux standard convention as

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)

and param schema would be same almost exact to linux convention.

Current SetUp (Mkdir, touch)

Json Schema (Array)

          "actions" => [
                [
                    'what'  => "mkdir",
                    'param' => [
                        'name' => "cache",
                        'in'   => "bootstrap",
                    ],
                ],
                [
                    'what'  => "touch",
                    'param' => [
                        'name' => ".gitignore",
                        'in'   => "bootstrap/cache",
                    ],
                ]
            ],

and its iterate through all action and resolve action class per what type (mkdir,touch) like MkdirOperation for mkdir and call handle functions respectively.

<?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;
    }
}

Requirement:

//somefile.php
$path = "/var/www/ins/"

//someotherfile.php
return [
 'files' = [
     'Path\\To\\NameSpace',
     'Path\\To\\NameSpace'
 ]
];

So, basically I want to update/override my mentioned variable/array according to specific rules, for that purpose, I tried to prepare rules in json schema:

"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"
          }
        }
      ]

The Component I'm using

  • symfony/console
  • symfony/finder
  • Symfony/filesystem

Looking for:

  • Suggestion to organize rules set schema in such manner to iterate through all actions for update/override variable or push/pull element from array and perform action.
  • Mechanism to update the value of specific variable and also push/pull element from array/subarray using php.

If still something unclear from my side let me know. Thanks in advance.

Safoor Safdar
  • 5,516
  • 1
  • 28
  • 32

0 Answers0