Example #1
0
 /**
  * @throws InvalidArgumentException from sfYaml::load
  * @throws Zend_Config_Exception
  * @param string $filename
  * @param boolean $allowModifications
  */
 public function __construct($filename, $allowModifications = false)
 {
     $this->filename = $filename;
     $this->checkIfFilenameIsFile();
     $content = SymfonyYaml::load($this->filename);
     if (is_null($content)) {
         $content = array();
     }
     parent::__construct($content, $allowModifications);
 }
Example #2
0
 /**
  * @param array $array
  * @param string $modelName
  */
 public function __construct(array $array, $modelName = 'HealthCheck\\Model\\Service', $useRealPath = false)
 {
     $this->modelName = (string) $modelName;
     if (!class_exists($modelName)) {
         throw new Exception\BadModelCallException(sprintf('Model class "%s"', $modelName));
     }
     $data = array();
     foreach ($array as $serviceFilename) {
         /**
          * TODO Caching
          */
         $model = new $this->modelName($serviceFilename, $useRealPath);
         if (is_object($model) && method_exists($model, 'getName')) {
             $data[$model->getName()] = $model;
         } else {
             throw new Exception\BadModelCallException('Model class requires method "getName"');
         }
     }
     parent::__construct($data);
     if ($this->count() === 0) {
         throw new Exception\UnexpectedValueException(sprintf('Could not create any instance of "%s"', $this->modelName));
     }
 }
Example #3
0
 /**
  * Constructor
  *
  * @param bool $allowModifications readonly flag
  */
 public function __construct($allowModifications = false)
 {
     $home = self::getHomeDir();
     $cache = self::getCacheDir($home);
     parent::__construct(['home' => $home, 'cache-dir' => $cache], $allowModifications);
 }
Example #4
0
File: Json.php Project: ruflin/zf2
    /**
     * Loads the section $section from the config file encoded as JSON
     *
     * Sections are defined as properties of the main object
     *
     * In order to extend another section, a section defines the "_extends"
     * property having a value of the section name from which the extending
     * section inherits values.
     *
     * Note that the keys in $section will override any keys of the same
     * name in the sections that have been included via "_extends".
     *
     * @param  string  $json     JSON file or string to process
     * @param  mixed   $section Section to process
     * @param  boolean $options Whether modifiacations are allowed at runtime
     * @throws Exception\InvalidArgumentException When JSON text is not set or cannot be loaded
     * @throws Exception\RuntimeException When section $sectionName cannot be found in $json
     */
    public function __construct($json, $section = null, $options = false)
    {
        if (empty($json)) {
            throw new Exception\InvalidArgumentException('Filename is not set');
        }

        $allowModifications = false;
        if (is_bool($options)) {
            $allowModifications = $options;
        } elseif (is_array($options)) {
            foreach ($options as $key => $value) {
                switch (strtolower($key)) {
                    case 'allow_modifications':
                    case 'allowmodifications':
                        $allowModifications = (bool) $value;
                        break;
                    case 'skip_extends':
                    case 'skipextends':
                        $this->_skipExtends = (bool) $value;
                        break;
                    case 'ignore_constants':
                    case 'ignoreconstants':
                        $this->_ignoreConstants = (bool) $value;
                        break;
                    default:
                        break;
                }
            }
        }

        if ($json[0] != '{') {
            // read json file
            $this->_setErrorHandler();
            $content = file_get_contents($json, true);
            $errorMessages = $this->_restoreErrorHandler();
            if ($content === false) {
                $e = null;
                foreach ($errorMessages as $errMsg) {
                    $e = new Exception\RuntimeException($errMsg, 0, $e);
                }
                $e = new Exception\RuntimeException("Can't read file '{$json}'", 0, $e);
                throw $e;
            }
            $json = $content;
        }

        // Replace constants
        if (!$this->_ignoreConstants) {
            $json = $this->_replaceConstants($json);
        }

        // Parse/decode
        $config = JsonUtil::decode($json);

        if (null === $config) {
            // decode failed
            throw new Exception\RuntimeException("Error parsing JSON data");
        }

        // Flatten object structure into array
        $config = $this->flattenObjects($config);

        if ($section === null) {
            $dataArray = array();
            foreach ($config as $sectionName => $sectionData) {
                $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
            }

            parent::__construct($dataArray, $allowModifications);
        } elseif (is_array($section)) {
            $dataArray = array();
            foreach ($section as $sectionName) {
                if (!isset($config[$sectionName])) {
                    throw new Exception\RuntimeException(sprintf('Section "%s" cannot be found', $sectionName));
                }

                $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
            }

            parent::__construct($dataArray, $allowModifications);
        } else {
            if (!isset($config[$section])) {
                throw new Exception\RuntimeException(sprintf('Section "%s" cannot be found', $section));
            }

            $dataArray = $this->_processExtends($config, $section);
            if (!is_array($dataArray)) {
                // Section in the JSON data contains just one top level string
                $dataArray = array($section => $dataArray);
            }

            parent::__construct($dataArray, $allowModifications);
        }

        $this->_loadedSection = $section;
    }
Example #5
0
 public function __construct(array $data)
 {
     parent::__construct($data);
 }
Example #6
0
 public function __construct($params = [])
 {
     parent::__construct($params);
 }