2017 © Pedro Peláez
 

library zend-yaml-config

Allows to use YAML configuration files in ZF2

image

rastusik/zend-yaml-config

Allows to use YAML configuration files in ZF2

  • Sunday, December 21, 2014
  • by Rastusik
  • Repository
  • 1 Watchers
  • 1 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

ZendYamlConfig

Module providing the ability to use YAML configuration files in ZF2., (*1)

Many devs don't like ZF2 for many reasons, one of them is the necessity to use php arrays for configuration. With this module, it is possible to leave behind all the php arrays and to use a nicer and more compact format, in other words, you are able to change files like this:, (*2)

<?php

return [
    'modules' => [
        'ZendDeveloperTools',
        'ZendYamlConfig',
        'Application',
    ],
    'module_listener_options' => [
        'config_glob_paths' => [
            'config/autoload/{,*.}{global,local}.php',
        ],
        'module_paths' => [
            './module',
            './vendor',
        ],
    ],
];

into this:, (*3)

modules:
  - ZendDeveloperTools
  - ZendYamlConfig
  - Application

module_listener_options:
  config_glob_paths:
    - config/autoload/{,*.}{global,local}.yaml

  module_paths:
    - ./module
    - ./vendor

Note the difference between the number of lines. YAML also prohibits you to write executable code into your configuration files, so the configuration does only what it is supposed to do (without any anonymous callbacks)., (*4)

Requirements

Installation

  1. Add "rastusik/zend-yaml-config": "dev-master" to your composer.json
  2. Run php composer.phar install
  3. Enable the module in your config/application.config.php by adding ZendYamlConfig to modules (note: the module has to be loaded before all the modules that use YAML config files)

Usage

If you want to read the configuration files on the Module level (the config files of your modules), your module has to look like this:, (*5)

<?php

class Module implements DependencyIndicatorInterface, InitProviderInterface
{

    /**
     * @var ZendYamlConfig\Service\YamlFileParser
     */
    protected $yamlParser;

    /**
     * Expected to return an array of modules on which the current one depends on
     * - this module dependes on ZendYamlConfig
     *
     * @return array
     */
    public function getModuleDependencies()
    {
        return ['ZendYamlConfig'];
    }

    /**
     * Initialization of the module - retrieval of the YAML file parser
     */
    public function init(ModuleManagerInterface $manager)
    {
        if (!$manager instanceof ModuleManager) {
            return;
        }

        $event = $manager->getEvent();
        /* @var $serviceManager ServiceManager */
        $serviceManager = $event->getParam('ServiceManager');
        $this->yamlParser = $serviceManager->get('yamlParser');
    }

    /**
     * Config array retrieval from the YAML file
     */
    public function getConfig()
    {
        $config = $this->yamlParser->parseFile(__DIR__ . '/config/module.config.yaml');

        return $config;
    }
}

It is possible to use YAML files also as the main config files of the project. The main config file should similar to this one:, (*6)

modules:
  - ZendDeveloperTools
  - ZendYamlConfig
  - Application

module_listener_options:
  config_glob_paths:
    - config/autoload/{,*.}{global,local}.yaml

  module_paths:
    - ./module
    - ./vendor

Then, the app has to be initialized the following way:, (*7)

<?php

$yamlParser = ZendYamlConfig\Service\YamlFileParserFactory::getYamlFileParser();
$config = $yamlParser->parseFile(__DIR__ . '/../config/application.config.yaml');

Zend\Mvc\Application::init($config)->run();

Features

The file parser replaces every occurence of the __DIR__ constant in the YAML files with the path to the parent directory of the YAML file (basically it is the same function as the __DIR__ constant in a usual PHP file., (*8)

TODO

Parsing of the YAML files is not as fast as using plain PHP files, so a caching layer will have to be imlemented. Soon., (*9)

The Versions

21/12 2014

dev-master

9999999-dev https://github.com/Rastusik/ZendYamlConfig

Allows to use YAML configuration files in ZF2

  Sources   Download

MIT

The Requires

 

The Development Requires

framework zf2 module