Packages

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

Classes

  • NConfig
  • NIniAdapter
  • NNeonAdapter

Interfaces

  • IConfigAdapter
  • Overview
  • Package
  • 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:  * @package Nette\Config
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Configuration storage.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Config
 20:  */
 21: class NConfig
 22: {
 23:     /** @var array */
 24:     private static $extensions = array(
 25:         'ini' => 'NIniAdapter',
 26:         'neon' => 'NNeonAdapter',
 27:     );
 28: 
 29: 
 30: 
 31:     /**
 32:      * Static class - cannot be instantiated.
 33:      */
 34:     final public function __construct()
 35:     {
 36:         throw new NStaticClassException;
 37:     }
 38: 
 39: 
 40: 
 41:     /**
 42:      * Registers adapter for given file extension.
 43:      * @param  string  file extension
 44:      * @param  string  class name (IConfigAdapter)
 45:      * @return void
 46:      */
 47:     public static function registerExtension($extension, $class)
 48:     {
 49:         if (!class_exists($class)) {
 50:             throw new InvalidArgumentException("Class '$class' was not found.");
 51:         }
 52: 
 53:         if (!NClassReflection::from($class)->implementsInterface('IConfigAdapter')) {
 54:             throw new InvalidArgumentException("Configuration adapter '$class' is not IConfigAdapter implementor.");
 55:         }
 56: 
 57:         self::$extensions[strtolower($extension)] = $class;
 58:     }
 59: 
 60: 
 61: 
 62:     /**
 63:      * Creates new configuration object from file.
 64:      * @param  string  file name
 65:      * @param  string  section to load
 66:      * @return array
 67:      */
 68:     public static function fromFile($file, $section = NULL)
 69:     {
 70:         $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
 71:         if (!isset(self::$extensions[$extension])) {
 72:             throw new InvalidArgumentException("Unknown file extension '$file'.");
 73:         }
 74: 
 75:         $data = call_user_func(array(self::$extensions[$extension], 'load'), $file, $section);
 76:         if ($section) {
 77:             if (!isset($data[$section]) || !is_array($data[$section])) {
 78:                 throw new InvalidStateException("There is not section [$section] in file '$file'.");
 79:             }
 80:             $data = $data[$section];
 81:         }
 82:         return $data;
 83:     }
 84: 
 85: 
 86: 
 87:     /**
 88:      * Save configuration to file.
 89:      * @param  mixed
 90:      * @param  string  file
 91:      * @return void
 92:      */
 93:     public static function save($config, $file)
 94:     {
 95:         $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
 96:         if (!isset(self::$extensions[$extension])) {
 97:             throw new InvalidArgumentException("Unknown file extension '$file'.");
 98:         }
 99:         return call_user_func(array(self::$extensions[$extension], 'save'), $config, $file);
100:     }
101: 
102: }
103: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0