1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* This file is part of the BEAR.SsrModule package. |
6
|
|
|
* |
7
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
8
|
|
|
*/ |
9
|
|
|
namespace BEAR\SsrModule; |
10
|
|
|
|
11
|
|
|
use BEAR\SsrModule\Annotation\Ssr; |
|
|
|
|
12
|
|
|
use Koriym\Baracoa\Baracoa; |
13
|
|
|
use Koriym\Baracoa\BaracoaInterface; |
14
|
|
|
use Koriym\Baracoa\ExceptionHandler; |
15
|
|
|
use Koriym\Baracoa\ExceptionHandlerInterface; |
16
|
|
|
use Ray\Di\AbstractModule; |
17
|
|
|
|
18
|
|
|
class SsrModule extends AbstractModule |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $bundleSrcBasePath; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $bundleSrcBasePath js application directory |
27
|
|
|
* @param AbstractModule|null $module Module |
28
|
|
|
*/ |
29
|
7 |
|
public function __construct(string $bundleSrcBasePath, AbstractModule $module = null) |
30
|
|
|
{ |
31
|
7 |
|
$this->bundleSrcBasePath = $bundleSrcBasePath; |
32
|
7 |
|
parent::__construct($module); |
33
|
7 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
7 |
|
protected function configure() |
39
|
|
|
{ |
40
|
7 |
|
$this->bind(SsrFactoryInterface::class)->to(SsrFactory::class); |
41
|
7 |
|
$this->bind(BaracoaInterface::class)->toConstructor(Baracoa::class, 'bundleSrcBasePath=bundleSrcBasePath'); |
42
|
7 |
|
$this->bind()->annotatedWith('bundleSrcBasePath')->toInstance($this->bundleSrcBasePath); |
43
|
7 |
|
$this->bind(ExceptionHandlerInterface::class)->to(ExceptionHandler::class); |
44
|
7 |
|
$this->bindInterceptor( |
45
|
7 |
|
$this->matcher->any(), |
46
|
7 |
|
$this->matcher->annotatedWith(Ssr::class), |
47
|
7 |
|
[SsrInterceptor::class] |
48
|
|
|
); |
49
|
7 |
|
$this->bind(\V8Js::class)->toConstructor(\V8Js::class, 'object_name=v8js_object_name,variables=v8js_variables,extensions=v8js_extensions,report_uncaught_exceptions=v8_report_uncaught_exceptions,snapshot_blob=v8js_snapshot_blob'); |
50
|
7 |
|
$this->bind()->annotatedWith('v8js_object_name')->toInstance(''); |
51
|
7 |
|
$this->bind()->annotatedWith('v8js_variables')->toInstance([]); |
52
|
7 |
|
$this->bind()->annotatedWith('v8js_extensions')->toInstance([]); |
53
|
7 |
|
$this->bind()->annotatedWith('v8_report_uncaught_exceptions')->toInstance(true); |
54
|
7 |
|
$this->bind()->annotatedWith('v8js_snapshot_blob')->toInstance(''); |
55
|
7 |
|
} |
56
|
|
|
} |
57
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: