Skip to content

Commit

Permalink
add configuration for bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Dec 7, 2016
1 parent 9de9506 commit 6b27e7e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public function registerBundles()
}
```

## Configuration

```yml
gps_lab_date:
# Is not required
# As a default uset timezone from date_default_timezone_get()
time_zone: 'Europe/Moscow'

# HTTP Cookie var names for store user timezone
# It's a default values
cookie:
name: '_time_zone_name'
offset: '_time_zone_offset'
```

## Usage

### Util
Expand Down Expand Up @@ -288,16 +303,6 @@ How get timezone keeper from DI?
$tz_keeper = $this->get('gpslab.date.tz.keeper');
```

### Parameters

You can change DI parameter `%time_zone%` for change default timezone. As a default used timezone from
[date_default_timezone_get()](http://php.net/manual/en/function.date-default-timezone-get.php).

User timezone as a default stored in HTTP Cookies. DI parameter is store cookies var name:

* Parameter `%date.time_zone.param.name%` is var name for `\DateTimeZone::getName()`;
* Parameter `%date.time_zone.param.offset%` is var name for `\DateTimeZone::getOffset()`.

## License

This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE
52 changes: 52 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* GpsLab component.
*
* @author Peter Gribanov <info@peter-gribanov.ru>
* @copyright Copyright (c) 2016, Peter Gribanov
* @license http://opensource.org/licenses/MIT
*/

namespace GpsLab\Bundle\DateBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* Config tree builder.
*
* Example config:
*
* gps_lab_date:
* time_zone: 'Europe/Moscow'
* cookie:
* name: '_time_zone_name'
* offset: '_time_zone_offset'
*
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
return (new TreeBuilder())
->root('gps_lab_date')
->addDefaultsIfNotSet()
->children()
->scalarNode('time_zone')
->end()
->arrayNode('cookie')
->children()
->scalarNode('name')
->cannotBeEmpty()
->defaultValue('_time_zone_name')
->end()
->scalarNode('offset')
->cannotBeEmpty()
->defaultValue('_time_zone_offset')
->end()
->end()
->end()
->end();
}
}
7 changes: 6 additions & 1 deletion src/DependencyInjection/GpsLabDateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class GpsLabDateExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);

$container->setParameter('date.time_zone', $config['time_zone']);
$container->setParameter('date.time_zone.param.name', $config['cookie']['name']);
$container->setParameter('date.time_zone.param.offset', $config['cookie']['offset']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('parameters.yml');
$loader->load('services.yml');
}
}
4 changes: 0 additions & 4 deletions src/Resources/config/parameters.yml

This file was deleted.

2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:

gpslab.date.tz.resolver.console:
class: GpsLab\Bundle\DateBundle\Date\TimeZone\Resolver\ConsoleResolver
arguments: [ '%time_zone%' ]
arguments: [ '%date.time_zone%' ]
tags:
- { name: gpslab.date.tz.resolver, priority: 900 }
public: false
Expand Down

0 comments on commit 6b27e7e

Please sign in to comment.