<?php

namespace Heanup\Frame;

/**
 * Library的父类, 基础类库
 */
class Library
{

    protected $configClass = null;
    protected $configSection = null;

    /**
     * 构造函数
     * @param null $configClass
     * @param null $configSection
     */
    public function __construct($configClass = null, $configSection = null)
    {
        if ($configClass) {
            // 判断是否是全路径
            if(substr($configClass, 0, strlen(Dispatcher::CONFIG_PREFIX)) != Dispatcher::CONFIG_PREFIX){
                $this->configClass = Dispatcher::CONFIG_PREFIX . '\\' . trim($configClass, '\\');
            }else{
                $this->configClass = $configClass;
            }
        }
        // 设置配置文件内容片段
        if (!is_null($configSection)) {
            $this->configSection = $configSection;
        }
    }

    /**
     * 根据索引取出详细配置信息
     */
    protected function getConfig()
    {
        $class = $this->configClass;
        if (class_exists($class) && method_exists($class, 'getData')) {
            return $class::getData($this->configSection);
        }
        return array();
    }

    /**
     * 获取一组标识
     * @param $flag
     * @return string
     */
    protected function getFlag($flag = false)
    {
        $flag = $this->configClass . ':' . $this->configSection . ':' . intval($flag);
        return $flag;
    }

}
