4 * Dependency injection configuration
8 * Copyright © 2003-2018 The Galette Team
10 * This file is part of Galette (http://galette.tuxfamily.org).
12 * Galette is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * Galette is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
28 * @author Frédéric Jacquot <unknown@unknow.com>
29 * @author Georges Khaznadar (password encryption, images) <unknown@unknow.com>
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2003-2018 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
36 use Psr\Container\ContainerInterface
;
37 use Galette\Entity\PdfModel
;
38 use Slim\Event\SlimEventManager
;
39 use Slim\Views\SmartyPlugins
;
41 $container = $app->getContainer();
43 // -----------------------------------------------------------------------------
45 // -----------------------------------------------------------------------------
47 $container->set('errorHandler', function ($c) {
48 return new Galette\Handlers\
Error($c->get('view'), true);
51 $container->set('phpErrorHandler', function ($c) {
52 return new Galette\Handlers\
PhpError($c->get('view'), true);
55 $container->set('notFoundHandler', function ($c) {
56 return new Galette\Handlers\
NotFound($c->get('view'));
59 // -----------------------------------------------------------------------------
61 // -----------------------------------------------------------------------------
63 // Register Smarty View helper
64 //TODO: old way - to drop
67 DI\
get('Slim\Views\Smarty')
69 $container->set('Slim\Views\Smarty', function (ContainerInterface
$c) {
70 $view = new \Slim\Views\
Smarty(
71 rtrim(GALETTE_ROOT
. GALETTE_TPL_SUBDIR
, DIRECTORY_SEPARATOR
),
73 'cacheDir' => rtrim(GALETTE_CACHE_DIR
, DIRECTORY_SEPARATOR
),
74 'compileDir' => rtrim(GALETTE_COMPILE_DIR
, DIRECTORY_SEPARATOR
),
76 GALETTE_ROOT
. 'includes/smarty_plugins'
81 // Add Slim specific plugins
82 $basepath = str_replace(
85 $c->get('request')->getUri()->getBasePath()
87 $smartyPlugins = new SmartyPlugins($c->get('router'), $basepath);
88 $view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
89 $view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
91 $smarty = $view->getSmarty();
92 $smarty->inheritance_merge_compiled_includes
= false;
94 $smarty->assign('flash', $c->get('flash'));
96 $smarty->assign('login', $c->get('login'));
97 $smarty->assign('logo', $c->get('logo'));
98 $smarty->assign('tpl', $smarty);
99 $smarty->assign('headers', $c->get('plugins')->getTplHeaders());
100 $smarty->assign('plugin_actions', $c->get('plugins')->getTplAdhActions());
102 'plugin_batch_actions',
103 $c->get('plugins')->getTplAdhBatchActions()
106 'plugin_detailled_actions',
107 $c->get('plugins')->getTplAdhDetailledActions()
109 $smarty->assign('scripts_dir', 'js/');
110 $smarty->assign('jquery_dir', 'js/jquery/');
111 $smarty->assign('jquery_markitup_version', JQUERY_MARKITUP_VERSION
);
112 $smarty->assign('PAGENAME', basename($_SERVER['SCRIPT_NAME']));
113 $smarty->assign('galette_base_path', './');
114 $smarty->assign('GALETTE_VERSION', GALETTE_VERSION
);
115 $smarty->assign('GALETTE_MODE', GALETTE_MODE
);
116 $smarty->assign('GALETTE_DISPLAY_ERRORS', GALETTE_DISPLAY_ERRORS
);
117 $smarty->assign('_CURRENT_THEME_PATH', _CURRENT_THEME_PATH
);
119 /*if ($this->parserConfigDir) {
120 $instance->setConfigDir($this->parserConfigDir);
123 $smarty->assign('template_subdir', GALETTE_THEME
);
124 foreach ($c->get('plugins')->getTplAssignments() as $k => $v) {
125 $smarty->assign($k, $v);
127 /** galette_lang should be removed and languages used instead */
128 $smarty->assign('galette_lang', $c->get('i18n')->getAbbrev());
129 $smarty->assign('galette_lang_name', $c->get('i18n')->getName());
130 $smarty->assign('languages', $c->get('i18n')->getList());
131 $smarty->assign('i18n', $c->get('i18n'));
132 $smarty->assign('plugins', $c->get('plugins'));
133 $smarty->assign('preferences', $c->get('preferences'));
134 $smarty->assign('pref_slogan', $c->get('preferences')->pref_slogan
);
135 $smarty->assign('pref_theme', $c->get('preferences')->pref_theme
);
136 $smarty->assign('pref_statut', $c->get('preferences')->pref_statut
);
138 'pref_editor_enabled',
139 $c->get('preferences')->pref_editor_enabled
141 $smarty->assign('pref_mail_method', $c->get('preferences')->pref_mail_method
);
142 $smarty->assign('existing_mailing', $c->get('session')->mailing
!== null);
143 $smarty->assign('contentcls', null);
144 $smarty->assign('additionnal_html_class', null);
145 $smarty->assign('error_detected', null);
146 $smarty->assign('warning_detected', null);
147 $smarty->assign('success_detected', null);
148 $smarty->assign('require_tree', null);
149 $smarty->assign('html_editor', null);
150 $smarty->assign('require_charts', null);
151 $smarty->assign('require_mass', null);
152 $smarty->assign('autocomplete', null);
153 if ($c->get('login')->isAdmin() && $c->get('preferences')->pref_telemetry_date
) {
154 $now = new \
DateTime();
155 $sent = new \
DateTime($c->get('preferences')->pref_telemetry_date
);
156 $sent->add(new \
DateInterval('P1Y')); // ask to resend telemetry after one year
157 if ($now > $sent && !$_COOKIE['renew_telemetry']) {
158 $smarty->assign('renew_telemetry', true);
162 foreach ($c->get('plugins')->getModules() as $module_id => $module) {
163 $smarty->addTemplateDir(
164 $module['root'] . '/templates/' . $c->get('preferences')->pref_theme
,
172 //TODO: old way - to drop
175 DI\
get('Slim\Flash\Messages')
177 $container->set('Slim\Flash\Messages', DI\autowire
());
179 //TODO: old way - to drop
182 \DI\
get(Galette\Core\Plugins
::class)
185 $container->set(Galette\Core\Plugins
::class, function (ContainerInterface
$c) use ($plugins) {
186 $i18n = $c->get('i18n');
187 $plugins->loadModules($c->get('preferences'), GALETTE_PLUGINS_PATH
, $i18n->getLongID());
191 //TODO: old way - to drop
194 \DI\
get('Galette\Core\I18n')
197 $container->set('Galette\Core\I18n', function (ContainerInterface
$c) {
198 $i18n = $c->get('session')->i18n
;
199 if (!$i18n ||
!$i18n->getId() ||
isset($_GET['ui_pref_lang']) && $_GET['ui_pref_lang']) {
200 $i18n = new Galette\Core\
I18n($_GET['ui_pref_lang'] ??
false);
201 $c->get('session')->i18n
= $i18n;
206 $container->set('l10n', function (ContainerInterface
$c) {
207 $l10n = new Galette\Core\
L10n(
214 //TODO: old way - to drop
217 DI\
get('Galette\Core\Db')
219 $container->set('Galette\Core\Db', DI\autowire
());
221 //TODO: old way - to drop
224 DI\
get('Galette\Core\Preferences')
226 $container->set('Galette\Core\Preferences', DI\autowire
());
228 //TODO: old way - to drop
231 DI\
get('Galette\Core\Login')
233 $container->set('Galette\Core\Login', function (ContainerInterface
$c) {
234 $login = $c->get('session')->login
;
236 $login = new Galette\Core\
Login(
244 /*$container->set('session', function (ContainerInterface $c) {
245 $session = new \RKA\Session();
249 //TODO: old way - to drop
252 DI\
get('Galette\Core\Logo')
254 $container->set('Galette\Core\Logo', DI\autowire
());
256 //TODO: old way - to drop
259 DI\
get('Galette\Core\PrintLogo')
261 $container->set('Galette\Core\PrintLogo', DI\autowire
());
263 //TODO: old way - to drop
266 DI\
get('Galette\Core\History')
268 $container->set('Galette\Core\History', \DI\autowire
());
270 $container->set('acls', function (ContainerInterface
$c) {
271 include_once GALETTE_ROOT
. 'includes/core_acls.php';
274 foreach ($c->get('plugins')->getModules() as $plugin) {
275 $acls[$plugin['route'] . 'Info'] = 'member';
278 //use + to be sure core ACLs are not overrided by plugins ones.
279 $acls = $acls +
$c->get('plugins')->getAcls();
281 //load user defined ACLs
282 if (file_exists(GALETTE_CONFIG_PATH
. 'local_acls.inc.php')) {
283 //use array_merge here, we want $local_acls to override core ones.
284 $acls = array_merge($acls, $local_acls);
290 $container->set('texts_fields', function (ContainerInterface
$c) {
291 include_once GALETTE_ROOT
. 'includes/fields_defs/texts_fields.php';
292 return $texts_fields;
295 $container->set('members_fields', function (ContainerInterface
$c) {
296 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
297 return $members_fields;
300 $container->set('members_form_fields', function (ContainerInterface
$c) {
301 $fields = $c->get('members_fields');
302 foreach ($fields as $k => $field) {
303 if ($field['position'] == -1) {
310 $container->set('members_fields_cats', function (ContainerInterface
$c) {
311 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields_cats.php';
312 return $members_fields_cats;
315 // -----------------------------------------------------------------------------
317 // -----------------------------------------------------------------------------
320 $container->set('logger', function (ContainerInterface
$c) {
321 $settings = $c->get('settings');
322 $logger = new \Monolog\
Logger($settings['logger']['name']);
323 $logger->pushProcessor(new \Monolog\Processor\
UidProcessor());
324 $logger->pushHandler(new \Monolog\Handler\
StreamHandler($settings['logger']['path'], $settings['logger']['level']));
328 //TODO: old way - to drop
331 DI\
get('Galette\Entity\FieldsConfig')
333 $container->set('Galette\Entity\FieldsConfig', function (ContainerInterface
$c) {
334 $fc = new Galette\Entity\
FieldsConfig(
336 Galette\Entity\Adherent
::TABLE
,
337 $c->get('members_fields'),
338 $c->get('members_fields_cats')
343 //TODO: old way - to drop
346 DI\
get('Galette\Entity\ListsConfig')
348 $container->set('Galette\Entity\ListsConfig', function (ContainerInterface
$c) {
349 $fc = new Galette\Entity\
ListsConfig(
351 Galette\Entity\Adherent
::TABLE
,
352 $c->get('members_fields'),
353 $c->get('members_fields_cats')
358 $container->set('cache', function (ContainerInterface
$c) {
360 if (function_exists('wincache_ucache_add')) {
361 //since APCu is not known to work on windows
362 $adapter = 'wincache';
363 } elseif (function_exists('apcu_fetch')) {
366 if ($adapter !== null) {
367 $uuid = $c->get('galette.mode') !== 'INSTALL' ?
$c->get('preferences')->pref_instance_uuid
: '_install';
368 $cache = Laminas\Cache\StorageFactory
::factory([
369 'adapter' => $adapter,
371 'namespace' => str_replace(
372 ['%version', '%uuid'],
373 [GALETTE_VERSION
, $uuid],
374 'galette_%version_%uuid'
383 //TODO: old way - to drop
386 DI\
get('Galette\Core\Translator')
388 $container->set('Galette\Core\Translator', function (ContainerInterface
$c) {
389 $translator = new Galette\Core\
Translator();
391 $domains = ['galette'];
392 foreach ($domains as $domain) {
393 //load translation file for domain
394 $translator->addTranslationFilePattern(
396 GALETTE_ROOT
. '/lang/',
397 '/%s/LC_MESSAGES/' . $domain . '.mo',
401 //check if a local lang file exists and load it
402 $translator->addTranslationFilePattern(
404 GALETTE_ROOT
. '/lang/',
405 $domain . '_%s_local_lang.php',
410 $translator->setLocale($c->get('i18n')->getLongID());
412 !$c->has('galette.mode')
413 ||
$c->get('galette.mode') !== 'INSTALL'
414 && $c->get('galette.mode') !== 'NEED_UPDATE'
416 $translator->setCache($c->get('cache'));
421 // Add Event manager to dependency.
424 DI\
create('Slim\Event\SlimEventManager')
426 'useListenerProvider',
427 DI\
get('Galette\Events\MemberListener')
430 'useListenerProvider',
431 DI\
get('Galette\Events\ContribListener')
437 function (ContainerInterface
$c) {
439 $guard = new \Slim\Csrf\
Guard(
448 $guard->setFailureCallable(function ($request, $response, $next) {
450 'CSRF check has failed',
453 throw new \
RuntimeException(_T('Failed CSRF check!'));
460 //For bad existing globals can be used...
461 global $translator, $i18n;
463 !$container->has('galette.mode')
464 ||
$container->get('galette.mode') !== 'INSTALL'
465 && $container->get('galette.mode') !== 'NEED_UPDATE'
467 global $zdb, $preferences, $login, $hist, $l10n, $emitter;
468 $zdb = $container->get('zdb');
469 $preferences = $container->get('preferences');
470 $login = $container->get('login');
471 $hist = $container->get('history');
472 $l10n = $container->get('l10n');
473 $emitter = $container->get('event_manager');
474 $router = $container->get('router');
476 $i18n = $container->get('i18n');
477 $translator = $container->get('translator');
478 require_once GALETTE_ROOT
. 'includes/i18n.inc.php';