services.php 28 KB

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