Skip to content

Commit

Permalink
Fix Twig 3.x compatibility by no longer typehinting constructor param…
Browse files Browse the repository at this point in the history
…eter
  • Loading branch information
bobvandevijver committed Sep 2, 2020
1 parent ed87108 commit a0a2564
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions Generator/LatexGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
use BobV\LatexBundle\Exception\LatexException;
use BobV\LatexBundle\Exception\LatexParseException;
use BobV\LatexBundle\Latex\LatexBaseInterface;
use DateTime;
use Exception;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

/**
* Class LatexGenerator
Expand All @@ -30,30 +36,30 @@ class LatexGenerator implements LatexGeneratorInterface
protected $forceRegenerate;
/** @var LatexBaseInterface */
protected $latex;
/** @var \DateTime */
/** @var DateTime */
protected $maxAge;
/** @var string */
protected $pdfLatexLocation;
/** @var string */
protected $outputDir;
/** @var int|float|null */
protected $timeout;
/** @var \Twig_Environment */
/** @var Environment */
protected $twig;

/**
* @param $cacheDir
* @param $env
* @param \Twig_Environment $twig
* @param $maxAge
* @param $pdfLatexLocation
* @param string $cacheDir
* @param string $env
* @param Environment $twig
* @param DateTime $maxAge
* @param string $pdfLatexLocation
*/
public function __construct($cacheDir, $env, \Twig_Environment $twig, $maxAge, $pdfLatexLocation) {
public function __construct($cacheDir, $env, $twig, $maxAge, $pdfLatexLocation) {
$this->cacheDir = $cacheDir;
$this->env = $env;
$this->twig = $twig;
$this->filesystem = new Filesystem();
$this->maxAge = new \DateTime();
$this->maxAge = new DateTime();
$this->maxAge->modify($maxAge);
$this->pdfLatexLocation = $pdfLatexLocation;
$this->forceRegenerate = false;
Expand All @@ -72,9 +78,9 @@ public function __construct($cacheDir, $env, \Twig_Environment $twig, $maxAge, $
*
* @throws ImageNotFoundException
* @throws LatexException
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function createPdfResponse(LatexBaseInterface $latex, bool $download = true) {
$pdfLocation = $this->generate($latex);
Expand All @@ -96,9 +102,9 @@ public function createPdfResponse(LatexBaseInterface $latex, bool $download = tr
* @return BinaryFileResponse
* @throws ImageNotFoundException
* @throws LatexException
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function createTexResponse(LatexBaseInterface $latex, bool $download = true) {
$texLocation = $this->generateLatex($latex);
Expand All @@ -114,15 +120,15 @@ public function createTexResponse(LatexBaseInterface $latex, bool $download = tr
/**
* Compile a LaTeX object into the wanted PDF file
*
* @param \BobV\LatexBundle\Latex\LatexBaseInterface $latex
* @param LatexBaseInterface $latex
*
* @return string Location of the PDF document
*
* @throws ImageNotFoundException
* @throws LatexException
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function generate(LatexBaseInterface $latex) {
$this->latex = $latex;
Expand All @@ -134,15 +140,15 @@ public function generate(LatexBaseInterface $latex) {
/**
* Generates a latex file for the given LaTeX object
*
* @param \BobV\LatexBundle\Latex\LatexBaseInterface $latex
* @param LatexBaseInterface $latex
*
* @return string Location of the generated LaTeX file
*
* @throws ImageNotFoundException
* @throws LatexException
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function generateLatex(LatexBaseInterface $latex = NULL) {

Expand Down Expand Up @@ -171,7 +177,7 @@ public function generateLatex(LatexBaseInterface $latex = NULL) {

try {
$texLocation = $this->writeTexFile($texData, $latex->getFileName());
} catch (\Exception $e) {
} catch (Exception $e) {
if ($e instanceof IOException || $e instanceof LatexException) {
throw $e;
}
Expand Down Expand Up @@ -201,7 +207,7 @@ public function generateLatex(LatexBaseInterface $latex = NULL) {
* @param array $compileOptions Optional compile options for pdflatex
*
* @return string Location of the generated PDF file
* @throws \Symfony\Component\Filesystem\Exception\IOException
* @throws IOException
* @throws LatexException
*/
public function generatePdf($texLocation, array $compileOptions = array()) {
Expand All @@ -213,8 +219,8 @@ public function generatePdf($texLocation, array $compileOptions = array()) {

try {
$pdfLocation = $this->compilePdf($texLocation, $compileOptions);
} catch (\Exception $e) {
if ($e instanceof IOException || $e instanceOf LatexException) {
} catch (Exception $e) {
if ($e instanceof IOException || $e instanceof LatexException) {
throw $e;
}

Expand Down Expand Up @@ -252,7 +258,7 @@ public function setForceRegenerate($forceRegenerate) {
}

/**
* @param \DateTime $maxAge
* @param DateTime $maxAge
*
* @return LatexGenerator
*/
Expand Down Expand Up @@ -299,8 +305,8 @@ protected function checkFilesystem() {
* @param string $texLocation Location of the .tex file
* @param array $compileOptions Optional compile options for pdflatex
*
* @throws \Exception
* @return string
* @throws Exception
*/
protected function compilePdf($texLocation, array $compileOptions = array()) {

Expand Down Expand Up @@ -382,8 +388,8 @@ protected function getCacheBasePath() {
* @param string $texData
* @param $fileName
*
* @throws \Symfony\Component\Filesystem\Exception\IOException
* @return string The location of the saved .tex file
* @throws IOException
*/
protected function writeTexFile($texData, $fileName) {
$this->checkFilesystem();
Expand Down

0 comments on commit a0a2564

Please sign in to comment.