1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/js-sandbox PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the |
11
|
|
|
* LICENSE file that was distributed with this source or visit |
12
|
|
|
* http://opensource.org/licenses/MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Pinepain\JsSandbox\Modules; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use League\Flysystem\FilesystemInterface; |
20
|
|
|
use Pinepain\JsSandbox\Exceptions\NativeException; |
21
|
|
|
use V8\Context; |
22
|
|
|
use V8\FunctionObject; |
23
|
|
|
use V8\Isolate; |
24
|
|
|
use V8\Script; |
25
|
|
|
use V8\ScriptOrigin; |
26
|
|
|
use V8\StringValue; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class SourceModuleBuilder implements SourceModuleBuilderInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var FilesystemInterface |
33
|
|
|
*/ |
34
|
|
|
private $filesystem; |
35
|
|
|
|
36
|
|
|
private $source_wrapper = [ |
37
|
|
|
"(function (exports, require, module, __filename, __dirname) {\n", |
38
|
|
|
// Your module code actually lives in here |
39
|
|
|
"\n});", |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
public function __construct(FilesystemInterface $filesystem) |
43
|
|
|
{ |
44
|
|
|
$this->filesystem = $filesystem; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function build(Isolate $isolate, Context $context, ModuleInterface $module): FunctionObject |
48
|
|
|
{ |
49
|
|
|
$source = $this->source_wrapper[0] . $this->filesystem->read($module->getFilename()) . $this->source_wrapper[1]; |
50
|
|
|
|
51
|
|
|
$source = new StringValue($isolate, $source); |
52
|
|
|
$origin = new ScriptOrigin($module->getFilename(), 1); |
53
|
|
|
$script = new Script($context, $source, $origin); |
54
|
|
|
|
55
|
|
|
$function = $script->run($context); |
56
|
|
|
|
57
|
|
|
if (!($function instanceof FunctionObject)) { |
|
|
|
|
58
|
|
|
// UNLIKELY |
59
|
|
|
throw new NativeException('Malformed module source code'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $function; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.