| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- /*
- * This file is part of the php-phantomjs.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace JonnyW\PhantomJs;
- use JonnyW\PhantomJs\Exception\InvalidExecutableException;
- /**
- * PHP PhantomJs
- *
- * @author Jon Wenmoth <contact@jonnyw.me>
- */
- class Engine
- {
- /**
- * Executable path.
- *
- * @var string
- * @access protected
- */
- protected $path;
- /**
- * Debug flag.
- *
- * @var boolean
- * @access protected
- */
- protected $debug;
- /**
- * Cache flag.
- *
- * @var boolean
- * @access protected
- */
- protected $cache;
- /**
- * PhantomJs run options.
- *
- * @var array
- * @access protected
- */
- protected $options;
- /**
- * Log info
- *
- * @var string
- * @access protected
- */
- protected $log;
- /**
- * Internal constructor
- *
- * @access public
- * @return void
- */
- public function __construct()
- {
- $this->path = 'bin/phantomjs';
- $this->options = array();
- $this->debug = false;
- $this->cache = true;
- }
- /**
- * Get PhantomJs run command with
- * loader run options.
- *
- * @access public
- * @return string
- */
- public function getCommand()
- {
- $path = $this->getPath();
- $options = $this->getOptions();
- $this->validateExecutable($path);
- if ($this->cache) {
- array_push($options, '--disk-cache=true');
- }
- if ($this->debug) {
- array_push($options, '--debug=true');
- }
- return sprintf('%s %s', $path, implode(' ', $options));
- }
- /**
- * Set path.
- *
- * @access public
- * @param string $path
- * @return \JonnyW\PhantomJs\Client
- */
- public function setPath($path)
- {
- $this->validateExecutable($path);
- $this->path = $path;
- return $this;
- }
- /**
- * Get path.
- *
- * @access public
- * @return string
- */
- public function getPath()
- {
- return $this->path;
- }
- /**
- * Set PhantomJs run options.
- *
- * @access public
- * @param array $options
- * @return \JonnyW\PhantomJs\Client
- */
- public function setOptions(array $options)
- {
- $this->options = $options;
- return $this;
- }
- /**
- * Get PhantomJs run options.
- *
- * @access public
- * @return array
- */
- public function getOptions()
- {
- return (array) $this->options;
- }
- /**
- * Add single PhantomJs run option.
- *
- * @access public
- * @param string $option
- * @return \JonnyW\PhantomJs\Client
- */
- public function addOption($option)
- {
- if (!in_array($option, $this->options)) {
- $this->options[] = $option;
- }
- return $this;
- }
- /**
- * Debug.
- *
- * @access public
- * @param boolean $doDebug
- * @return \JonnyW\PhantomJs\Client
- */
- public function debug($doDebug)
- {
- $this->debug = $doDebug;
- return $this;
- }
- /**
- * Cache.
- *
- * @access public
- * @param boolean $doCache
- * @return \JonnyW\PhantomJs\Client
- */
- public function cache($doCache)
- {
- $this->cache = $doCache;
- return $this;
- }
- /**
- * Log info.
- *
- * @access public
- * @param string $info
- * @return \JonnyW\PhantomJs\Client
- */
- public function log($info)
- {
- $this->log = $info;
- return $this;
- }
- /**
- * Get log info.
- *
- * @access public
- * @return string
- */
- public function getLog()
- {
- return $this->log;
- }
- /**
- * Clear log info.
- *
- * @access public
- * @return \JonnyW\PhantomJs\Client
- */
- public function clearLog()
- {
- $this->log = '';
- return $this;
- }
- /**
- * Validate execuable file.
- *
- * @access private
- * @param string $file
- * @return boolean
- * @throws \JonnyW\PhantomJs\Exception\InvalidExecutableException
- */
- private function validateExecutable($file)
- {
- if (!file_exists($file) || !is_executable($file)) {
- throw new InvalidExecutableException(sprintf('File does not exist or is not executable: %s', $file));
- }
- return true;
- }
- }
|