/
DrupalServiceProvider.php
178 lines (152 loc) · 6.42 KB
/
DrupalServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Bangpound\Silex;
use Bangpound\Bridge\Drupal\Bootstrap;
use Bangpound\Bridge\Drupal\BootstrapEvents;
use Bangpound\Bridge\Drupal\Controller\Controller;
use Bangpound\Bridge\Drupal\Event\BootstrapEvent;
use Bangpound\Bridge\Drupal\EventListener\AutoloadListener;
use Bangpound\Bridge\Drupal\EventListener\DefaultPhasesListener;
use Bangpound\Bridge\Drupal\EventListener\ConfigurationListener;
use Bangpound\Bridge\Drupal\EventListener\ExceptionListener;
use Bangpound\Bridge\Drupal\EventListener\FullListener;
use Bangpound\Bridge\Drupal\EventListener\HeaderListener;
use Bangpound\Bridge\Drupal\EventListener\PageCacheListener;
use Bangpound\Bridge\Drupal\EventListener\PageHeaderListener;
use Bangpound\Bridge\Drupal\EventListener\VariablesListener;
use Silex\Application;
use Silex\ControllerCollection;
use Silex\ControllerProviderInterface;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
class DrupalServiceProvider implements ServiceProviderInterface, ControllerProviderInterface
{
/**
* Returns routes to connect to the given application.
*
* @param Application $app An Application instance
*
* @throws \LogicException
* @return ControllerCollection A ControllerCollection instance
*/
public function connect(Application $app)
{
/** @var ControllerCollection $controllers */
$controllers = $app['controllers_factory'];
// Drupal front controller.
$controllers
->match('/{q}', 'drupal.controller:deliverAction')
->assert('q', '[^_].+$')
->value('_legacy', 'drupal')
->convert('q', function ($q) {
return drupal_get_normal_path($q);
})
->convert('router_item', function ($router_item = array(), Request $request) {
$q = $request->get('q');
return menu_get_item($q);
})
;
return $controllers;
}
/**
* Registers services on the given app.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Application $app An Application instance
*/
public function register(Application $app)
{
$app['drupal.bootstrap.class'] = 'Bangpound\\Bridge\\Drupal\\Bootstrap';
$app['drupal.class'] = 'Druplex';
$app['legacy.request_matcher'] = $app->share(
$app->extend('legacy.request_matcher',
function (RequestMatcher $matcher, $c) {
$matcher->matchAttribute('_legacy', 'drupal');
return $matcher;
}
)
);
$app['drupal.listener.header'] = $app->share(
function ($c) {
return new HeaderListener($c['legacy.request_matcher']);
}
);
$app['drupal.bootstrap'] = $app->share(
function () use ($app) {
/** @var Bootstrap $bootstrap */
$bootstrap = new $app['drupal.bootstrap.class']();
$bootstrap->setEventDispatcher($app['dispatcher']);
require_once $app['web_dir'] . '/includes/bootstrap.inc';
drupal_bootstrap(NULL, TRUE, $bootstrap);
return $bootstrap;
}
);
$app->before(
function (Request $request) use ($app) {
$app['drupal.bootstrap'];
define('DRUPAL_ROOT', getcwd());
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$pathinfo = $request->getPathInfo();
// The 'q' variable is pervasive in Drupal, so it's best to just keep
// it even though it's very un-Symfony.
$path = drupal_get_normal_path(substr($pathinfo, 1));
if (variable_get('menu_rebuild_needed', FALSE) || !variable_get('menu_masks', array())) {
menu_rebuild();
}
$original_map = arg(NULL, $path);
$parts = array_slice($original_map, 0, MENU_MAX_PARTS);
$ancestors = menu_get_ancestors($parts);
$router_item = db_query_range('SELECT * FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(':ancestors' => $ancestors))->fetchAssoc();
if ($router_item) {
// Allow modules to alter the router item before it is translated and
// checked for access.
drupal_alter('menu_get_item', $router_item, $path, $original_map);
// The requested path is an unalaised Drupal route.
$request->attributes->add(
array(
'_route' => $router_item['path'],
'_legacy' => 'drupal',
)
);
}
}, 33
);
}
/**
* Bootstraps the application.
*
* This method is called after all services are registered
* and should be used for "dynamic" configuration (whenever
* a service must be requested).
*/
public function boot(Application $app)
{
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
$dispatcher = $app['dispatcher'];
$dispatcher->addSubscriber($app['drupal.listener.header']);
$dispatcher->addSubscriber(new DefaultPhasesListener());
$dispatcher->addSubscriber(new ConfigurationListener());
$dispatcher->addSubscriber(new PageCacheListener());
$dispatcher->addSubscriber(new PageHeaderListener());
$dispatcher->addSubscriber(new FullListener());
$dispatcher->addSubscriber(new ExceptionListener($app['legacy.request_matcher']));
$dispatcher->addSubscriber(new VariablesListener($app['drupal.conf']));
$dispatcher->addSubscriber(new AutoloadListener());
$dispatcher->addListener(BootstrapEvents::FILTER_DATABASE,
function (BootstrapEvent $event) use ($app) {
$app['db.options'] = array(
'pdo' => \Database::getConnection(),
);
}
);
$app['drupal.controller'] = $app->share(
function () {
return new Controller();
}
);
$app->mount('', $this->connect($app));
$app['drupal.class']::setPimple($app);
}
}