1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace JonnyW\PhantomJs\Procedure; |
10
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Validator\EngineInterface; |
12
|
|
|
use JonnyW\PhantomJs\Exception\SyntaxException; |
13
|
|
|
use JonnyW\PhantomJs\Exception\RequirementException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PHP PhantomJs |
17
|
|
|
* |
18
|
|
|
* @author Jon Wenmoth <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ProcedureValidator implements ProcedureValidatorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Procedure loader. |
24
|
|
|
* |
25
|
|
|
* @var \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface |
26
|
|
|
* @access protected |
27
|
|
|
*/ |
28
|
|
|
protected $procedureLoader; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Validator engine |
32
|
|
|
* |
33
|
|
|
* @var \JonnyW\PhantomJs\Validator\EngineInterface |
34
|
|
|
* @access protected |
35
|
|
|
*/ |
36
|
|
|
protected $engine; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Internal constructor. |
40
|
|
|
* |
41
|
|
|
* @access public |
42
|
|
|
* @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface $procedureLoader |
43
|
|
|
* @param \JonnyW\PhantomJs\Validator\EngineInterface $engine |
44
|
|
|
*/ |
45
|
1 |
|
public function __construct(ProcedureLoaderInterface $procedureLoader, EngineInterface $engine) |
46
|
|
|
{ |
47
|
1 |
|
$this->procedureLoader = $procedureLoader; |
48
|
1 |
|
$this->engine = $engine; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Validate procedure. |
53
|
|
|
* |
54
|
|
|
* @access public |
55
|
|
|
* @param string $procedure |
56
|
|
|
* @return boolean |
57
|
|
|
* @throws \JonnyW\PhantomJs\Exception\ProcedureValidationException |
58
|
|
|
*/ |
59
|
6 |
|
public function validate($procedure) |
60
|
|
|
{ |
61
|
6 |
|
$this->validateSyntax($procedure); |
62
|
5 |
|
$this->validateRequirements($procedure); |
63
|
|
|
|
64
|
5 |
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Validate syntax. |
69
|
|
|
* |
70
|
|
|
* @access protected |
71
|
|
|
* @param string $procedure |
72
|
|
|
* @return void |
73
|
|
|
* @throws \JonnyW\PhantomJs\Exception\SyntaxException |
74
|
|
|
*/ |
75
|
6 |
|
protected function validateSyntax($procedure) |
76
|
|
|
{ |
77
|
6 |
|
$input = new Input(); |
78
|
6 |
|
$output = new Output(); |
79
|
|
|
|
80
|
6 |
|
$input->set('procedure', $procedure); |
81
|
6 |
|
$input->set('engine', $this->engine->toString()); |
82
|
|
|
|
83
|
6 |
|
$validator = $this->procedureLoader->load('validator'); |
84
|
6 |
|
$validator->run($input, $output); |
85
|
|
|
|
86
|
6 |
|
$errors = $output->get('errors'); |
87
|
|
|
|
88
|
6 |
|
if (!empty($errors)) { |
89
|
1 |
|
throw new SyntaxException('Your procedure failed to compile due to a javascript syntax error', (array) $errors); |
90
|
|
|
} |
91
|
5 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* validateRequirements function. |
95
|
|
|
* |
96
|
|
|
* @access protected |
97
|
|
|
* @param string $procedure |
98
|
|
|
* @return void |
99
|
|
|
* @throws \JonnyW\PhantomJs\Exception\RequirementException |
100
|
|
|
*/ |
101
|
5 |
|
protected function validateRequirements($procedure) |
102
|
|
|
{ |
103
|
5 |
|
if (preg_match('/phantom\.exit\(/', $procedure, $matches) !== 1) { |
104
|
|
|
throw new RequirementException('Your procedure must contain a \'phantom.exit(1);\' command to avoid the PhantomJS process hanging'); |
105
|
|
|
} |
106
|
5 |
|
} |
107
|
|
|
} |
108
|
|
|
|