refactored the tree controller

This commit is contained in:
Fabien Potencier
2012-07-06 22:57:30 +02:00
parent 9f837611b5
commit cddca4683a
7 changed files with 42 additions and 57 deletions

View File

@@ -1,53 +1,17 @@
<?php <?php
$app->get('{repo}/', function($repo) use($app) { $app->get('{repo}/tree/{branch}/{tree}/', $treeController = function($repo, $branch = '', $tree = '') use($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo); $repository = $app['git']->getRepository($app['git.repos'] . $repo);
$defaultBranch = $repository->getHead(); if (!$branch) {
$tree = $repository->getTree($defaultBranch); $branch = $repository->getHead();
}
return $app['twig']->render('tree.twig', array( $files = $repository->getTree($tree ? "$branch:'$tree'/" : $branch);
'page' => 'files',
'files' => $tree->output(),
'repo' => $repo,
'branch' => $defaultBranch,
'path' => '',
'parent' => '',
'breadcrumbs' => array(),
'branches' => $repository->getBranches(),
'tags' => $repository->getTags(),
'readme' => $app['utils']->getReadme($repo, $defaultBranch),
));
})->assert('repo', '[\w-._]+')
->bind('repository');
$app->get('{repo}/tree/{branch}/', function($repo, $branch) use($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
$tree = $repository->getTree($branch);
return $app['twig']->render('tree.twig', array(
'page' => 'files',
'files' => $tree->output(),
'repo' => $repo,
'branch' => $branch,
'path' => '',
'parent' => '',
'breadcrumbs' => array(),
'branches' => $repository->getBranches(),
'tags' => $repository->getTags(),
'readme' => $app['utils']->getReadme($repo, $branch),
));
})->assert('repo', '[\w-._]+')
->assert('branch', '[\w-._]+')
->bind('tree');
$app->get('{repo}/tree/{branch}/{tree}/', function($repo, $branch, $tree) use($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
$files = $repository->getTree("$branch:'$tree'/");
$breadcrumbs = $app['utils']->getBreadcrumbs($tree); $breadcrumbs = $app['utils']->getBreadcrumbs($tree);
$parent = null;
if (($slash = strrpos($tree, '/')) !== false) { if (($slash = strrpos($tree, '/')) !== false) {
$parent = substr($tree, 0, $slash); $parent = substr($tree, 0, $slash);
} else { } elseif (!empty($tree)) {
$parent = ''; $parent = '';
} }
@@ -56,14 +20,25 @@ $app->get('{repo}/tree/{branch}/{tree}/', function($repo, $branch, $tree) use($a
'files' => $files->output(), 'files' => $files->output(),
'repo' => $repo, 'repo' => $repo,
'branch' => $branch, 'branch' => $branch,
'path' => "$tree/", 'path' => $tree ? $tree.'/' : $tree,
'parent' => $parent, 'parent' => $parent,
'breadcrumbs' => $breadcrumbs, 'breadcrumbs' => $breadcrumbs,
'branches' => $repository->getBranches(), 'branches' => $repository->getBranches(),
'tags' => $repository->getTags(), 'tags' => $repository->getTags(),
'readme' => $app['utils']->getReadme($repo, $branch), 'readme' => $app['utils']->getReadme($repo, $branch),
)); ));
})->assert('tree', '.+') })->assert('repo', '[\w-._]+')
->assert('repo', '[\w-._]+')
->assert('branch', '[\w-._]+') ->assert('branch', '[\w-._]+')
->bind('tree_dir'); ->assert('tree', '.+')
->bind('tree');
$app->get('{repo}/{branch}/', function($repo, $branch) use($app, $treeController) {
return $treeController($repo, $branch);
})->assert('repo', '[\w-._]+')
->assert('branch', '[\w-._]+')
->bind('branch');
$app->get('{repo}/', function($repo) use($app, $treeController) {
return $treeController($repo);
})->assert('repo', '[\w-._]+')
->bind('repository');

View File

