Engine.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs;
  9. use JonnyW\PhantomJs\Exception\InvalidExecutableException;
  10. /**
  11. * PHP PhantomJs.
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class Engine
  16. {
  17. /**
  18. * Executable path.
  19. *
  20. * @var string
  21. */
  22. protected $path;
  23. /**
  24. * Debug flag.
  25. *
  26. * @var bool
  27. */
  28. protected $debug;
  29. /**
  30. * Cache flag.
  31. *
  32. * @var bool
  33. */
  34. protected $cache;
  35. /**
  36. * PhantomJs run options.
  37. *
  38. * @var array
  39. */
  40. protected $options;
  41. /**
  42. * Log info.
  43. *
  44. * @var string
  45. */
  46. protected $log;
  47. /**
  48. * Internal constructor.
  49. */
  50. public function __construct()
  51. {
  52. $this->path = 'bin'.DIRECTORY_SEPARATOR.'phantomjs';
  53. $this->options = array();
  54. $this->debug = false;
  55. $this->cache = true;
  56. }
  57. /**
  58. * Get PhantomJs run command with
  59. * loader run options.
  60. *
  61. * @return string
  62. */
  63. public function getCommand()
  64. {
  65. $path = $this->getPath();
  66. $options = $this->getOptions();
  67. $this->validateExecutable($path);
  68. if ($this->cache) {
  69. array_push($options, '--disk-cache=true');
  70. }
  71. if ($this->debug) {
  72. array_push($options, '--debug=true');
  73. }
  74. return sprintf('%s %s', $path, implode(' ', $options));
  75. }
  76. /**
  77. * Set path.
  78. *
  79. * @param string $path
  80. *
  81. * @return \JonnyW\PhantomJs\Client
  82. */
  83. public function setPath($path)
  84. {
  85. $this->validateExecutable($path);
  86. $this->path = $path;
  87. return $this;
  88. }
  89. /**
  90. * Get path.
  91. *
  92. * @return string
  93. */
  94. public function getPath()
  95. {
  96. return $this->path;
  97. }
  98. /**
  99. * Set PhantomJs run options.
  100. *
  101. * @param array $options
  102. *
  103. * @return \JonnyW\PhantomJs\Client
  104. */
  105. public function setOptions(array $options)
  106. {
  107. $this->options = $options;
  108. return $this;
  109. }
  110. /**
  111. * Get PhantomJs run options.
  112. *
  113. * @return array
  114. */
  115. public function getOptions()
  116. {
  117. return (array) $this->options;
  118. }
  119. /**
  120. * Add single PhantomJs run option.
  121. *
  122. * @param string $option
  123. *
  124. * @return \JonnyW\PhantomJs\Client
  125. */
  126. public function addOption($option)
  127. {
  128. if (!in_array($option, $this->options)) {
  129. $this->options[] = $option;
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Debug.
  135. *
  136. * @param bool $doDebug
  137. *
  138. * @return \JonnyW\PhantomJs\Client
  139. */
  140. public function debug($doDebug)
  141. {
  142. $this->debug = $doDebug;
  143. return $this;
  144. }
  145. /**
  146. * Cache.
  147. *
  148. * @param bool $doCache
  149. *
  150. * @return \JonnyW\PhantomJs\Client
  151. */
  152. public function cache($doCache)
  153. {
  154. $this->cache = $doCache;
  155. return $this;
  156. }
  157. /**
  158. * Log info.
  159. *
  160. * @param string $info
  161. *
  162. * @return \JonnyW\PhantomJs\Client
  163. */
  164. public function log($info)
  165. {
  166. $this->log = $info;
  167. return $this;
  168. }
  169. /**
  170. * Get log info.
  171. *
  172. * @return string
  173. */
  174. public function getLog()
  175. {
  176. return $this->log;
  177. }
  178. /**
  179. * Clear log info.
  180. *
  181. * @return \JonnyW\PhantomJs\Client
  182. */
  183. public function clearLog()
  184. {
  185. $this->log = '';
  186. return $this;
  187. }
  188. /**
  189. * Validate execuable file.
  190. *
  191. * @param string $file
  192. *
  193. * @return bool
  194. *
  195. * @throws \JonnyW\PhantomJs\Exception\InvalidExecutableException
  196. */
  197. private function validateExecutable($file)
  198. {
  199. if (!file_exists($file) || !is_executable($file)) {
  200. throw new InvalidExecutableException(sprintf('File does not exist or is not executable: %s', $file));
  201. }
  202. return true;
  203. }
  204. }