services.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. use Silex\Application;
  9. use Silex\ServiceProviderInterface;
  10. // Monolog.
  11. if (is_writable($app['sys_temp_path'])) {
  12. /**
  13. * Adding Monolog service provider.
  14. * Examples:
  15. * $app['monolog']->addDebug('Testing the Monolog logging.');
  16. * $app['monolog']->addInfo('Testing the Monolog logging.');
  17. * $app['monolog']->addError('Testing the Monolog logging.');
  18. */
  19. $app->register(
  20. new Silex\Provider\MonologServiceProvider(),
  21. array(
  22. 'monolog.logfile' => $app['chamilo.log'],
  23. 'monolog.name' => 'chamilo',
  24. )
  25. );
  26. }
  27. //Setting HttpCacheService provider in order to use do: $app['http_cache']->run();
  28. /*
  29. $app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
  30. 'http_cache.cache_dir' => $app['http_cache.cache_dir'].'/',
  31. ));*/
  32. // Session provider
  33. //$app->register(new Silex\ProviderSessionServiceProvider());
  34. /*
  35. $app->register(new Silex\Provider\SecurityServiceProvider(), array(
  36. 'security.firewalls' => array(
  37. 'login' => array(
  38. 'pattern' => '^/login$',
  39. 'anonymous' => true
  40. ),
  41. 'secured' => array(
  42. 'http' => true,
  43. 'pattern' => '^/admin/.*$',
  44. 'form' => array(
  45. 'login_path' => '/login',
  46. 'check_path' => '/admin/login_check',
  47. ),
  48. 'logout' => array(
  49. 'path' => '/logout',
  50. 'target' => '/'
  51. ),
  52. 'users' => $app->share(function() use ($app) {
  53. $user = new Entity\User();
  54. $user->setEntityManager($app['orm.em']);
  55. //$user->loadUserByUsername('admin');
  56. return $user;
  57. }),
  58. 'anonymous' => false
  59. ),
  60. 'classic' => array(
  61. 'pattern' => '^/.*$',
  62. 'anonymous' => true
  63. )
  64. ),
  65. 'security.role_hierarchy'=> array(
  66. 'ROLE_ADMIN' => array('ROLE_TEACHER'),
  67. "ROLE_TEACHER" => array('ROLE_STUDENT'),
  68. "ROLE_STUDENT" => array('ROLE_STUDENT'),
  69. "ROLE_ANONYMOUS" => array("ROLE_ANONYMOUS"),
  70. "ROLE_RRHH" => array("ROLE_RRHH"),
  71. "ROLE_QUESTION_MANAGER" => array("ROLE_QUESTION_MANAGER")
  72. )
  73. ));
  74. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  75. $app['security.encoder.digest'] = $app->share(function($app) {
  76. // use the sha1 algorithm
  77. // don't base64 encode the password
  78. // use only 1 iteration
  79. return new MessageDigestPasswordEncoder('sha1', false, 1);
  80. });
  81. */
  82. /*
  83. *
  84. *
  85. $app['security.access_manager'] = $app->share(function($app) {
  86. return new AccessDecisionManager($app['security.voters'], 'unanimous');
  87. });*/
  88. // Setting Controllers as services provider
  89. $app->register(new Silex\Provider\ServiceControllerServiceProvider());
  90. // Validator provider
  91. $app->register(new Silex\Provider\ValidatorServiceProvider());
  92. // Implements Symfony2 translator (needed when using forms in Twig)
  93. $app->register(new Silex\Provider\TranslationServiceProvider(), array(
  94. 'locale' => 'es',
  95. 'locale_fallback' => 'es'
  96. ));
  97. // Handling po files (gettext)
  98. use Symfony\Component\Translation\Loader\PoFileLoader;
  99. use Symfony\Component\Translation\Loader\MoFileLoader;
  100. use Symfony\Component\Finder\Finder;
  101. $app['translator.cache.enabled'] = true;
  102. //$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
  103. /** @var Symfony\Component\Translation\Translator $translator */
  104. /* if ($app['translator.cache.enabled']) {
  105. $locale = $translator->getLocale();
  106. //$phpFileDumper = new Symfony\Component\Translation\Dumper\PhpFileDumper();
  107. $dumper = new Symfony\Component\Translation\Dumper\MoFileDumper();
  108. $catalogue = new Symfony\Component\Translation\MessageCatalogue($locale);
  109. $catalogue->add(array('foo' => 'bar'));
  110. $dumper->dump($catalogue, array('path' => $app['sys_temp_path']));
  111. } else {
  112. $translator->addLoader('pofile', new PoFileLoader());
  113. $finder = new Finder();
  114. $files = $finder->files()->name('*.po')->in(api_get_path(SYS_PATH).'temp/langs/');
  115. //$language = api_get_language_interface();
  116. // @var SplFileInfo $entry
  117. foreach ($files as $entry) {
  118. $domain = basename($entry->getPath());
  119. $code = $entry->getBasename('.po');
  120. //$domain = $entry->getBasename('.inc.po');
  121. //$locale = api_get_language_isocode($language); //'es_ES';
  122. //if ($domain == 'admin') {
  123. // var_dump($entry->getPathname());
  124. //$translator->addResource('pofile', $entry->getPathname(), $code, $domain);
  125. $translator->addResource('pofile', $entry->getPathname(), $code);
  126. //}
  127. //$translator->addResource('pofile', $entry->getPathname(), $locale, 'messages');
  128. }
  129. return $translator;
  130. }
  131. }));
  132. */
  133. // Form provider
  134. $app->register(new Silex\Provider\FormServiceProvider());
  135. // URL generator provider
  136. $app->register(new Silex\Provider\UrlGeneratorServiceProvider());
  137. /*
  138. use Doctrine\Common\Persistence\AbstractManagerRegistry;
  139. class ManagerRegistry extends AbstractManagerRegistry
  140. {
  141. protected $container;
  142. protected function getService($name)
  143. {
  144. return $this->container[$name];
  145. }
  146. protected function resetService($name)
  147. {
  148. unset($this->container[$name]);
  149. }
  150. public function getAliasNamespace($alias)
  151. {
  152. throw new \BadMethodCallException('Namespace aliases not supported.');
  153. }
  154. public function setContainer(Application $container)
  155. {
  156. $this->container = $container;
  157. }
  158. }
  159. $app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions, $app) {
  160. $managerRegistry = new ManagerRegistry(null, array(), array('orm.em'), null, null, $app['orm.proxies_namespace']);
  161. $managerRegistry->setContainer($app);
  162. $extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($managerRegistry);
  163. return $extensions;
  164. }));*/
  165. // Setting Doctrine service provider (DBAL)
  166. if (isset($app['configuration']['main_database'])) {
  167. /* The database connection can be overwritten if you set $_configuration['db.options']
  168. in configuration.php like this : */
  169. $defaultDatabaseOptions = array(
  170. 'db_read' => array(
  171. 'driver' => 'pdo_mysql',
  172. 'host' => $app['configuration']['db_host'],
  173. 'dbname' => $app['configuration']['main_database'],
  174. 'user' => $app['configuration']['db_user'],
  175. 'password' => $app['configuration']['db_password'],
  176. 'charset' => 'utf8',
  177. //'priority' => '1'
  178. ),
  179. 'db_write' => array(
  180. 'driver' => 'pdo_mysql',
  181. 'host' => $app['configuration']['db_host'],
  182. 'dbname' => $app['configuration']['main_database'],
  183. 'user' => $app['configuration']['db_user'],
  184. 'password' => $app['configuration']['db_password'],
  185. 'charset' => 'utf8',
  186. //'priority' => '2'
  187. ),
  188. );
  189. // Could be set in the $_configuration array
  190. if (isset($app['configuration']['db.options'])) {
  191. $defaultDatabaseOptions = $app['configuration']['db.options'];
  192. }
  193. $app->register(
  194. new Silex\Provider\DoctrineServiceProvider(),
  195. array(
  196. 'dbs.options' => $defaultDatabaseOptions
  197. )
  198. );
  199. $mappings = array(
  200. array(
  201. /* If true, only simple notations like @Entity will work.
  202. If false, more advanced notations and aliasing via use will work.
  203. (Example: use Doctrine\ORM\Mapping AS ORM, @ORM\Entity)*/
  204. 'use_simple_annotation_reader' => false,
  205. 'type' => 'annotation',
  206. 'namespace' => 'Entity',
  207. 'path' => api_get_path(INCLUDE_PATH).'Entity',
  208. // 'orm.default_cache' =>
  209. ),
  210. array(
  211. 'use_simple_annotation_reader' => false,
  212. 'type' => 'annotation',
  213. 'namespace' => 'Gedmo',
  214. 'path' => api_get_path(SYS_PATH).'vendors/gedmo/doctrine-extensions/lib/Gedmo',
  215. )
  216. );
  217. // Setting Doctrine ORM
  218. $app->register(
  219. new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider,
  220. array(
  221. 'orm.auto_generate_proxies' => true,
  222. 'orm.proxies_dir' => $app['db.orm.proxies_dir'],
  223. //'orm.proxies_namespace' => '\Doctrine\ORM\Proxy\Proxy',
  224. 'orm.ems.default' => 'db_read',
  225. 'orm.ems.options' => array(
  226. 'db_read' => array(
  227. 'connection' => 'db_read',
  228. 'mappings' => $mappings,
  229. ),
  230. 'db_write' => array(
  231. 'connection' => 'db_write',
  232. 'mappings' => $mappings,
  233. ),
  234. ),
  235. )
  236. );
  237. }
  238. // Setting Twig as a service provider
  239. $app->register(
  240. new Silex\Provider\TwigServiceProvider(),
  241. array(
  242. 'twig.path' => array(
  243. api_get_path(SYS_CODE_PATH).'template', //template folder
  244. api_get_path(SYS_PLUGIN_PATH) //plugin folder
  245. ),
  246. // twitter bootstrap form twig templates
  247. 'twig.form.templates' => array('form_div_layout.html.twig', 'default/form/form_custom_template.tpl'),
  248. 'twig.options' => array(
  249. 'debug' => $app['debug'],
  250. 'charset' => 'utf-8',
  251. 'strict_variables' => false,
  252. 'autoescape' => false,
  253. 'cache' => $app['debug'] ? false : $app['twig.cache.path'],
  254. 'optimizations' => -1, // turn on optimizations with -1
  255. )
  256. )
  257. );
  258. // Setting Twig options
  259. $app['twig'] = $app->share(
  260. $app->extend('twig', function ($twig) {
  261. $twig->addFilter('get_lang', new Twig_Filter_Function('get_lang'));
  262. $twig->addFilter('get_path', new Twig_Filter_Function('api_get_path'));
  263. $twig->addFilter('get_setting', new Twig_Filter_Function('api_get_setting'));
  264. $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
  265. $twig->addFilter('return_message', new Twig_Filter_Function('Display::return_message_and_translate'));
  266. $twig->addFilter('display_page_header', new Twig_Filter_Function('Display::page_header_and_translate'));
  267. $twig->addFilter(
  268. 'display_page_subheader',
  269. new Twig_Filter_Function('Display::page_subheader_and_translate')
  270. );
  271. $twig->addFilter('icon', new Twig_Filter_Function('Template::get_icon_path'));
  272. $twig->addFilter('format_date', new Twig_Filter_Function('Template::format_date'));
  273. return $twig;
  274. })
  275. );
  276. // Developer tools
  277. if (is_writable($app['sys_temp_path'])) {
  278. if ($app['debug'] && $app['show_profiler']) {
  279. // Adding Symfony2 web profiler (memory, time, logs, etc)
  280. $app->register(
  281. $p = new Silex\Provider\WebProfilerServiceProvider(),
  282. array(
  283. 'profiler.cache_dir' => $app['profiler.cache_dir'],
  284. )
  285. );
  286. $app->mount('/_profiler', $p);
  287. // PHP errors for cool kids
  288. $app->register(new Whoops\Provider\Silex\WhoopsServiceProvider);
  289. }
  290. }
  291. // Pagerfanta settings (Pagination using Doctrine2, arrays, etc)
  292. use FranMoreno\Silex\Provider\PagerfantaServiceProvider;
  293. $app->register(new PagerfantaServiceProvider());
  294. // Custom route params see https://github.com/franmomu/silex-pagerfanta-provider/pull/2
  295. //$app['pagerfanta.view.router.name']
  296. //$app['pagerfanta.view.router.params']
  297. $app['pagerfanta.view.options'] = array(
  298. 'routeName' => null,
  299. 'routeParams' => array(),
  300. 'pageParameter' => '[page]',
  301. 'proximity' => 3,
  302. 'next_message' => '&raquo;',
  303. 'prev_message' => '&laquo;',
  304. 'default_view' => 'twitter_bootstrap' // the pagination style
  305. );
  306. // Registering Menu service provider (too gently creating menus with the URLgenerator provider)
  307. $app->register(new \Knp\Menu\Silex\KnpMenuServiceProvider());
  308. // @todo use a app['image_processor'] setting
  309. define('IMAGE_PROCESSOR', 'gd'); // imagick or gd strings
  310. // Setting the Imagine service provider to deal with image transformations used in social group.
  311. $app->register(new Grom\Silex\ImagineServiceProvider(), array(
  312. 'imagine.factory' => 'Gd'
  313. ));
  314. // Prompts Doctrine SQL queries using Monolog.
  315. $app['dbal_logger'] = $app->share(function() {
  316. //return new Doctrine\DBAL\Logging\DebugStack();
  317. });
  318. if ($app['debug']) {
  319. /*$logger = $app['dbal_logger'];
  320. $app['db.config']->setSQLLogger($logger);
  321. $app->after(function() use ($app, $logger) {
  322. // Log all queries as DEBUG.
  323. foreach ($logger->queries as $query) {
  324. $app['monolog']->debug(
  325. $query['sql'],
  326. array(
  327. 'params' => $query['params'],
  328. 'types' => $query['types'],
  329. 'executionMS' => $query['executionMS']
  330. )
  331. );
  332. }
  333. });*/
  334. }
  335. // Email service provider
  336. $app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
  337. 'swiftmailer.options' => array(
  338. 'host' => isset($platform_email['SMTP_HOST']) ? $platform_email['SMTP_HOST'] : null,
  339. 'port' => isset($platform_email['SMTP_PORT']) ? $platform_email['SMTP_PORT'] : null,
  340. 'username' => isset($platform_email['SMTP_USER']) ? $platform_email['SMTP_USER'] : null,
  341. 'password' => isset($platform_email['SMTP_PASS']) ? $platform_email['SMTP_PASS'] : null,
  342. 'encryption' => null,
  343. 'auth_mode' => null
  344. )
  345. ));
  346. // Mailer
  347. $app['mailer'] = $app->share(function ($app) {
  348. return new \Swift_Mailer($app['swiftmailer.transport']);
  349. });
  350. // Assetic service provider.
  351. if ($app['assetic.enabled']) {
  352. $app->register(new SilexAssetic\AsseticServiceProvider(), array(
  353. 'assetic.options' => array(
  354. 'debug' => $app['debug'],
  355. 'auto_dump_assets' => $app['assetic.auto_dump_assets'],
  356. )
  357. ));
  358. // Less filter
  359. $app['assetic.filter_manager'] = $app->share(
  360. $app->extend('assetic.filter_manager', function($fm, $app) {
  361. $fm->set('lessphp', new Assetic\Filter\LessphpFilter());
  362. return $fm;
  363. })
  364. );
  365. $app['assetic.asset_manager'] = $app->share(
  366. $app->extend('assetic.asset_manager', function($am, $app) {
  367. $am->set('styles', new Assetic\Asset\AssetCache(
  368. new Assetic\Asset\GlobAsset(
  369. $app['assetic.input.path_to_css'],
  370. array($app['assetic.filter_manager']->get('lessphp'))
  371. ),
  372. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  373. ));
  374. $am->get('styles')->setTargetPath($app['assetic.output.path_to_css']);
  375. $am->set('scripts', new Assetic\Asset\AssetCache(
  376. new Assetic\Asset\GlobAsset($app['assetic.input.path_to_js']),
  377. new Assetic\Cache\FilesystemCache($app['assetic.path_to_cache'])
  378. ));
  379. $am->get('scripts')->setTargetPath($app['assetic.output.path_to_js']);
  380. return $am;
  381. })
  382. );
  383. }
  384. // Gaufrette service provider (to manage files/dirs) (not used yet)
  385. /*
  386. use Bt51\Silex\Provider\GaufretteServiceProvider\GaufretteServiceProvider;
  387. $app->register(new GaufretteServiceProvider(), array(
  388. 'gaufrette.adapter.class' => 'Local',
  389. 'gaufrette.options' => array(api_get_path(SYS_DATA_PATH))
  390. ));
  391. */
  392. // Use Symfony2 filesystem instead of custom scripts
  393. $app->register(new Neutron\Silex\Provider\FilesystemServiceProvider());
  394. /** Chamilo service provider */
  395. class ChamiloServiceProvider implements ServiceProviderInterface
  396. {
  397. public function register(Application $app)
  398. {
  399. // Template class
  400. $app['template'] = $app->share(function () use ($app) {
  401. $template = new Template($app);
  402. return $template;
  403. });
  404. $app['paths'] = $app->share(function () use ($app) {
  405. return array(
  406. //'root_web' => $app['root_web'],
  407. 'root_sys' => $app['root_sys'],
  408. 'sys_data_path' => $app['sys_data_path'],
  409. 'sys_config_path' => $app['sys_config_path'],
  410. 'sys_temp_path' => $app['sys_temp_path'],
  411. 'sys_log_path' => $app['sys_log_path']
  412. );
  413. });
  414. // Chamilo data filesystem
  415. $app['chamilo.filesystem'] = $app->share(function () use ($app) {
  416. $filesystem = new ChamiloLMS\Component\DataFilesystem\DataFilesystem($app['paths'], $app['filesystem']);
  417. return $filesystem;
  418. });
  419. // Page controller class
  420. $app['page_controller'] = $app->share(function () use ($app) {
  421. $pageController = new PageController($app);
  422. return $pageController;
  423. });
  424. // Mail template generator
  425. $app['mail_generator'] = $app->share(function () use ($app) {
  426. $mailGenerator = new ChamiloLMS\Component\Mail\MailGenerator($app['twig'], $app['mailer']);
  427. return $mailGenerator;
  428. });
  429. // Database
  430. $app['database'] = $app->share(function () use ($app) {
  431. $db = new Database($app['db'], $app['dbs']);
  432. return $db;
  433. });
  434. }
  435. public function boot(Application $app)
  436. {
  437. }
  438. }
  439. // Registering Chamilo service provider
  440. $app->register(new ChamiloServiceProvider(), array());
  441. // Controller as services definitions see
  442. $app['pages.controller'] = $app->share(
  443. function () use ($app) {
  444. return new PagesController($app['pages.repository']);
  445. }
  446. );
  447. $app['index.controller'] = $app->share(
  448. function () use ($app) {
  449. return new ChamiloLMS\Controller\IndexController();
  450. }
  451. );
  452. $app['legacy.controller'] = $app->share(
  453. function () use ($app) {
  454. return new ChamiloLMS\Controller\LegacyController();
  455. }
  456. );
  457. $app['userPortal.controller'] = $app->share(
  458. function () use ($app) {
  459. return new ChamiloLMS\Controller\UserPortalController();
  460. }
  461. );
  462. $app['learnpath.controller'] = $app->share(
  463. function () use ($app) {
  464. return new ChamiloLMS\Controller\LearnpathController();
  465. }
  466. );
  467. $app['course_home.controller'] = $app->share(
  468. function () use ($app) {
  469. return new ChamiloLMS\Controller\CourseHomeController();
  470. }
  471. );
  472. $app['course_home.controller'] = $app->share(
  473. function () use ($app) {
  474. return new ChamiloLMS\Controller\CourseHomeController();
  475. }
  476. );
  477. $app['introduction_tool.controller'] = $app->share(
  478. function () use ($app) {
  479. return new ChamiloLMS\Controller\IntroductionToolController();
  480. }
  481. );
  482. $app['certificate.controller'] = $app->share(
  483. function () use ($app) {
  484. return new ChamiloLMS\Controller\CertificateController();
  485. }
  486. );
  487. $app['user.controller'] = $app->share(
  488. function () use ($app) {
  489. return new ChamiloLMS\Controller\UserController();
  490. }
  491. );
  492. $app['news.controller'] = $app->share(
  493. function () use ($app) {
  494. return new ChamiloLMS\Controller\NewsController();
  495. }
  496. );
  497. $app['editor.controller'] = $app->share(
  498. function () use ($app) {
  499. return new ChamiloLMS\Controller\EditorController();
  500. }
  501. );
  502. $app['question_manager.controller'] = $app->share(
  503. function () use ($app) {
  504. return new ChamiloLMS\Controller\Admin\QuestionManager\QuestionManagerController();
  505. }
  506. );
  507. $app['exercise_manager.controller'] = $app->share(
  508. function () use ($app) {
  509. return new ChamiloLMS\Controller\ExerciseController();
  510. }
  511. );
  512. $app['role.controller'] = $app->share(
  513. function () use ($app) {
  514. return new ChamiloLMS\Controller\Admin\Administrator\RoleController($app);
  515. }
  516. );
  517. $app['model_ajax.controller'] = $app->share(
  518. function () use ($app) {
  519. return new ChamiloLMS\Controller\ModelAjaxController();
  520. }
  521. );