@@ -24,6 +24,10 @@ class Utils
*/ */
public function getBreadcrumbs($spec) public function getBreadcrumbs($spec)
{ {
if (!$spec) {
return array();
}
$paths = explode('/', $spec); $paths = explode('/', $spec);
foreach ($paths as $i => $path) { foreach ($paths as $i => $path) {

View File

@@ -3,12 +3,12 @@
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li class="dropdown-header">Branches</li> <li class="dropdown-header">Branches</li>
{% for item in branches %} {% for item in branches %}
<li><a href="{{ path('tree', {repo: repo, branch: item}) }}">{{ item }}</a></li> <li><a href="{{ path('branch', {repo: repo, branch: item}) }}">{{ item }}</a></li>
{% endfor %} {% endfor %}
{% if tags %} {% if tags %}
<li class="dropdown-header">Tags</li> <li class="dropdown-header">Tags</li>
{% for item in tags %} {% for item in tags %}
<li><a href="{{ path('tree', {repo: repo, branch: item}) }}">{{ item }}</a></li> <li><a href="{{ path('branch', {repo: repo, branch: item}) }}">{{ item }}</a></li>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
</ul> </ul>

View File

@@ -1,8 +1,8 @@
<ul class="breadcrumb"> <ul class="breadcrumb">
<li><a href="{{ path('repository', {repo: repo}) }}">{{ repo }}</a></li> <li><a href="{{ path('repository', {repo: repo}) }}">{{ repo }}</a></li>
{% for breadcrumb in breadcrumbs %} {% for breadcrumb in breadcrumbs %}
<span class="divider">/</span> <span class="divider">/</span>
<li{% if loop.last %} class="active"{% endif %}>{% if not loop.last %}<a href="{{ path('tree_dir', {repo: repo, branch: branch, tree: breadcrumb.path}) }}">{{ breadcrumb.dir }}</a>{% endif %}{% if loop.last %}{{ breadcrumb.dir }}{% endif %}</li> <li{% if loop.last %} class="active"{% endif %}>{% if not loop.last %}<a href="{{ path('tree', {repo: repo, branch: branch, tree: breadcrumb.path}) }}">{{ breadcrumb.dir }}</a>{% endif %}{% if loop.last %}{{ breadcrumb.dir }}{% endif %}</li>
{% endfor %} {% endfor %}
{% block extra %}{% endblock %} {% block extra %}{% endblock %}

View File

@@ -7,7 +7,7 @@
<div class="commit-view"> <div class="commit-view">
<div class="commit-header"> <div class="commit-header">
<span class="pull-right"><a class="btn btn-small" href="{{ path('tree', {repo: repo, branch: commit.getHash}) }}" title="Browse code at this point in history"><i class="icon-list-alt"></i> Browse code</a></span> <span class="pull-right"><a class="btn btn-small" href="{{ path('branch', {repo: repo, branch: commit.getHash}) }}" title="Browse code at this point in history"><i class="icon-list-alt"></i> Browse code</a></span>
<h4>{{ commit.getMessage }}</h4> <h4>{{ commit.getMessage }}</h4>
</div> </div>
<div class="commit-body"> <div class="commit-body">

View File

@@ -1,5 +1,5 @@
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li{% if page == 'files' %} class="active"{% endif %}><a href="{{ path('tree', {repo: repo, branch: branch}) }}">Files</a></li> <li{% if page == 'files' %} class="active"{% endif %}><a href="{{ path('branch', {repo: repo, branch: branch}) }}">Files</a></li>
<li{% if page == 'commits' %} class="active"{% endif %}><a href="{{ path('commits', {repo: repo, branch: branch}) }}">Commits</a></li> <li{% if page == 'commits' %} class="active"{% endif %}><a href="{{ path('commits', {repo: repo, branch: branch}) }}">Commits</a></li>
<li{% if page == 'stats' %} class="active"{% endif %}><a href="{{ path('stats', {repo: repo, branch: branch}) }}">Stats</a></li> <li{% if page == 'stats' %} class="active"{% endif %}><a href="{{ path('stats', {repo: repo, branch: branch}) }}">Stats</a></li>
</ul> </ul>

View File

@@ -18,9 +18,15 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% if parent is not empty %} {% if parent is not null %}
<tr> <tr>
<td><i class="icon-spaced"></i> <a href="{{ path('tree_dir', {repo: repo, branch: branch, tree: parent}) }}">..</a></td> <td><i class="icon-spaced"></i>
{% if not parent %}
<a href="{{ path('branch', {repo: repo, branch: branch}) }}">..</a>
{% else %}
<a href="{{ path('tree', {repo: repo, branch: branch, tree: parent}) }}">..</a>
{% endif %}
</td>
<td></td> <td></td>
<td></td> <td></td>
</tr> </tr>
@@ -29,7 +35,7 @@
<tr> <tr>
<td><i class="{{ file.type == "folder" or file.type == "symlink" ? "icon-folder-open" : "icon-file" }} icon-spaced"></i> <a href=" <td><i class="{{ file.type == "folder" or file.type == "symlink" ? "icon-folder-open" : "icon-file" }} icon-spaced"></i> <a href="
{%- if file.type == "folder" or file.type == "symlink" %} {%- if file.type == "folder" or file.type == "symlink" %}
{{ path('tree_dir', {repo: repo, branch: branch, tree: path ~ (file.type == "symlink" ? file.path : file.name)}) }} {{ path('tree', {repo: repo, branch: branch, tree: path ~ (file.type == "symlink" ? file.path : file.name)}) }}
{% else %} {% else %}
{{ path('blob', {repo: repo, branch: branch, file: path ~ (file.type == "symlink" ? file.path : file.name)}) }} {{ path('blob', {repo: repo, branch: branch, file: path ~ (file.type == "symlink" ? file.path : file.name)}) }}
{% endif -%} {% endif -%}