Skip to content

Commit aafd77f

Browse files
committed
Added super simple Drupal adapter.
Fixed also some double autoloading issues.
1 parent 8216582 commit aafd77f

File tree

4 files changed

+101
-38
lines changed

4 files changed

+101
-38
lines changed

Bootstraps/Drupal.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace PHPPM\Bootstraps;
4+
5+
use Drupal\Core\DrupalKernel;
6+
use Drupal\Core\Site\Settings;
7+
use Symfony\Component\HttpFoundation\Request;
8+
9+
/**
10+
* A PHP-PM bootstrap for the Drupal framework.
11+
*
12+
* @see \PHPPM\Bootstraps\Symfony
13+
* @see \PHPPM\Bridges\HttpKernel
14+
*/
15+
class Drupal implements BootstrapInterface
16+
{
17+
/**
18+
* The PHP environment in which to bootstrap (such as 'dev' or 'production').
19+
*
20+
* @var string|null
21+
*/
22+
protected $appenv;
23+
24+
/**
25+
* @var boolean
26+
*/
27+
protected $debug;
28+
29+
/**
30+
* Instantiate the bootstrap, storing the $appenv.
31+
*/
32+
public function __construct($appenv, $debug)
33+
{
34+
$this->appenv = $appenv;
35+
$this->debug = $debug;
36+
}
37+
38+
/**
39+
* @return string
40+
*/
41+
public function getStaticDirectory() {
42+
return './';
43+
}
44+
45+
/**
46+
* Create a Drupal application.
47+
*/
48+
public function getApplication()
49+
{
50+
//load drupals autoload.php, so their classes are available
51+
$autoloader = require './vendor/autoload.php';
52+
53+
$sitePath = 'sites/default';
54+
55+
Settings::initialize('./', $sitePath, $autoloader);
56+
57+
$app = new DrupalKernel($this->appenv, $autoloader);
58+
$app->setSitePath($sitePath);
59+
60+
return $app;
61+
}
62+
}

Bootstraps/Laravel.php

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public function __construct($appenv, $debug)
3737
putenv("APP_DEBUG=" . ($debug ? 'TRUE' : 'FALSE'));
3838
}
3939

40+
/**
41+
* @return string
42+
*/
43+
public function getStaticDirectory() {
44+
return 'public/';
45+
}
46+
4047
/**
4148
* Create a Laravel application
4249
*/

Bootstraps/Symfony.php

+13-22
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,27 @@ public function __construct($appenv, $debug)
2727
$this->debug = $debug;
2828
}
2929

30+
/**
31+
* @return string
32+
*/
33+
public function getStaticDirectory() {
34+
return 'web/';
35+
}
36+
3037
/**
3138
* Create a Symfony application
3239
* @return SymfonyAppKernel
3340
*/
3441
public function getApplication()
3542
{
36-
if (file_exists('./app/AppKernel.php')) {
37-
require_once './app/AppKernel.php';
43+
// include applications autoload
44+
$appAutoLoader = './app/autoload.php';
45+
if (file_exists($appAutoLoader)) {
46+
require $appAutoLoader;
47+
} else {
48+
require './vendor/autoload.php';
3849
}
3950

40-
$this->includeAutoload();
41-
4251
$app = new SymfonyAppKernel($this->appenv, $this->debug); //which extends \AppKernel
4352
$app->loadClassCache();
4453
$app->boot();
@@ -50,22 +59,4 @@ public function getApplication()
5059

5160
return $app;
5261
}
53-
54-
/**
55-
* Includes the autoload file from the app directory, if available.
56-
*
57-
* The Symfony standard edition configures the annotation class loading
58-
* in that file.
59-
* The alternative bootstrap.php.cache cannot be included as that leads
60-
* to "Cannot redeclare class" error, when starting php-pm.
61-
*/
62-
protected function includeAutoload()
63-
{
64-
$info = new \ReflectionClass('AppKernel');
65-
$appDir = dirname($info->getFileName());
66-
$symfonyAutoload = $appDir . '/autoload.php';
67-
if (is_file($symfonyAutoload)) {
68-
require_once $symfonyAutoload;
69-
}
70-
}
7162
}

Bridges/HttpKernel.php

+19-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class HttpKernel implements BridgeInterface
2020
*/
2121
protected $application;
2222

23+
/**
24+
* @var BootstrapInterface
25+
*/
26+
protected $bootstrap;
27+
2328
/**
2429
* Bootstrap an application implementing the HttpKernelInterface.
2530
*
@@ -37,18 +42,12 @@ class HttpKernel implements BridgeInterface
3742
*/
3843
public function bootstrap($appBootstrap, $appenv, $debug)
3944
{
40-
// include applications autoload
41-
$autoloader = './vendor/autoload.php';
42-
if (file_exists($autoloader)) {
43-
require_once $autoloader;
44-
}
45-
4645
$appBootstrap = $this->normalizeAppBootstrap($appBootstrap);
4746

48-
$bootstrap = new $appBootstrap($appenv, $debug);
47+
$this->bootstrap = new $appBootstrap($appenv, $debug);
4948

50-
if ($bootstrap instanceof BootstrapInterface) {
51-
$this->application = $bootstrap->getApplication();
49+
if ($this->bootstrap instanceof BootstrapInterface) {
50+
$this->application = $this->bootstrap->getApplication();
5251
}
5352
}
5453

@@ -57,7 +56,7 @@ public function bootstrap($appBootstrap, $appenv, $debug)
5756
*/
5857
public function getStaticDirectory()
5958
{
60-
return 'web/';
59+
return $this->bootstrap->getStaticDirectory();
6160
}
6261

6362
/**
@@ -186,13 +185,17 @@ protected static function mapResponse(HttpResponse $reactResponse, SymfonyRespon
186185
protected function normalizeAppBootstrap($appBootstrap)
187186
{
188187
$appBootstrap = str_replace('\\\\', '\\', $appBootstrap);
189-
if (false === class_exists($appBootstrap)) {
190-
$appBootstrap = '\\' . $appBootstrap;
191-
if (false === class_exists($appBootstrap)) {
192-
throw new \RuntimeException('Could not find bootstrap class ' . $appBootstrap);
188+
189+
$bootstraps = [
190+
$appBootstrap,
191+
'\\' . $appBootstrap,
192+
'\\PHPPM\Bootstraps\\' . ucfirst($appBootstrap)
193+
];
194+
195+
foreach ($bootstraps as $class) {
196+
if (class_exists($class)) {
197+
return $class;
193198
}
194-
return $appBootstrap;
195199
}
196-
return $appBootstrap;
197200
}
198201
}

0 commit comments

Comments
 (0)