Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • Config
  • IniAdapter
  • NeonAdapter

Interfaces

  • IAdapter
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Config;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Configuration storage.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: class Config
 24: {
 25:     /** @var array */
 26:     private static $extensions = array(
 27:         'ini' => 'Nette\Config\IniAdapter',
 28:         'neon' => 'Nette\Config\NeonAdapter',
 29:     );
 30: 
 31: 
 32: 
 33:     /**
 34:      * Static class - cannot be instantiated.
 35:      */
 36:     final public function __construct()
 37:     {
 38:         throw new Nette\StaticClassException;
 39:     }
 40: 
 41: 
 42: 
 43:     /**
 44:      * Registers adapter for given file extension.
 45:      * @param  string  file extension
 46:      * @param  string  class name (IConfigAdapter)
 47:      * @return void
 48:      */
 49:     public static function registerExtension($extension, $class)
 50:     {
 51:         if (!class_exists($class)) {
 52:             throw new Nette\InvalidArgumentException("Class '$class' was not found.");
 53:         }
 54: 
 55:         if (!Nette\Reflection\ClassType::from($class)->implementsInterface('Nette\Config\IAdapter')) {
 56:             throw new Nette\InvalidArgumentException("Configuration adapter '$class' is not Nette\\Config\\IAdapter implementor.");
 57:         }
 58: 
 59:         self::$extensions[strtolower($extension)] = $class;
 60:     }
 61: 
 62: 
 63: 
 64:     /**
 65:      * Creates new configuration object from file.
 66:      * @param  string  file name
 67:      * @param  string  section to load
 68:      * @return array
 69:      */
 70:     public static function fromFile($file, $section = NULL)
 71:     {
 72:         $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
 73:         if (!isset(self::$extensions[$extension])) {
 74:             throw new Nette\InvalidArgumentException("Unknown file extension '$file'.");
 75:         }
 76: 
 77:         $data = call_user_func(array(self::$extensions[$extension], 'load'), $file, $section);
 78:         if ($section) {
 79:             if (!isset($data[$section]) || !is_array($data[$section])) {
 80:                 throw new Nette\InvalidStateException("There is not section [$section] in file '$file'.");
 81:             }
 82:             $data = $data[$section];
 83:         }
 84:         return $data;
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Save configuration to file.
 91:      * @param  mixed
 92:      * @param  string  file
 93:      * @return void
 94:      */
 95:     public static function save($config, $file)
 96:     {
 97:         $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
 98:         if (!isset(self::$extensions[$extension])) {
 99:             throw new Nette\InvalidArgumentException("Unknown file extension '$file'.");
100:         }
101:         return call_user_func(array(self::$extensions[$extension], 'save'), $config, $file);
102:     }
103: 
104: }
105: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0