Source of file Info.php

Size: 2,842 Bytes - Last Modified: 2019-05-22T08:51:12+00:00

/home/matic/hprojects/public-docs/intrigo-plugin-box/src/RefineIt/Info.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
<?php

declare(strict_types=1);

/**
 * --------------------------------------------------------------------------------
 * THIS FILE SHOULD NOT BE EDITED MANUALLY!
 * 
 * Please refer to: 
 * http://maticzagmajster.ddns.net/docs/intrigo-plugin-box/ 
 * for more details.
 * --------------------------------------------------------------------------------
 */

namespace RefineIt;

use RefineIt\Support\Plugin\InfoInterface;


class Info implements InfoInterface {

	/**
	 * Config of the scope (module) the class is in.
	 *
	 * When config is loaded this is flat associative array holding all the values 
	 * from files it can find in relevant config folder.
	 * 
	 * @var mixed
	 */
	protected static $config = NULL;

	/**
	 * Configuration files we are looking for in every config folder.
	 * 
	 * @var array
	 */
	const VALID_CONFIG_FILES = [
		'option.php',
		'context.php',
		'structure.php'
	];

	/**
	 * Name of the config folder in given scope.
	 *
	 * @var string
	 */
	const CONFIG_FOLDER = 'config';

	/**
	 * Gets root path of the module.
	 * 
	 * @return string Absolute path.
	 */
	public static function root_path(): string {
		return \trailingslashit(__DIR__);
	}

	/**
	 * Gets root url of the module.
	 * 
	 * @return string Absolute path.
	 */
	public static function root_url(): string {
		return \plugin_dir_url(__FILE__);
	}

	/**
	 * Gets root plugin path.
	 * 
	 * @return string Absolute path.
	 */
	public static function root_plugin_path(): string {
		return \trailingslashit(\realpath((self::root_path() . '/..')));
	}

	/**
	 * Offers access to $config variable.
	 * 
	 * @return array
	 */
	public static function g(): array {

		if (self::$config === NULL) {
			$config_path = self::root_path() . '/' . self::CONFIG_FOLDER;
			$config_reader_factory = new \Helhum\ConfigLoader\ConfigurationReaderFactory($config_path);

			$config_readers = [];
			foreach (self::VALID_CONFIG_FILES as $f) {
				$path = $config_path . '/' . $f;
				if (!file_exists($path)) {
					continue;
				}

				$config_readers[] = $config_reader_factory->createReader($path);
			}

			$config_loader = new \Helhum\ConfigLoader\ConfigurationLoader($config_readers);
			self::$config = $config_loader->load();
		}

		return self::$config;
	}

	/**
	 * Gets configuration just in one file.
	 * 
	 * @param  string $filename Filename from config folder.
	 * @return array
	 */
	public static function get(string $filename): array {
		$config_path = self::root_path() . '/' . self::CONFIG_FOLDER;
		$config_reader_factory = new \Helhum\ConfigLoader\ConfigurationReaderFactory($config_path);

		$path = $config_path . '/' . $filename;

		if (!file_exists($path)) {
			return [];
		}

		$config_loader = new \Helhum\ConfigLoader\ConfigurationLoader([
			$config_reader_factory->createReader($path)
		]);
		return $config_loader->load();
	}

}