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.

NetworkController.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace GitList\Controller;
  3. use GitList\Git\Repository;
  4. use Gitter\Model\Commit\Commit;
  5. use Silex\Application;
  6. use Silex\ControllerProviderInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class NetworkController implements ControllerProviderInterface
  9. {
  10. public function connect(Application $app)
  11. {
  12. $route = $app['controllers_factory'];
  13. $route->get('{repo}/network/{commitishPath}/{page}.json',
  14. function ($repo, $commitishPath, $page) use ($app) {
  15. /** @var $repository Repository */
  16. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  17. if ($commitishPath === null) {
  18. $commitishPath = $repository->getHead();
  19. }
  20. $pager = $app['util.view']->getPager($page, $repository->getTotalCommits($commitishPath));
  21. $commits = $repository->getPaginatedCommits($commitishPath, $pager['current']);
  22. $jsonFormattedCommits = array();
  23. foreach ($commits as $commit) {
  24. $detailsUrl = $app['url_generator']->generate(
  25. 'commit',
  26. array(
  27. 'repo' => $repo,
  28. 'commit' => $commit->getHash()
  29. )
  30. );
  31. $jsonFormattedCommits[$commit->getHash()] = array(
  32. 'hash' => $commit->getHash(),
  33. 'parentsHash' => $commit->getParentsHash(),
  34. 'date' => $commit->getDate()->format('U'),
  35. 'message' => htmlentities($commit->getMessage()),
  36. 'details' => $detailsUrl,
  37. 'author' => array(
  38. 'name' => $commit->getAuthor()->getName(),
  39. 'email' => $commit->getAuthor()->getEmail(),
  40. // due to the lack of a inbuilt javascript md5 mechanism, build the full avatar url on the php side
  41. 'image' => 'http://gravatar.com/avatar/' . md5(
  42. strtolower($commit->getAuthor()->getEmail())
  43. ) . '?s=40'
  44. )
  45. );
  46. }
  47. $nextPageUrl = null;
  48. if ($pager['last'] !== $pager['current']) {
  49. $nextPageUrl = $app['url_generator']->generate(
  50. 'networkData',
  51. array(
  52. 'repo' => $repo,
  53. 'commitishPath' => $commitishPath,
  54. 'page' => $pager['next']
  55. )
  56. );
  57. }
  58. // when no commits are given, return an empty response - issue #369
  59. if( count($commits) === 0 ) {
  60. return $app->json( array(
  61. 'repo' => $repo,
  62. 'commitishPath' => $commitishPath,
  63. 'nextPage' => null,
  64. 'start' => null,
  65. 'commits' => $jsonFormattedCommits
  66. ), 200
  67. );
  68. }
  69. return $app->json( array(
  70. 'repo' => $repo,
  71. 'commitishPath' => $commitishPath,
  72. 'nextPage' => $nextPageUrl,
  73. 'start' => $commits[0]->getHash(),
  74. 'commits' => $jsonFormattedCommits
  75. ), 200
  76. );
  77. }
  78. )->assert('repo', $app['util.routing']->getRepositoryRegex())
  79. ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
  80. ->value('commitishPath', null)
  81. ->convert('commitishPath', 'escaper.argument:escape')
  82. ->assert('page', '\d+')
  83. ->value('page', '0')
  84. ->bind('networkData');
  85. $route->get(
  86. '{repo}/network/{commitishPath}',
  87. function ($repo, $commitishPath) use ($app) {
  88. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  89. if ($commitishPath === null) {
  90. $commitishPath = $repository->getHead();
  91. }
  92. list($branch, $file) = $app['util.routing']->parseCommitishPathParam($commitishPath, $repo);
  93. list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
  94. return $app['twig']->render(
  95. 'network.twig',
  96. array(
  97. 'repo' => $repo,
  98. 'branch' => $branch,
  99. 'commitishPath' => $commitishPath,
  100. )
  101. );
  102. }
  103. )->assert('repo', $app['util.routing']->getRepositoryRegex())
  104. ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
  105. ->value('commitishPath', null)
  106. ->convert('commitishPath', 'escaper.argument:escape')
  107. ->bind('network');
  108. return $route;
  109. }
  110. }