Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
do not allow routes to be rearranged at runtime
  • Loading branch information
kraih committed Jul 21, 2014
1 parent 76842a4 commit 6519891
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 50 deletions.
2 changes: 0 additions & 2 deletions Changes
@@ -1,7 +1,5 @@

5.16 2014-07-21
- Added empty method to Mojo::Cache.
- Added flush method to Mojolicious::Routes.
- Improved Mojo::Asset::File to allow appending data to existing files.
(iakuf, sri)

Expand Down
8 changes: 0 additions & 8 deletions lib/Mojo/Cache.pm
Expand Up @@ -3,8 +3,6 @@ use Mojo::Base -base;

has 'max_keys' => 100;

sub empty { delete shift->{cache} }

sub get { (shift->{cache} || {})->{shift()} }

sub set {
Expand Down Expand Up @@ -55,12 +53,6 @@ Maximum number of cache keys, defaults to C<100>.
L<Mojo::Cache> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 empty
$cache->empty;
Empty cache.
=head2 get
my $value = $cache->get('foo');
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -804,10 +804,9 @@ unescaped and decoded from bytes to characters.

=head2 Rearranging routes

All routes can be moved around or even removed with methods like
L<Mojolicious::Routes::Route/"add_child"> and
L<Mojolicious::Routes::Route/"remove">, after the first request has been
handled this can affect performance though.
Until the first request has been handled, all routes can still be moved around
or even removed with methods like L<Mojolicious::Routes::Route/"add_child">
and L<Mojolicious::Routes::Route/"remove">.

# GET /example/show -> {controller => 'example', action => 'show'}
my $show = $r->get('/show')->to('example#show');
Expand All @@ -818,7 +817,7 @@ handled this can affect performance though.
$r->find('show_secrets')->remove;

Especially for rearranging routes created by plugins this can be very useful,
L<Mojolicious::Routes::Route/"find"> is used to locate routes by their name.
to find routes by their name you can use L<Mojolicious::Routes::Route/"find">.

=head2 Conditions

Expand Down
12 changes: 0 additions & 12 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -53,12 +53,6 @@ sub dispatch {
return 1;
}

sub flush {
my $self = shift;
if (my $cache = $self->cache) { $cache->empty }
delete $self->{reverse};
}

sub hide { push @{shift->hidden}, @_ }

sub is_hidden {
Expand Down Expand Up @@ -340,12 +334,6 @@ every action.
Match routes with L</"match"> and dispatch with L</"continue">.
=head2 flush
$r->flush;
Flush caches and allow routes to be changed again.
=head2 hide
$r = $r->hide(qw(foo bar));
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -24,7 +24,6 @@ sub AUTOLOAD {

sub add_child {
my ($self, $route) = @_;
$self->root->flush;
weaken $route->remove->parent($self)->{parent};
push @{$self->children}, $route;
return $self;
Expand Down Expand Up @@ -116,7 +115,6 @@ sub put { shift->_generate_route(PUT => @_) }
sub remove {
my $self = shift;
return $self unless my $parent = $self->parent;
$self->root->flush;
@{$parent->children} = grep { $_ ne $self } @{$parent->children};
return $self->parent(undef);
}
Expand Down
10 changes: 3 additions & 7 deletions t/mojo/cache.t
Expand Up @@ -19,13 +19,9 @@ is $cache->get('foo'), undef, 'no result';
is $cache->get('bar'), undef, 'no result';
is $cache->get('baz'), 'yada', 'right result';
is $cache->get('yada'), 23, 'right result';
$cache->empty;
is $cache->get('baz'), undef, 'no result';
is $cache->get('yada'), undef, 'no result';
$cache->max_keys(1)->set(one => 1)->set(two => 2)->set(three => 3);
is $cache->get('one'), undef, 'no result';
is $cache->get('two'), undef, 'no result';
is $cache->get('three'), 3, 'right result';
$cache->max_keys(1)->set(one => 1)->set(two => 2);
is $cache->get('one'), undef, 'no result';
is $cache->get('two'), 2, 'right result';

$cache = Mojo::Cache->new(max_keys => 3);
is $cache->get('foo'), undef, 'no result';
Expand Down
8 changes: 0 additions & 8 deletions t/mojolicious/app.t
Expand Up @@ -269,14 +269,6 @@ $t->get_ok('/happy/fun/time' => {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->content_is('Have fun!');

# Foo::index (modify routes at runtime)
ok $t->app->routes->cache, 'routes are cached';
$t->get_ok('/foo/index')->content_like(qr/Hello Mojo/);
$t->app->routes->get('/foo/index' => {text => 'new route'});
$t->app->routes->add_child($t->app->routes->find('default'));
$t->get_ok('/foo/index')->content_is('new route');
ok $t->app->routes->cache, 'routes are still cached';

# Foo::test
$t->get_ok('/foo/test' => {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => 'Bite my shiny metal ass!')
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/lib/MojoliciousTest.pm
Expand Up @@ -156,7 +156,7 @@ sub startup {
$r->route('/rss.xml')->to('foo#bar', format => 'rss');

# /*/* (the default route)
$r->route('/(controller)/(action)')->to(action => 'index')->name('default');
$r->route('/(controller)/(action)')->to(action => 'index');

# /just/some/template (embedded template)
$r->route('/just/some/template')->to(template => 'just/some/template');
Expand Down
11 changes: 6 additions & 5 deletions t/mojolicious/routes.t
Expand Up @@ -181,9 +181,7 @@ my $third = $source->route('/third')->to('#third');
my $target = $r->remove->route('/target')->to('target#');
my $second = $r->find('second');
is $second->render('', {}), '/source/second', 'right result';
is $r->cache(0)->lookup('second'), $second, 'route does exist';
$second->remove;
isnt $r->lookup('second'), $second, 'route does not exist';
is $second->render('', {}), '/second', 'right result';
$target->add_child($first)->add_child($second);
is $second->render('', {}), '/target/second', 'right result';
Expand All @@ -197,14 +195,17 @@ $r->route('/missing/too/*', '' => ['test'])
->to('missing#too', '' => 'missing');

# Cached lookup
is $r->find('fast'), undef, 'fastest route not found';
is $r->lookup('fast'), undef, 'fastest route not found';
my $fast = $r->route('/fast');
is $r->find('fast'), $fast, 'fast route found';
is $r->lookup('fast'), $fast, 'fast route found';
my $faster = $r->route('/faster')->name('fast');
is $r->find('fast'), $faster, 'faster route found';
is $r->lookup('fast'), $faster, 'fast route found';
is $r->lookup('fast'), $fast, 'fast route found';
is $r->find('fastest'), undef, 'fastest route not found';
is $r->lookup('fastest'), undef, 'fastest route not found';
my $fastest = $r->route('/fastest');
is $r->find('fastest'), $fastest, 'fastest route found';
is $r->lookup('fastest'), $fastest, 'fastest route found';

# Make sure stash stays clean
my $c = Mojolicious::Controller->new;
Expand Down

0 comments on commit 6519891

Please sign in to comment.