Skip to content

Instantly share code, notes, and snippets.

@programgames
Created November 26, 2021 13:22
Show Gist options
  • Save programgames/f84e6e937c77ae14ef5aeaf6cfdb8826 to your computer and use it in GitHub Desktop.
Save programgames/f84e6e937c77ae14ef5aeaf6cfdb8826 to your computer and use it in GitHub Desktop.
Export EmailTemplateCommand
class ExportEmailTemplatesCommand extends Command
{
/** @var string */
protected static $defaultName = 'app:export:email-templates';
private ManagerRegistry $doctrine;
private FileManager $fileManager;
/**
* ExportEmailTemplatesCommand constructor.
* @param ManagerRegistry $doctrine
* @param FileManager $fileManager
*/
public function __construct(ManagerRegistry $doctrine, FileManager $fileManager)
{
$this->doctrine = $doctrine;
$this->fileManager = $fileManager;
parent::__construct();
}
protected function configure()
{
$this->setName(self::$defaultName)
->setDescription('Export all email templates');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$templates = $this->doctrine->getRepository(EmailTemplate::class)->findAll();
/** @var EmailTemplate $emailTemplate */
foreach ($templates as $emailTemplate) {
$content = '';
$content .= sprintf('@name = %s', $emailTemplate->getName()) . PHP_EOL;
if($emailTemplate->getEntityName()) {
$content .= sprintf('@entityName = %s', $emailTemplate->getEntityName()) . PHP_EOL;
}
$content .= sprintf('@subject = %s', $emailTemplate->getSubject()) . PHP_EOL;
$content .= sprintf('@isSystem = %s', (int)$emailTemplate->getIsSystem()) . PHP_EOL;
$content .= sprintf('@isEditable = %s', (int)$emailTemplate->getIsEditable()) . PHP_EOL;
$content .= sprintf('%s%s', PHP_EOL, $emailTemplate->getContent());
if($emailTemplate->getEntityName()) {
$refClass = new \ReflectionClass($emailTemplate->getEntityName());
$this->fileManager->getFileSystem()->write(
sprintf('%s%s/%s%s','emails/',$refClass->getShortName(), $emailTemplate->getName(), '.html.twig'),
$content,
true
);
} else {
$this->fileManager->getFileSystem()->write(
sprintf('%s%s/%s%s','emails/','Others', $emailTemplate->getName(), '.html.twig'),
$content,
true
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment