services.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file includes all the services that are loaded via the ServiceProviderInterface
  5. *
  6. * @package chamilo.services
  7. */
  8. // Needed to use the "entity" option in symfony forms
  9. use Doctrine\Common\Persistence\AbstractManagerRegistry;
  10. use FranMoreno\Silex\Provider\PagerfantaServiceProvider;
  11. use Silex\Application;
  12. use Silex\ServiceProviderInterface;
  13. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  14. use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
  15. // Flint
  16. $app->register(new Flint\Provider\ConfigServiceProvider());
  17. $app['root_dir'] = $app['root_sys'];
  18. $app->register(new Flint\Provider\RoutingServiceProvider(), array(
  19. 'routing.resource' => $app['sys_config_path'].'routing.yml',
  20. 'routing.options' => array(
  21. //'cache_dir' => $app['debug'] == true ? null : $app['sys_temp_path']
  22. //'cache_dir' => $app['sys_temp_path']
  23. ),
  24. ));
  25. use Knp\Provider\ConsoleServiceProvider;
  26. $app->register(new ConsoleServiceProvider(), array(
  27. 'console.name' => 'Chamilo',
  28. 'console.version' => '1.0.0',
  29. 'console.project_directory' => __DIR__.'/..'
  30. ));
  31. // Monolog.
  32. if (is_writable($app['sys_temp_path'])) {
  33. /**
  34. * Adding Monolog service provider.
  35. * Examples:
  36. * $app['monolog']->addDebug('Testing the Monolog logging.');
  37. * $app['monolog']->addInfo('Testing the Monolog logging.');
  38. * $app['monolog']->addError('Testing the Monolog logging.');
  39. */
  40. if ($app['debug']) {
  41. $app->register(
  42. new Silex\Provider\MonologServiceProvider(),
  43. array(
  44. 'monolog.logfile' => $app['chamilo.log'],
  45. 'monolog.name' => 'chamilo',
  46. )
  47. );
  48. }
  49. }
  50. //Setting HttpCacheService provider in order to use do: $app['http_cache']->run();
  51. /*
  52. $app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
  53. 'http_cache.cache_dir' => $app['http_cache.cache_dir'].'/',
  54. ));*/
  55. class SecurityServiceProvider extends \Silex\Provider\SecurityServiceProvider
  56. {
  57. public function addFakeRoute($method, $pattern, $name)
  58. {
  59. // Dont do anything otherwise the closures will be dumped and that leads to fatal errors.
  60. }
  61. }
  62. $app->register(new SecurityServiceProvider, array(
  63. 'security.firewalls' => array(
  64. 'login' => array(
  65. 'pattern' => '^/login$',
  66. 'anonymous' => true
  67. ),
  68. 'secured' => array(
  69. 'pattern' => '^/.*$',
  70. 'form' => array(
  71. 'login_path' => '/login',
  72. 'check_path' => '/secured/login_check',
  73. 'default_target_path' => '/userportal',
  74. 'username_parameter' => 'username',
  75. 'password_parameter' => 'password',
  76. ),
  77. 'logout' => array(
  78. 'logout_path' => '/secured/logout',
  79. 'target' => '/'
  80. ),
  81. 'users' => $app->share(function() use ($app) {
  82. return $app['orm.em']->getRepository('Entity\User');
  83. }),
  84. 'switch_user' => true,
  85. 'anonymous' => true
  86. )
  87. )
  88. ));
  89. // Registering Password encoder.
  90. $app['security.encoder.digest'] = $app->share(function($app) {
  91. // use the sha1 algorithm
  92. // don't base64 encode the password
  93. // use only 1 iteration
  94. return new MessageDigestPasswordEncoder($app['configuration']['password_encryption'], false, 1);
  95. });
  96. // What to do when login success?
  97. $app['security.authentication.success_handler.secured'] = $app->share(function($app) {
  98. return new ChamiloLMS\Component\Auth\LoginSuccessHandler($app['url_generator'], $app['security']);
  99. });
  100. // What to do when logout?
  101. $app['security.authentication.logout_handler.secured'] = $app->share(function($app) {
  102. return new ChamiloLMS\Component\Auth\LogoutSuccessHandler($app['url_generator'], $app['security']);
  103. });
  104. // What to do when switch user?
  105. $app['security.authentication_listener.switch_user.secured'] = $app->share(function($app) {
  106. return new ChamiloLMS\Component\Auth\LoginListener();
  107. });
  108. // Role hierarchy
  109. $app['security.role_hierarchy'] = array(
  110. // the admin that belongs to the portal #1 can affect all portals
  111. 'ROLE_GLOBAL_ADMIN' => array('ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH'),
  112. // the default admin
  113. 'ROLE_ADMIN' => array(
  114. 'ROLE_QUESTION_MANAGER',
  115. 'ROLE_SESSION_MANAGER',
  116. 'ROLE_TEACHER',
  117. 'ROLE_DIRECTOR',
  118. 'ROLE_JURY_PRESIDENT'
  119. ),
  120. 'ROLE_RRHH' => array('ROLE_TEACHER'),
  121. 'ROLE_TEACHER' => array('ROLE_STUDENT'),
  122. 'ROLE_QUESTION_MANAGER' => array('ROLE_STUDENT', 'ROLE_QUESTION_MANAGER'),
  123. 'ROLE_SESSION_MANAGER' => array('ROLE_STUDENT', 'ROLE_SESSION_MANAGER', 'ROLE_ALLOWED_TO_SWITCH'),
  124. 'ROLE_STUDENT' => array('ROLE_STUDENT'),
  125. 'ROLE_ANONYMOUS' => array('ROLE_ANONYMOUS'),
  126. // Ministerio
  127. 'ROLE_JURY_PRESIDENT' => array('ROLE_JURY_PRESIDENT', 'ROLE_JURY_MEMBER', 'ROLE_JURY_SUBSTITUTE'),
  128. 'ROLE_JURY_SUBSTITUTE' => array('ROLE_JURY_SUBSTITUTE', 'ROLE_JURY_MEMBER'),
  129. 'ROLE_JURY_MEMBER' => array('ROLE_JURY_MEMBER', 'ROLE_STUDENT'),
  130. 'ROLE_DIRECTOR' => array('ROLE_DIRECTOR')
  131. );
  132. // Role rules
  133. $app['security.access_rules'] = array(
  134. array('^/admin/administrator', 'ROLE_ADMIN'),
  135. array('^/main/admin/.*', 'ROLE_ADMIN'),
  136. array('^/admin/questionmanager', 'ROLE_QUESTION_MANAGER'),
  137. array('^/main/auth/inscription.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
  138. array('^/main/auth/lostPassword.php', 'IS_AUTHENTICATED_ANONYMOUSLY'),
  139. array('^/courses/.*/curriculum/category', 'ROLE_TEACHER'),
  140. array('^/courses/.*/curriculum/item', 'ROLE_TEACHER'),
  141. array('^/courses/.*/curriculum/user', 'ROLE_STUDENT'),
  142. array('^/courses/.*/curriculum', 'ROLE_STUDENT'),
  143. // Ministerio routes
  144. array('^/admin/director', 'ROLE_DIRECTOR'),
  145. array('^/admin/jury_president', 'ROLE_JURY_PRESIDENT'),
  146. array('^/admin/jury_member', 'ROLE_JURY_MEMBER') //? jury subsitute??
  147. //array('^.*$', 'ROLE_USER'),
  148. );
  149. // Roles that have an admin toolbar
  150. $app['allow_admin_toolbar'] = array(
  151. 'ROLE_ADMIN',
  152. 'ROLE_QUESTION_MANAGER',
  153. 'ROLE_SESSION_MANAGER',
  154. // Ministerio routes
  155. 'ROLE_DIRECTOR',
  156. 'ROLE_JURY_PRESIDENT',
  157. 'ROLE_JURY_SUBSTITUTE',
  158. 'ROLE_JURY_MEMBER'
  159. );
  160. use SilexOpauth\OpauthExtension;
  161. $strategies = isset($_configuration['strategies']) ? $_configuration['strategies'] : null;
  162. if (!empty($strategies)) {
  163. $app['opauth'] = array(
  164. 'login' => '/auth/login',
  165. 'callback' => '/auth/callback',
  166. 'config' => array(
  167. 'security_salt' => $_configuration['security_key'],
  168. 'Strategy' => array(
  169. $strategies
  170. )
  171. )
  172. );
  173. $app->register(new OpauthExtension());
  174. }
  175. /*
  176. $app['security.access_manager'] = $app->share(function($app) {
  177. return new AccessDecisionManager($app['security.voters'], 'unanimous');
  178. });*/
  179. // Setting Controllers as services provider.
  180. $app->register(new Silex\Provider\ServiceControllerServiceProvider());
  181. // Implements Symfony2 translator.
  182. $app->register(new Silex\Provider\TranslationServiceProvider(), array(
  183. 'locale' => 'en',
  184. 'locale_fallback' => 'en',
  185. 'translator.domains' => array()
  186. ));
  187. // Validator provider.
  188. $app->register(new Silex\Provider\ValidatorServiceProvider());
  189. // Form provider
  190. $app->register(new Silex\Provider\FormServiceProvider(), array(
  191. 'form.secret' => sha1(__DIR__)
  192. ));
  193. // URL generator provider
  194. //$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
  195. class ManagerRegistry extends AbstractManagerRegistry
  196. {
  197. protected $container;
  198. protected function getService($name)
  199. {
  200. return $this->container[$name];
  201. }
  202. protected function resetService($name)
  203. {
  204. unset($this->container[$name]);
  205. }
  206. /**
  207. * @param string $alias
  208. * @return string|void
  209. * @throws BadMethodCallException
  210. */
  211. public function getAliasNamespace($alias)
  212. {
  213. throw new \BadMethodCallException('Namespace aliases not supported.');
  214. }
  215. public function setContainer(Application $container)
  216. {
  217. $this->container = $container;
  218. }
  219. }
  220. // Setting up the Manager registry
  221. $app['manager_registry'] = $app->share(function() use ($app) {
  222. $managerRegistry = new ManagerRegistry(null, array('db'), array('orm.em'), null, null, $app['orm.proxies_namespace']);
  223. $managerRegistry->setContainer($app);
  224. return $managerRegistry;
  225. });
  226. // Needed to use the "entity" option in Symfony forms
  227. $app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions, $app) {
  228. $extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($app['manager_registry']);
  229. return $extensions;
  230. }));
  231. // Needed to use the "UniqueEntity" validator
  232. $app['validator.validator_factory'] = $app->share(function ($app) {
  233. $uniqueValidator = new Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator($app['manager_registry']);
  234. $factory = new ChamiloLMS\Component\Validator\ConstraintValidatorFactory();
  235. $factory->addInstance('doctrine.orm.validator.unique', $uniqueValidator);
  236. return $factory;
  237. });
  238. // Setting Doctrine service provider (DBAL)
  239. if (isset($app['configuration']['main_database'])) {
  240. /* The database connection can be overwritten if you set $_configuration['db.options']
  241. in configuration.php like this : */
  242. $dbPort = isset($app['configuration']['db_port']) ? $app['configuration']['db_port'] : 3306;
  243. $dbDriver = isset($app['configuration']['db_driver']) ? $app['configuration']['db_driver'] : 'pdo_mysql';
  244. $host = $app['configuration']['db_host'];
  245. // Accepts that db_host can have a port part like: localhost:6666;
  246. $hostParts = explode(':', $app['configuration']['db_host']);
  247. if (isset($hostParts[1]) && !empty($hostParts[1])) {
  248. $dbPort = $hostParts[1];
  249. $host = str_replace(':'.$dbPort, '', $app['configuration']['db_host']);
  250. }
  251. $defaultDatabaseOptions = array(
  252. 'db_read' => array(
  253. 'driver' => $dbDriver,
  254. 'host' => $host,
  255. 'port' => $dbPort,
  256. 'dbname' => $app['configuration']['main_database'],
  257. 'user' => $app['configuration']['db_user'],
  258. 'password' => $app['configuration']['db_password'],
  259. 'charset' => 'utf8',
  260. //'priority' => '1'
  261. ),
  262. 'db_write' => array(
  263. 'driver' => $dbDriver,
  264. 'host' => $host,
  265. 'port' => $dbPort,
  266. 'dbname' => $app['configuration']['main_database'],
  267. 'user' => $app['configuration']['db_user'],
  268. 'password' => $app['configuration']['db_password'],
  269. 'charset' => 'utf8',
  270. //'priority' => '2'
  271. ),
  272. );
  273. // Could be set in the $_configuration array
  274. if (isset($app['configuration']['db.options'])) {
  275. $defaultDatabaseOptions = $app['configuration']['db.options'];
  276. }
  277. $app->register(
  278. new Silex\Provider\DoctrineServiceProvider(),
  279. array(
  280. 'dbs.options' => $defaultDatabaseOptions
  281. )
  282. );
  283. $mappings = array(
  284. array(
  285. /* If true, only simple notations like @Entity will work.
  286. If false, more advanced notations and aliasing via use will work.
  287. (Example: use Doctrine\ORM\Mapping AS ORM, @ORM\Entity)*/
  288. 'use_simple_annotation_reader' => false,
  289. 'type' => 'annotation',
  290. 'namespace' => 'Entity',
  291. 'path' => api_get_path(INCLUDE_PATH).'Entity',
  292. // 'orm.default_cache' =>
  293. ),
  294. array(
  295. 'use_simple_annotation_reader' => false,
  296. 'type' => 'annotation',
  297. 'namespace' => 'Gedmo',
  298. 'path' => api_get_path(SYS_PATH).'vendors/gedmo/doctrine-extensions/lib/Gedmo',
  299. )
  300. );
  301. // Setting Doctrine ORM.
  302. $app->register(
  303. new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider,
  304. array(
  305. // Doctrine2 ORM cache
  306. /*'orm.default_cache' => 'apc', // array, apc, xcache, memcache, memcached
  307. 'metadata_cache' => 'apc',
  308. 'result_cache' => 'apc',*/
  309. // Proxies
  310. 'orm.auto_generate_proxies' => true,
  311. 'orm.proxies_dir' => $app['db.orm.proxies_dir'],
  312. 'orm.proxies_namespace' => 'Doctrine\ORM\Proxy\Proxy',
  313. 'orm.ems.default' => 'db_read',
  314. 'orm.ems.options' => array(
  315. 'db_read' => array(
  316. 'connection' => 'db_read',
  317. 'mappings' => $mappings,
  318. ),
  319. 'db_write' => array(
  320. 'connection' => 'db_write',
  321. 'mappings' => $mappings,
  322. ),
  323. ),
  324. )
  325. );
  326. }
  327. // Setting Twig as a service provider.
  328. $app->register(
  329. new Silex\Provider\TwigServiceProvider(),
  330. array(
  331. 'twig.path' => array(
  332. $app['sys_root'].'main/template', //template folder
  333. $app['sys_root'].'plugin' //plugin folder
  334. ),
  335. // twitter bootstrap form twig templates
  336. 'twig.form.templates' => array('form_div_layout.html.twig', 'default/form/form_custom_template.tpl'),
  337. 'twig.options' => array(
  338. 'debug' => $app['debug'],
  339. 'charset' => 'utf-8',
  340. 'strict_variables' => false,
  341. 'autoescape' => false,
  342. 'cache' => $app['debug'] ? false : $app['twig.cache.path'],
  343. 'optimizations' => -1, // turn on optimizations with -1
  344. )
  345. )
  346. );
  347. // Setting Twig options
  348. $app['twig'] = $app->share(
  349. $app->extend('twig', function ($twig) {
  350. $twig->addFilter('get_lang', new Twig_Filter_Function('get_lang'));
  351. $twig->addFilter('get_path', new Twig_Filter_Function('api_get_path'));
  352. $twig->addFilter('get_setting', new Twig_Filter_Function('api_get_setting'));
  353. $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
  354. $twig->addFilter('return_message', new Twig_Filter_Function('Display::return_message_and_translate'));
  355. $twig->addFilter('display_page_header', new Twig_Filter_Function('Display::page_header_and_translate'));
  356. $twig->addFilter(
  357. 'display_page_subheader',
  358. new Twig_Filter_Function('Display::page_subheader_and_translate')
  359. );
  360. $twig->addFilter('icon', new Twig_Filter_Function('Template::get_icon_path'));
  361. $twig->addFilter('format_date', new Twig_Filter_Function('Template::format_date'));
  362. return $twig;
  363. })
  364. );
  365. // Developer tools.
  366. if (is_writable($app['sys_temp_path'])) {
  367. if ($app['show_profiler']) {
  368. // Adding Symfony2 web profiler (memory, time, logs, etc)
  369. $app->register(
  370. $p = new Silex\Provider\WebProfilerServiceProvider(),
  371. array(
  372. 'profiler.cache_dir' => $app['profiler.cache_dir'],
  373. )
  374. );
  375. $app->mount('/_profiler', $p);
  376. // PHP errors for cool kids
  377. //$app->register(new Whoops\Provider\Silex\WhoopsServiceProvider);
  378. }
  379. }
  380. // Pagerfanta settings (Pagination using Doctrine2, arrays, etc)
  381. $app->register(new PagerfantaServiceProvider());
  382. // Custom route params see https://github.com/franmomu/silex-pagerfanta-provider/pull/2
  383. //$app['pagerfanta.view.router.name']
  384. //$app['pagerfanta.view.router.params']
  385. $app['pagerfanta.view.options'] = array(
  386. 'routeName' => null,
  387. 'routeParams' => array(),
  388. 'pageParameter' => '[page]',
  389. 'proximity' => 3,
  390. 'next_message' => '&raquo;',
  391. 'prev_message' => '&laquo;',
  392. 'default_view' => 'twitter_bootstrap' // the pagination style
  393. );
  394. // Registering Menu service provider (too gently creating menus with the URLgenerator provider)
  395. $app->register(new \Knp\Menu\Silex\KnpMenuServiceProvider());
  396. // @todo use a app['image_processor'] setting
  397. define('IMAGE_PROCESSOR', 'gd'); // imagick or gd strings
  398. // Setting the Imagine service provider to deal with image transformations used in social group.
  399. $app->register(new Grom\Silex\ImagineServiceProvider(), array(
  400. 'imagine.factory' => 'Gd'
  401. ));
  402. // Prompts Doctrine SQL queries using Monolog.
  403. $app['dbal_logger'] = $app->share(function() {
  404. //return new Doctrine\DBAL\Logging\DebugStack();
  405. });
  406. if ($app['debug']) {
  407. /*$logger = new Doctrine\DBAL\Logging\DebugStack();
  408. $app['db.config']->setSQLLogger($logger);
  409. $app->after(function() use ($app, $logger) {
  410. // Log all queries as DEBUG.
  411. $app['monolog']->addDebug('---- Doctrine SQL queries ----');
  412. foreach ($logger->queries as $query) {
  413. $app['monolog']->addDebug(
  414. $query['sql'],
  415. array(
  416. 'params' => $query['params'],
  417. 'types' => $query['types'],
  418. 'executionMS' => $query['executionMS']
  419. )
  420. );
  421. }
  422. $app['monolog']->addDebug('---- End Doctrine SQL queries ----');
  423. });*/
  424. }
  425. // Email service provider.
  426. $app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
  427. 'swiftmailer.options' => array(
  428. 'host' => isset($platform_email['SMTP_HOST']) ? $platform_email['SMTP_HOST'] : null,
  429. 'port' => isset($platform_email['SMTP_PORT']) ? $platform_email['SMTP_PORT'] : null,
  430. 'username' => isset($platform_email['SMTP_USER']) ? $platform_email['SMTP_USER'] : null,
  431. 'password' => isset($platform_email['SMTP_PASS']) ? $platform_email['SMTP_PASS'] : null,
  432. 'encryption' => null,
  433. 'auth_mode' => null
  434. )
  435. ));
  436. // Mailer
  437. $app['mailer'] = $app->share(function ($app) {
  438. return new \Swift_Mailer($app['swiftmailer.transport']);
  439. });
  440. // Assetic service provider.
  441. if ($app['assetic.enabled']) {
  442. $app->register(new SilexAssetic\AsseticServiceProvider(), array(
  443. 'assetic.options' => array(
  444. 'debug' => $app['debug'],
  445. 'auto_dump_assets' => $app['assetic.auto_dump_assets'],
  446. )
  447. ));
  448. // Less filter
  449. $app['assetic.filter_manager'] = $app->share(
  450. $app->extend('assetic.filter_manager', function($fm, $app) {
  451. $fm->set('lessphp', new Assetic\Filter\LessphpFilter());
  452. return $fm;
  453. })
  454. );
  455. $app['assetic.asset_manager'] = $app->share(
  456. $app->extend('assetic.asset_manager', function($am, $app) {
  457. $am->set('styles', new Assetic\Asset\AssetCache(
  458. new Assetic\Asset\GlobAsset(
  459. $app['assetic.input.path_to_css'],
  460. array($app['assetic.filter_manager']->get('lessphp'))
  461. ),
  462. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  463. ));
  464. $am->get('styles')->setTargetPath($app['assetic.output.path_to_css']);
  465. $am->set('scripts', new Assetic\Asset\AssetCache(
  466. new Assetic\Asset\GlobAsset($app['assetic.input.path_to_js']),
  467. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  468. ));
  469. $am->get('scripts')->setTargetPath($app['assetic.output.path_to_js']);
  470. return $am;
  471. })
  472. );
  473. }
  474. // Gaufrette service provider (to manage files/dirs) (not used yet)
  475. /*
  476. use Bt51\Silex\Provider\GaufretteServiceProvider\GaufretteServiceProvider;
  477. $app->register(new GaufretteServiceProvider(), array(
  478. 'gaufrette.adapter.class' => 'Local',
  479. 'gaufrette.options' => array(api_get_path(SYS_DATA_PATH))
  480. ));
  481. */
  482. // Use Symfony2 filesystem instead of custom scripts
  483. $app->register(new Neutron\Silex\Provider\FilesystemServiceProvider());
  484. /** Chamilo service provider. */
  485. class ChamiloServiceProvider implements ServiceProviderInterface
  486. {
  487. public function register(Application $app)
  488. {
  489. // Database.
  490. $app['database'] = $app->share(function () use ($app) {
  491. $db = new Database($app['db'], $app['dbs']);
  492. return $db;
  493. });
  494. $database = $app['database'];
  495. // Template class
  496. $app['template'] = $app->share(function () use ($app) {
  497. $template = new Template($app, $app['database'], $app['security']);
  498. return $template;
  499. });
  500. // Paths
  501. $app['paths'] = $app->share(function () use ($app) {
  502. return array(
  503. //'root_web' => $app['root_web'],
  504. 'root_sys' => $app['root_sys'],
  505. 'sys_root' => $app['root_sys'], // just an alias
  506. 'sys_data_path' => $app['sys_data_path'],
  507. 'sys_config_path' => $app['sys_config_path'],
  508. 'sys_temp_path' => $app['sys_temp_path'],
  509. 'sys_log_path' => $app['sys_log_path']
  510. );
  511. });
  512. // Chamilo data filesystem.
  513. $app['chamilo.filesystem'] = $app->share(function () use ($app) {
  514. $filesystem = new ChamiloLMS\Component\DataFilesystem\DataFilesystem($app['paths'], $app['filesystem']);
  515. return $filesystem;
  516. });
  517. // Page controller class.
  518. $app['page_controller'] = $app->share(function () use ($app) {
  519. $pageController = new PageController($app);
  520. return $pageController;
  521. });
  522. // Mail template generator.
  523. $app['mail_generator'] = $app->share(function () use ($app) {
  524. $mailGenerator = new ChamiloLMS\Component\Mail\MailGenerator($app['twig'], $app['mailer']);
  525. return $mailGenerator;
  526. });
  527. // Setting up name conventions
  528. $conventions = require_once $app['sys_root'].'main/inc/lib/internationalization_database/name_order_conventions.php';
  529. if (isset($configuration['name_order_conventions']) && !empty($configuration['name_order_conventions'])) {
  530. $conventions = array_merge($conventions, $configuration['name_order_conventions']);
  531. }
  532. $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
  533. $replacement1 = array('%F', '%L', '%T');
  534. $search2 = array('first_name', 'last_name', 'title');
  535. $replacement2 = array('%f', '%l', '%t');
  536. $keyConventions = array_keys($conventions);
  537. foreach ($keyConventions as $key) {
  538. $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
  539. $conventions[$key]['format'] = _api_validate_person_name_format(
  540. _api_clean_person_name(
  541. str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))
  542. )
  543. );
  544. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  545. }
  546. $app['name_order_conventions'] = $conventions;
  547. }
  548. /**
  549. * @param Application $app
  550. */
  551. public function boot(Application $app)
  552. {
  553. }
  554. }
  555. // Registering Chamilo service provider.
  556. $app->register(new ChamiloServiceProvider(), array());
  557. // Controller as services definitions.
  558. $app['pages.controller'] = $app->share(
  559. function () use ($app) {
  560. return new PagesController($app['pages.repository']);
  561. }
  562. );
  563. $app['index.controller'] = $app->share(
  564. function () use ($app) {
  565. $controller = new ChamiloLMS\Controller\IndexController($app);
  566. return $controller;
  567. }
  568. );
  569. $app['legacy.controller'] = $app->share(
  570. function () use ($app) {
  571. return new ChamiloLMS\Controller\LegacyController($app);
  572. }
  573. );
  574. $app['userPortal.controller'] = $app->share(
  575. function () use ($app) {
  576. return new ChamiloLMS\Controller\UserPortalController($app);
  577. }
  578. );
  579. $app['learnpath.controller'] = $app->share(
  580. function () use ($app) {
  581. return new ChamiloLMS\Controller\LearnpathController();
  582. }
  583. );
  584. $app['course_home.controller'] = $app->share(
  585. function () use ($app) {
  586. return new ChamiloLMS\Controller\CourseHomeController();
  587. }
  588. );
  589. $app['course_home.controller'] = $app->share(
  590. function () use ($app) {
  591. return new ChamiloLMS\Controller\CourseHomeController();
  592. }
  593. );
  594. $app['introduction_tool.controller'] = $app->share(
  595. function () use ($app) {
  596. return new ChamiloLMS\Controller\IntroductionToolController();
  597. }
  598. );
  599. $app['certificate.controller'] = $app->share(
  600. function () use ($app) {
  601. return new ChamiloLMS\Controller\CertificateController();
  602. }
  603. );
  604. $app['user.controller'] = $app->share(
  605. function () use ($app) {
  606. return new ChamiloLMS\Controller\UserController();
  607. }
  608. );
  609. $app['news.controller'] = $app->share(
  610. function () use ($app) {
  611. return new ChamiloLMS\Controller\NewsController();
  612. }
  613. );
  614. $app['editor.controller'] = $app->share(
  615. function () use ($app) {
  616. return new ChamiloLMS\Controller\EditorController();
  617. }
  618. );
  619. $app['question_manager.controller'] = $app->share(
  620. function () use ($app) {
  621. return new ChamiloLMS\Controller\Admin\QuestionManager\QuestionManagerController();
  622. }
  623. );
  624. $app['exercise_manager.controller'] = $app->share(
  625. function () use ($app) {
  626. return new ChamiloLMS\Controller\ExerciseController($app);
  627. }
  628. );
  629. $app['admin.controller'] = $app->share(
  630. function () use ($app) {
  631. return new ChamiloLMS\Controller\Admin\AdminController($app);
  632. }
  633. );
  634. $app['role.controller'] = $app->share(
  635. function () use ($app) {
  636. return new ChamiloLMS\Controller\Admin\Administrator\RoleController($app);
  637. }
  638. );
  639. $app['question_score.controller'] = $app->share(
  640. function () use ($app) {
  641. return new ChamiloLMS\Controller\Admin\Administrator\QuestionScoreController($app);
  642. }
  643. );
  644. $app['question_score_name.controller'] = $app->share(
  645. function () use ($app) {
  646. return new ChamiloLMS\Controller\Admin\Administrator\QuestionScoreNameController($app);
  647. }
  648. );
  649. $app['model_ajax.controller'] = $app->share(
  650. function () use ($app) {
  651. return new ChamiloLMS\Controller\ModelAjaxController();
  652. }
  653. );
  654. // Curriculum tool
  655. $app['curriculum.controller'] = $app->share(
  656. function () use ($app) {
  657. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumController($app);
  658. }
  659. );
  660. $app['curriculum_category.controller'] = $app->share(
  661. function () use ($app) {
  662. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumCategoryController($app);
  663. }
  664. );
  665. $app['curriculum_item.controller'] = $app->share(
  666. function () use ($app) {
  667. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumItemController($app);
  668. }
  669. );
  670. $app['curriculum_user.controller'] = $app->share(
  671. function () use ($app) {
  672. return new ChamiloLMS\Controller\Tool\Curriculum\CurriculumUserController($app);
  673. }
  674. );
  675. $app['upgrade.controller'] = $app->share(
  676. function () use ($app) {
  677. return new ChamiloLMS\Controller\Admin\Administrator\UpgradeController($app);
  678. }
  679. );
  680. // Ministerio
  681. $app['branch.controller'] = $app->share(
  682. function () use ($app) {
  683. return new ChamiloLMS\Controller\Admin\Administrator\BranchController($app);
  684. }
  685. );
  686. $app['branch_director.controller'] = $app->share(
  687. function () use ($app) {
  688. return new ChamiloLMS\Controller\Admin\Director\BranchDirectorController($app);
  689. }
  690. );
  691. $app['jury.controller'] = $app->share(
  692. function () use ($app) {
  693. return new ChamiloLMS\Controller\Admin\Administrator\JuryController($app);
  694. }
  695. );
  696. $app['jury_president.controller'] = $app->share(
  697. function () use ($app) {
  698. return new ChamiloLMS\Controller\Admin\JuryPresident\JuryPresidentController($app);
  699. }
  700. );
  701. $app['jury_member.controller'] = $app->share(
  702. function () use ($app) {
  703. return new ChamiloLMS\Controller\Admin\JuryMember\JuryMemberController($app);
  704. }
  705. );
  706. $app['exercise_distribution.controller'] = $app->share(
  707. function () use ($app) {
  708. return new ChamiloLMS\Controller\Admin\QuestionManager\ExerciseDistributionController($app);
  709. }
  710. );