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.

TreeController.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace GitList\Controller;
  3. use Silex\Application;
  4. use Silex\ControllerProviderInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  7. class TreeController implements ControllerProviderInterface
  8. {
  9. public function connect(Application $app)
  10. {
  11. $route = $app['controllers_factory'];
  12. $route->get('{repo}/tree/{commitishPath}/', $treeController = function ($repo, $commitishPath = '') use ($app) {
  13. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  14. if (!$commitishPath) {
  15. $commitishPath = $repository->getHead();
  16. }
  17. list($branch, $tree) = $app['util.routing']->parseCommitishPathParam($commitishPath, $repo);
  18. list($branch, $tree) = $app['util.repository']->extractRef($repository, $branch, $tree);
  19. $files = $repository->getTree($tree ? "$branch:\"$tree\"/" : $branch);
  20. $breadcrumbs = $app['util.view']->getBreadcrumbs($tree);
  21. $parent = null;
  22. if (($slash = strrpos($tree, '/')) !== false) {
  23. $parent = substr($tree, 0, $slash);
  24. } elseif (!empty($tree)) {
  25. $parent = '';
  26. }
  27. return $app['twig']->render('tree.twig', array(
  28. 'files' => $files->output(),
  29. 'repo' => $repo,
  30. 'branch' => $branch,
  31. 'path' => $tree ? $tree . '/' : $tree,
  32. 'parent' => $parent,
  33. 'breadcrumbs' => $breadcrumbs,
  34. 'branches' => $repository->getBranches(),
  35. 'tags' => $repository->getTags(),
  36. 'readme' => $app['util.repository']->getReadme($repository, $branch),
  37. ));
  38. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  39. ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
  40. ->convert('commitishPath', 'escaper.argument:escape')
  41. ->bind('tree');
  42. $route->post('{repo}/tree/{branch}/search', function (Request $request, $repo, $branch = '', $tree = '') use ($app) {
  43. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  44. if (!$branch) {
  45. $branch = $repository->getHead();
  46. }
  47. $query = $request->get('query');
  48. $breadcrumbs = array(array('dir' => 'Search results for: ' . $query, 'path' => ''));
  49. $results = $repository->searchTree($query, $branch);
  50. return $app['twig']->render('search.twig', array(
  51. 'results' => $results,
  52. 'repo' => $repo,
  53. 'branch' => $branch,
  54. 'path' => $tree,
  55. 'breadcrumbs' => $breadcrumbs,
  56. 'branches' => $repository->getBranches(),
  57. 'tags' => $repository->getTags(),
  58. 'query' => $query
  59. ));
  60. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  61. ->assert('branch', $app['util.routing']->getBranchRegex())
  62. ->convert('branch', 'escaper.argument:escape')
  63. ->bind('search');
  64. $route->get('{repo}/{format}ball/{branch}', function($repo, $format, $branch) use ($app) {
  65. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  66. $tree = $repository->getBranchTree($branch);
  67. if (false === $tree) {
  68. return $app->abort(404, 'Invalid commit or tree reference: ' . $branch);
  69. }
  70. $file = $app['cache.archives'] . DIRECTORY_SEPARATOR
  71. . $repo . DIRECTORY_SEPARATOR
  72. . substr($tree, 0, 2) . DIRECTORY_SEPARATOR
  73. . substr($tree, 2)
  74. . '.'
  75. . $format;
  76. if (!file_exists($file)) {
  77. $repository->createArchive($tree, $file, $format);
  78. }
  79. return new BinaryFileResponse($file);
  80. })->assert('format', '(zip|tar)')
  81. ->assert('repo', $app['util.routing']->getRepositoryRegex())
  82. ->assert('branch', $app['util.routing']->getBranchRegex())
  83. ->convert('branch', 'escaper.argument:escape')
  84. ->bind('archive');
  85. $route->get('{repo}/{branch}/', function($repo, $branch) use ($app, $treeController) {
  86. return $treeController($repo, $branch);
  87. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  88. ->assert('branch', $app['util.routing']->getBranchRegex())
  89. ->convert('branch', 'escaper.argument:escape')
  90. ->bind('branch');
  91. $route->get('{repo}/', function($repo) use ($app, $treeController) {
  92. return $treeController($repo);
  93. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  94. ->bind('repository');
  95. return $route;
  96. }
  97. }