我当前有一个Symfony项目,该项目使用电子邮件基础来创建响应式电子邮件。
Foundation Framework使用命令" NPM Run Build"来转换文件。我尝试使用流程类进行服务解析我的内容,但我必须使用它错误,因为它不会执行" npm run构建"。这是我的错误代码:
<?php
/**
* Created by PhpStorm.
* User: jeremie
* Date: 28/12/17
* Time: 16:59
*/
namespace AcmeOpen4XMLParserBundleServices;
use SymfonyComponentFilesystemFilesystem;
use SymfonyComponentProcessProcess;
/**
* @todo : code this
*/
class FoundationParser
{
protected $foundationLocation;
protected $process;
/**
* FoundationParser constructor.
* @param $foundationLocation
*/
public function __construct($foundationLocation)
{
$this->foundationLocation = $foundationLocation;
$this->process = new Process('npm run build', $this->foundationLocation);
}
/**
* Run npm run build if needed
*/
public function initFoundation()
{
//make sure that 'npm run build' is running and execute it if not
if (!$this->process->isRunning()) {
$this->process->start();
}
}
public function saveFoundationContent($foundationContent, $filename)
{
//save the file in foundation/src/pages
$fileSystem = new Filesystem();
$fileLocation = $this->foundationLocation . '/src/pages/' . $filename;
if (!$fileSystem->exists($fileLocation)) {
$fileSystem->dumpFile($fileLocation, $foundationContent);
$fileSystem->chmod($fileLocation, 0664);
}
}
public function retrieveValidHtml($fileName)
{
$fileLocation = $this->foundationLocation . '/dist/' . $fileName;
while (true) {
try {
$result = file_get_contents($fileLocation);
if ($result !== false){
return $result;
}
} catch (Exception $e) {
}
}
}
}
我这样使用我的服务:
$foundationParser = $this->container->get('open4xmlparser.foundationparser');
$foundationParser->initFoundation();
$foundationParser->saveFoundationContent($foundationContent, 'test.html');
$response = new Response($foundationParser->retrieveValidHtml('test.html'));
$response->headers->set('Content-Type', 'text/html');
$response->send();
它告诉我" test.html"不存在。关于如何做我想做什么?
我最终决定做的是Symfony命令,该命令在无限循环中启动我的程序(永远不应该停止(。而不是使用服务,而是直接在While循环中执行了" NPM Run Build"。
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln([
'',
'Running program',
'===============',
''
]);
$runBuild = new Process('npm run build', $this->getContainer()->getParameter('foundation_location'));
while (true) {
if (!$runBuild->isRunning()){
$output->writeln([
'',
'Executing npm run build',
''
]);
$runBuild->start();
}
}
}
目标文件中的saveFoundationContent
和retrieveValidHtml
中似乎有不同的路径。
// saveFoundationContent()
$fileLocation = $this->foundationLocation . '/src/pages/' . $filename;
// retrieveValidHtml()
$fileLocation = $this->foundationLocation . '/dist/' . $fileName;
显然, retrieveValidHtml()
无法在位置中找到文件。
提示:将子目录路径存储为类变量(或常数(:
class FoundationParser
{
private $subdir = "/dist/"; // or "/src/pages/";
…
public function retrieveValidHtml($fileName)
{
$fileLocation = sprintf("%s%s%s", $this->foundationLocation, $this->subdir, $fileName);
…
}
}