router = $router; $this->cache_dir = $cache_dir; $this->config_dir = $config_dir; $this->acl_file = $acl_file; $this->acl_key = $acl_key; } public function getACL() { $key = $this->acl_key; // cache config $cache_file = $this->cache_dir . '/' . $key . '.serial'; $cache = new ConfigCache($cache_file, true); // check if cache is fresh if (!$cache->isFresh()) { $files = []; $resources = []; try { // get location of the yaml file with the acls // $path = $this->config_dir . '/'; $path = $this->config_dir . '/' . $this->acl_file; $files[] = $path; $resources[] = new FileResource($path); // process acl config file $data = $this->parseACL($path, $key); } catch (\InvalidArgumentException $e) { error_log($e->getMessage()); error_log($key . ' key not found in ' . $this->acl_file . 'file.'); return $data; } $acl_serial = serialize($data); $cache->write($acl_serial, $resources); } else { $acl_serial = file_get_contents($cache_file); $data = unserialize($acl_serial); } return $data; } protected function parseACL($path, $key) { $parser = new YamlParser(); $config = $parser->parse(file_get_contents($path)); // check if we have access keys if (!isset($config[$key])) { error_log('No ' . $key . ' found for ' . $path); return; } $acl_hierarchy = []; $acl_index = []; // go through each one foreach ($config[$key] as $acl_data) { // build hierarchy $acl_hierarchy[$acl_data['id']] = [ 'label' => $acl_data['label'], 'acls' => [] ]; foreach ($acl_data['acls'] as $acl) { $id = $acl['id']; $label = $acl['label']; // set hierarchy and index $acl_hierarchy[$acl_data['id']]['acls'][$id] = $label; $acl_index[$id] = $label; } } return [ 'hierarchy' => $acl_hierarchy, 'index' => $acl_index ]; } }