use strict;
use SL::Auth;
-use YAML::XS ();
+use YAML ();
use File::Spec;
use SL::MoreCommon qw(uri_encode);
+our $yaml_xs;
+BEGIN {
+ $yaml_xs = eval { require YAML::XS };
+}
+
+our %menu_cache;
+
sub new {
my ($package, $domain) = @_;
- my $path = File::Spec->catdir('menus', $domain);
+ if (!$menu_cache{$domain}) {
+ my $path = File::Spec->catdir('menus', $domain);
- opendir my $dir, $path or die "can't open $path: $!";
- my @files = sort grep -f "$path/$_", readdir $dir;
- close $dir;
+ opendir my $dir, $path or die "can't open $path: $!";
+ my @files = sort grep -f "$path/$_", grep /\.yaml$/, readdir $dir;
+ close $dir;
- my $nodes = [];
- my $nodes_by_id = {};
- for my $file (@files) {
- my $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file));
- _merge($nodes, $nodes_by_id, $data);
- }
+ my $nodes = [];
+ my $nodes_by_id = {};
+ for my $file (@files) {
+ my $data;
+ if ($yaml_xs) {
+ $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file));
+ } else {
+ $data = YAML::LoadFile(File::Spec->catfile($path, $file));
+ }
+ _merge($nodes, $nodes_by_id, $data);
+ }
- my $self = bless {
- nodes => $nodes,
- by_id => $nodes_by_id,
- }, $package;
+ my $self = bless {
+ nodes => $nodes,
+ by_id => $nodes_by_id,
+ }, $package;
- $self->build_tree;
- $self->set_access;
+ $self->build_tree;
- return $self;
+ $menu_cache{$domain} = $self;
+ } else {
+ $menu_cache{$domain}->clear_access;
+ }
+
+ $menu_cache{$domain}->set_access;
+
+ return $menu_cache{$domain};
}
sub _merge {
# first, some sanity check. are all parents valid ids or empty?
for my $node ($self->nodes) {
next if !exists $node->{parent} || !$node->{parent} || $self->{by_id}->{$node->{id}};
- die "menu: node $node->{id} has non-existant parent $node->{parent}";
+ die "menu: node $node->{id} has non-existent parent $node->{parent}";
}
my %by_parent;
} elsif ($token eq ")") {
pop @stack;
if (!@stack) {
- die "Error in menu.ini for entry $node->{id}: missing '('";
+ die "Error while parsing menu entry $node->{id}: missing '('";
}
$cur_ary = $stack[-1];
}
if ($access) {
- die "Error in menu.ini for entry $node->{id}: unrecognized token at the start of '$access'\n";
+ die "Error while parsing menu entry $node->{id}: unrecognized token at the start of '$access'\n";
}
if (1 < scalar @stack) {
- die "Error in menu.ini for entry $node->{id}: Missing ')'\n";
+ die "Error while parsing menu entry $node->{id}: Missing ')'\n";
}
return SL::Auth::evaluate_rights_ary($stack[0]);
return $::instance_conf->data->{$setting};
}
+sub clear_access {
+ my ($self) = @_;
+ for my $node ($self->tree_walk("all")) {
+ delete $node->{visible};
+ delete $node->{visible_children};
+ }
+}
+
sub set_access {
my ($self) = @_;
# 1. evaluate access for all