|
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
|
4 |
|
public function validate($procedure) |
|
60
|
|
|
{ |
|
61
|
4 |
|
$this->validateSyntax($procedure); |
|
62
|
3 |
|
$this->validateRequirements($procedure); |
|
63
|
|
|
|
|
64
|
3 |
|
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
|
4 |
|
protected function validateSyntax($procedure) |
|
76
|
|
|
{ |
|
77
|
4 |
|
$input = new Input(); |
|
78
|
4 |
|
$output = new Output(); |
|
79
|
|
|
|
|
80
|
4 |
|
$input->set('procedure', $procedure); |
|
81
|
4 |
|
$input->set('engine', $this->engine->toString()); |
|
82
|
|
|
|
|
83
|
4 |
|
$validator = $this->procedureLoader->load('validator'); |
|
84
|
4 |
|
$validator->run($input, $output); |
|
85
|
|
|
|
|
86
|
4 |
|
$errors = $output->get('errors'); |
|
87
|
|
|
|
|
88
|
4 |
|
if (!empty($errors)) { |
|
89
|
1 |
|
throw new SyntaxException('Your procedure failed to compile due to a javascript syntax error', (array) $errors); |
|
90
|
|
|
} |
|
91
|
3 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* validateRequirements function. |
|
95
|
|
|
* |
|
96
|
|
|
* @access protected |
|
97
|
|
|
* @param string $procedure |
|
98
|
|
|
* @return void |
|
99
|
|
|
* @throws \JonnyW\PhantomJs\Exception\RequirementException |
|
100
|
|
|
*/ |
|
101
|
3 |
|
protected function validateRequirements($procedure) |
|
102
|
|
|
{ |
|
103
|
3 |
|
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
|
3 |
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|