Skip to content

Commit

Permalink
Merge pull request #65 from ajanssen/timeout
Browse files Browse the repository at this point in the history
allow to set timeout
  • Loading branch information
docteurklein committed Sep 5, 2013
2 parents f7a1ef5 + 4b25cc2 commit c06b94b
Showing 1 changed file with 51 additions and 36 deletions.
87 changes: 51 additions & 36 deletions src/Knp/Snappy/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ abstract class AbstractGenerator implements GeneratorInterface
private $binary;
private $options = array();
private $env;
private $timeout = false;
private $defaultExtension;

/**
* Constructor
*
* @param string $binary
* @param array $options
* @param string $binary
* @param array $options
*/
public function __construct($binary, array $options = array(), array $env = null)
{
Expand Down Expand Up @@ -64,8 +65,8 @@ public function getDefaultExtension()
* Sets an option. Be aware that option values are NOT validated and that
* it is your responsibility to validate user inputs
*
* @param string $name The option to set
* @param mixed $value The value (NULL to unset)
* @param string $name The option to set
* @param mixed $value The value (NULL to unset)
*/
public function setOption($name, $value)
{
Expand All @@ -76,10 +77,20 @@ public function setOption($name, $value)
$this->options[$name] = $value;
}

/**
* Sets the timeout. Be aware that option only works with symfony
*
* @param integer $timeout The timeout to set
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}

/**
* Sets an array of options
*
* @param array $options An associative array of options as name/value
* @param array $options An associative array of options as name/value
*/
public function setOptions(array $options)
{
Expand Down Expand Up @@ -164,7 +175,7 @@ public function getOutputFromHtml($html, array $options = array())
/**
* Defines the binary
*
* @param string $binary The path/name of the binary
* @param string $binary The path/name of the binary
*/
public function setBinary($binary)
{
Expand All @@ -184,9 +195,9 @@ public function getBinary()
/**
* Returns the command for the given input and output files
*
* @param string $input The input file
* @param string $output The ouput file
* @param array $options An optional array of options that will be used
* @param string $input The input file
* @param string $output The ouput file
* @param array $options An optional array of options that will be used
* only for this command
*
* @return string
Expand All @@ -201,8 +212,8 @@ public function getCommand($input, $output, array $options = array())
/**
* Adds an option
*
* @param string $name The name
* @param mixed $default An optional default value
* @param string $name The name
* @param mixed $default An optional default value
*/
protected function addOption($name, $default = null)
{
Expand All @@ -216,7 +227,7 @@ protected function addOption($name, $default = null)
/**
* Adds an array of options
*
* @param array $options
* @param array $options
*/
protected function addOptions(array $options)
{
Expand All @@ -229,7 +240,7 @@ protected function addOptions(array $options)
* Merges the given array of options to the instance options and returns
* the result options array. It does NOT change the instance options.
*
* @param array $options
* @param array $options
*
* @return array
*/
Expand All @@ -251,8 +262,8 @@ protected function mergeOptions(array $options)
/**
* Checks the specified output
*
* @param string $output The output filename
* @param string $command The generation command
* @param string $output The output filename
* @param string $command The generation command
*
* @throws RuntimeException if the output file generation failed
*/
Expand All @@ -278,10 +289,10 @@ protected function checkOutput($output, $command)
/**
* Checks the process return status
*
* @param int $status The exit status code
* @param string $stdout The stdout content
* @param string $stderr The stderr content
* @param string $command The run command
* @param int $status The exit status code
* @param string $stdout The stdout content
* @param string $stderr The stderr content
* @param string $command The run command
*
* @throws RuntimeException if the output file generation failed
*/
Expand All @@ -302,8 +313,8 @@ protected function checkProcessStatus($status, $stdout, $stderr, $command)
* Creates a temporary file.
* The file is not created if the $content argument is null
*
* @param string $content Optional content for the temporary file
* @param string $extension An optional extension for the filename
* @param string $content Optional content for the temporary file
* @param string $extension An optional extension for the filename
*
* @return string The filename
*/
Expand All @@ -325,10 +336,10 @@ protected function createTemporaryFile($content = null, $extension = null)
/**
* Builds the command string
*
* @param string $binary The binary path/name
* @param string $input Url or file location of the page to process
* @param string $output File location to the image-to-be
* @param array $options An array of options
* @param string $binary The binary path/name
* @param string $input Url or file location of the page to process
* @param string $output File location to the image-to-be
* @param array $options An array of options
*
* @return string
*/
Expand Down Expand Up @@ -373,25 +384,29 @@ protected function buildCommand($binary, $input, $output, array $options = array
* and not an indexed array
*
* @param array $array
*
* @return boolean
*/
protected function isAssociativeArray(array $array)
{
return (bool)count(array_filter(array_keys($array), 'is_string'));
return (bool) count(array_filter(array_keys($array), 'is_string'));
}

/**
* Executes the given command via shell and returns the complete output as
* a string
*
* @param string $command
* @param string $command
*
* @return array(status, stdout, stderr)
*/
protected function executeCommand($command)
{
if (class_exists('Symfony\Component\Process\Process')) {
$process = new \Symfony\Component\Process\Process($command, $this->env);
if ($this->timeout !== false) {
$process->setTimeout($this->timeout);
}
} else {
$process = new Process($command, $this->env);
}
Expand All @@ -408,8 +423,8 @@ protected function executeCommand($command)
/**
* Prepares the specified output
*
* @param string $filename The output filename
* @param boolean $overwrite Whether to overwrite the file if it already
* @param string $filename The output filename
* @param boolean $overwrite Whether to overwrite the file if it already
* exist
*/
protected function prepareOutput($filename, $overwrite)
Expand Down Expand Up @@ -444,7 +459,7 @@ protected function prepareOutput($filename, $overwrite)
/**
* Wrapper for the "file_get_contents" function
*
* @param string $filename
* @param string $filename
*
* @return string
*/
Expand All @@ -456,7 +471,7 @@ protected function getFileContents($filename)
/**
* Wrapper for the "file_exists" function
*
* @param string $filename
* @param string $filename
*
* @return boolean
*/
Expand All @@ -468,7 +483,7 @@ protected function fileExists($filename)
/**
* Wrapper for the "is_file" method
*
* @param string $filename
* @param string $filename
*
* @return boolean
*/
Expand All @@ -480,7 +495,7 @@ protected function isFile($filename)
/**
* Wrapper for the "filesize" function
*
* @param string $filename
* @param string $filename
*
* @return integer or FALSE on failure
*/
Expand All @@ -492,7 +507,7 @@ protected function filesize($filename)
/**
* Wrapper for the "unlink" function
*
* @param string $filename
* @param string $filename
*
* @return boolean
*/
Expand All @@ -504,7 +519,7 @@ protected function unlink($filename)
/**
* Wrapper for the "is_dir" function
*
* @param string $filename
* @param string $filename
*
* @return boolean
*/
Expand All @@ -516,7 +531,7 @@ protected function isDir($filename)
/**
* Wrapper for the mkdir function
*
* @param string $pathname
* @param string $pathname
*
* @return boolean
*/
Expand Down

0 comments on commit c06b94b

Please sign in to comment.