Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Application.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace GitList;
  3. use Silex\Application as SilexApplication;
  4. use Silex\Provider\TwigServiceProvider;
  5. use Silex\Provider\UrlGeneratorServiceProvider;
  6. use GitList\Provider\GitServiceProvider;
  7. use GitList\Provider\RepositoryUtilServiceProvider;
  8. use GitList\Provider\ViewUtilServiceProvider;
  9. use GitList\Provider\RoutingUtilServiceProvider;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. /**
  12. * GitList application.
  13. */
  14. class Application extends SilexApplication
  15. {
  16. protected $path;
  17. /**
  18. * Constructor initialize services.
  19. *
  20. * @param Config $config
  21. * @param string $root Base path of the application files (views, cache)
  22. */
  23. public function __construct(Config $config, $root = null)
  24. {
  25. parent::__construct();
  26. $app = $this;
  27. $this->path = realpath($root);
  28. $this['debug'] = $config->get('app', 'debug');
  29. $this['date.format'] = $config->get('date', 'format') ? $config->get('date', 'format') : 'd/m/Y H:i:s';
  30. $this['theme'] = $config->get('app', 'theme') ? $config->get('app', 'theme') : 'default';
  31. $this['filetypes'] = $config->getSection('filetypes');
  32. $this['binary_filetypes'] = $config->getSection('binary_filetypes');
  33. $this['cache.archives'] = $this->getCachePath() . 'archives';
  34. // Register services
  35. $this->register(new TwigServiceProvider(), array(
  36. 'twig.path' => array($this->getThemePath($this['theme']), $this->getThemePath('default')),
  37. 'twig.options' => $config->get('app', 'cache') ?
  38. array('cache' => $this->getCachePath() . 'views') : array(),
  39. ));
  40. $repositories = $config->get('git', 'repositories');
  41. $this->register(new GitServiceProvider(), array(
  42. 'git.client' => $config->get('git', 'client'),
  43. 'git.repos' => $repositories,
  44. 'ini.file' => "config.ini",
  45. 'git.hidden' => $config->get('git', 'hidden') ?
  46. $config->get('git', 'hidden') : array(),
  47. 'git.default_branch' => $config->get('git', 'default_branch') ?
  48. $config->get('git', 'default_branch') : 'master',
  49. ));
  50. $this->register(new ViewUtilServiceProvider());
  51. $this->register(new RepositoryUtilServiceProvider());
  52. $this->register(new UrlGeneratorServiceProvider());
  53. $this->register(new RoutingUtilServiceProvider());
  54. $this['twig'] = $this->share($this->extend('twig', function ($twig, $app) {
  55. $twig->addFilter(new \Twig_SimpleFilter('htmlentities', 'htmlentities'));
  56. $twig->addFilter(new \Twig_SimpleFilter('md5', 'md5'));
  57. $twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate')));
  58. return $twig;
  59. }));
  60. $this['escaper.argument'] = $this->share(function() {
  61. return new Escaper\ArgumentEscaper();
  62. });
  63. // Handle errors
  64. $this->error(function (\Exception $e, $code) use ($app) {
  65. if ($app['debug']) {
  66. return;
  67. }
  68. return $app['twig']->render('error.twig', array(
  69. 'message' => $e->getMessage(),
  70. ));
  71. });
  72. $this->finish(function () use ($app, $config) {
  73. if (!$config->get('app', 'cache')) {
  74. $fs = new Filesystem();
  75. $fs->remove($app['cache.archives']);
  76. }
  77. });
  78. }
  79. public function formatDate($date)
  80. {
  81. return $date->format($this['date.format']);
  82. }
  83. public function getPath()
  84. {
  85. return $this->path . DIRECTORY_SEPARATOR;
  86. }
  87. public function setPath($path)
  88. {
  89. $this->path = $path;
  90. return $this;
  91. }
  92. public function getCachePath()
  93. {
  94. return $this->path
  95. . DIRECTORY_SEPARATOR
  96. . 'cache'
  97. . DIRECTORY_SEPARATOR;
  98. }
  99. public function getThemePath($theme)
  100. {
  101. return $this->path
  102. . DIRECTORY_SEPARATOR
  103. . 'themes'
  104. . DIRECTORY_SEPARATOR
  105. . $theme
  106. . DIRECTORY_SEPARATOR
  107. . 'twig'
  108. . DIRECTORY_SEPARATOR;
  109. }
  110. }