rootPath = $rootPath; $this->names = $this->templates = null; // lazy loaded, not yet set } private function load() { if(!is_null($this->templates)) { return; // already loaded } $templateList = new \DirectoryIterator($this->rootPath . '/lib/conf.d/data_templates'); $this->templates = []; $this->names = []; foreach ($templateList as $template) { if ($template->isDot() || !$template->isFile() || $template->getExtension() !== self::TEMPLATE_EXTENSION ) { continue; } $name = $template->getBasename('.' . self::TEMPLATE_EXTENSION); // beware that the directoryiterator returns a reference on a static, so clone() $this->templates[$name] = clone($template); $this->names[] = $name; } } /** * @param $templateName * @return null|\SplFileInfo */ public function getByName($templateName) { $this->load(); if (!array_key_exists($templateName, $this->templates)) { return null; } return $this->templates[$templateName]; } /** * @param $index * @return null|\SplFileInfo */ public function getNameByIndex($index) { $this->load(); return $this->names[$index]; } /** * @return \string[] */ public function getNames() { $this->load(); return $this->names; } public function toString() { $this->load(); return implode(', ', $this->names); } /** * @return string */ public function getDefault() { $this->load(); return $this->getByName(self::DEFAULT_TEMPLATE) ? self::DEFAULT_TEMPLATE : $this->getNameByIndex(0); } }