Replace string read from a file

0

I'm creating a routine using Symfony console to create new controllers from a preexisting template file, but at the time of searching for% set% in this template, class pattern jumps and sometimes ignores some lines;

public function writeWithTemplate($template)
{
    try {
        $this->template = new \SplFileObject(__DIR__ . "/templates/{$template}", "r");

        $pattern = '@' . strtoupper($template) . '@';

        while (!$this->template->eof()) {
            $this->file->fwrite($this->template->fgets());
        }
        return true;
    } catch(\Exception $error) {

        return false;
    }
}

SplFileObject is also a file opened using $this->file , so this function works perfectly, but if I make some comparisons within SplFileObject while is not applied correctly

the template file is:

<?php

namespace App\Controller;

use App\Controller\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class @CONTROLLER@ extends Controller
{

    public function main(Request $request, Response $response)
    {

    }
}

Note that there is a write markup that is where I want to do the override if I do a check within the while;

...
while (!$this->template->eof()) {
    if (strpos($this->template->fgets(), $pattern) !== false) {
       $line = str_replace($pattern, $this->fileName, $this->template->fgets());
    }         
    $line = empty($line) ? $this->template->fgets() : $line;
    $this->file->fwrite($line); 
}

Sometimes the generated file exits only with @CONTROLLER@ (1 or more) and several other nonsense combinations.

    
asked by anonymous 26.12.2016 / 23:15

1 answer

3

You may have more problems with your code, but one thing is obvious:

Here you get a line and see if the pattern is on this line:

if (strpos($this->template->fgets(), $pattern) !== false) {
//                          ^^^^^ leu uma linha

But then you get a new line and try to change the pattern in this new one:

   $line = str_replace($pattern, $this->fileName, $this->template->fgets());
   //                                                              ^^^^^ leu outra

In other words, you are discarding most of the lines with pattern , and trying to change the pattern to some line you do not know if you really need it.

More sense would do this:

$line = $this->template->fgets();
if (strpos($line , $pattern) !== false) {
    $line = str_replace($pattern, $this->fileName, $line );
}

In fact, you need to see if you justify having if . Assuming it's not something to run at all times, that would be enough:

while (!$this->template->eof()) {
    $line = $this->template->fgets();
    if(!empty($line)) $this->file->fwrite(str_replace($pattern, $this->fileName, $line));
}

Whether you want it or not, strpos has some cost. It's a bit smaller than str_replace , but most likely the gain does not pay off in this scenario (I imagine your generated file is the cache for normal use, and you only consume the template when it has some change).

    
27.12.2016 / 00:47