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.