From ffd36207917c1a00154f038b4a50b8497415ddf0 Mon Sep 17 00:00:00 2001 From: Holger Woltersdorf Date: Thu, 7 Jul 2016 01:11:49 +0200 Subject: [PATCH] Adding global data mappers does not reserve the '*' key anymore, raised composer dev-dependency of tm/tooly-composer-script to ^1.0, Fixed author tags, Added changelog --- .gitignore | 1 - CHANGELOG.md | 21 +++++++ composer.json | 2 +- composer.lock | 82 +++++++++++++++++++++++++++ src/AbstractSession.php | 88 +++++++++++++++++------------ src/Exceptions/SessionException.php | 2 +- src/Interfaces/MapsSessionData.php | 2 +- tests/Unit/AbstractSessionTest.php | 2 +- tests/Unit/Fixtures/DataMapper.php | 2 +- tests/Unit/Fixtures/Session.php | 2 +- 10 files changed, 162 insertions(+), 42 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 composer.lock diff --git a/.gitignore b/.gitignore index e014a25..5037408 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ vagrant # composer vendor/ -composer.lock # locally generated docs build/api/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..815bbdf --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,21 @@ +# Change Log +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com). + +## [1.0.1] - 2016-07-06 + +### Added + +- Changelog + +### Changed + +- Adding global data mappers does not reserve the '*' key anymore +- raised composer dev-dependency of `tm/tooly-composer-script` to `^1.0` +- Fixed author tags + +## 1.0.0 - 2016-07-05 + +- First release + +[1.0.1]: https://github.com/icehawk/session/compare/v1.0.0...v1.0.1 \ No newline at end of file diff --git a/composer.json b/composer.json index 36476ae..2ef4d68 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "php": ">=7.0" }, "require-dev": { - "tm/tooly-composer-script": "^0.1.1" + "tm/tooly-composer-script": "^1.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..b16ae56 --- /dev/null +++ b/composer.lock @@ -0,0 +1,82 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "6e7a28bb6f9dfe788b0c667abc290df8", + "content-hash": "5adfeb7e3769dc3af06cfe676d658ed5", + "packages": [], + "packages-dev": [ + { + "name": "tm/tooly-composer-script", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/tommy-muehle/tooly-composer-script.git", + "reference": "1e29176f345d09afd328bdcb56805d94c0e3abbe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tommy-muehle/tooly-composer-script/zipball/1e29176f345d09afd328bdcb56805d94c0e3abbe", + "reference": "1e29176f345d09afd328bdcb56805d94c0e3abbe", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "codeclimate/php-test-reporter": "dev-master", + "composer/composer": "1.*", + "mikey179/vfsstream": "1.6.*" + }, + "type": "library", + "extra": { + "tools": { + "phpunit": { + "url": "https://phar.phpunit.de/phpunit-4.8.9.phar", + "only-dev": true + }, + "phpcpd": { + "url": "https://phar.phpunit.de/phpcpd-2.0.4.phar", + "only-dev": true + } + } + }, + "autoload": { + "psr-4": { + "Tooly\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Tommy Muehle", + "email": "tommy.muehle@gmail.com", + "homepage": "https://tommy-muehle.de" + } + ], + "description": "Simple composer script to manage phar files.", + "homepage": "https://github.com/tommy-muehle/tooly-composer-script", + "keywords": [ + "composer", + "composer-phar", + "composer-script", + "phar-management" + ], + "time": "2016-07-06 12:47:04" + } + ], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": true, + "prefer-lowest": false, + "platform": { + "php": ">=7.0" + }, + "platform-dev": [] +} diff --git a/src/AbstractSession.php b/src/AbstractSession.php index 110eaee..e722f91 100644 --- a/src/AbstractSession.php +++ b/src/AbstractSession.php @@ -1,6 +1,6 @@ sessionData = &$sessionData; - $this->dataMappers = [ ]; + $this->sessionData = &$sessionData; + $this->keyDataMappers = [ ]; + $this->globalDataMappers = [ ]; } final public function addDataMapper( MapsSessionData $dataMapper, array $keys = [ ] ) { if ( empty($keys) ) { - $keys = [ '*' ]; + $this->addGlobalDataMapper( $dataMapper ); } - - foreach ( array_unique( $keys ) as $key ) + else { - if ( isset($this->dataMappers[ $key ]) ) + foreach ( array_unique( $keys ) as $key ) { - if ( !in_array( $dataMapper, $this->dataMappers[ $key ] ) ) - { - $this->dataMappers[ $key ][] = $dataMapper; - } + $this->addKeyDataMapper( $dataMapper, $key ); } - else + } + } + + private function addGlobalDataMapper( MapsSessionData $dataMapper ) + { + if ( !in_array( $dataMapper, $this->globalDataMappers ) ) + { + $this->globalDataMappers[] = $dataMapper; + } + } + + private function addKeyDataMapper( MapsSessionData $dataMapper, string $key ) + { + if ( isset($this->keyDataMappers[ $key ]) ) + { + if ( !in_array( $dataMapper, $this->keyDataMappers[ $key ] ) ) { - $this->dataMappers[ $key ] = [ $dataMapper ]; + $this->keyDataMappers[ $key ][] = $dataMapper; } } + else + { + $this->keyDataMappers[ $key ] = [ $dataMapper ]; + } } final protected function set( string $key, $value ) @@ -55,27 +74,19 @@ final protected function set( string $key, $value ) private function mapValueToSessionData( string $key, $value ) { - $mappers = $this->getDataMappersForKey( $key ); + $keyDataMappers = $this->keyDataMappers[ $key ] ?? [ ]; - foreach ( $mappers as $mapper ) + foreach ( $keyDataMappers as $keyDataMapper ) { - $value = $mapper->toSessionData( $value ); + $value = $keyDataMapper->toSessionData( $value ); } - return $value; - } - - /** - * @param string $key - * - * @return array|MapsSessionData[] - */ - private function getDataMappersForKey( string $key ) : array - { - $mappers = $this->dataMappers[ $key ] ?? [ ]; - $mappers = array_merge( $mappers, $this->dataMappers['*'] ?? [ ] ); + foreach ( $this->globalDataMappers as $globalDataMapper ) + { + $value = $globalDataMapper->toSessionData( $value ); + } - return $mappers; + return $value; } final protected function get( string $key ) @@ -87,13 +98,20 @@ private function mapValueFromSessionData( string $key ) { if ( $this->isset( $key ) ) { - $value = $this->sessionData[ $key ]; - $mappers = $this->getDataMappersForKey( $key ); + $value = $this->sessionData[ $key ]; + $globalDataMappers = array_reverse( $this->globalDataMappers ); + $keyDataMappers = array_reverse( $this->keyDataMappers[ $key ] ?? [ ] ); + + /** @var MapsSessionData $globalDataMapper */ + foreach ( $globalDataMappers as $globalDataMapper ) + { + $value = $globalDataMapper->fromSessionData( $value ); + } - /** @var MapsSessionData $mapper */ - foreach ( array_reverse( $mappers ) as $mapper ) + /** @var MapsSessionData $keyDataMapper */ + foreach ( $keyDataMappers as $keyDataMapper ) { - $value = $mapper->fromSessionData( $value ); + $value = $keyDataMapper->fromSessionData( $value ); } return $value; diff --git a/src/Exceptions/SessionException.php b/src/Exceptions/SessionException.php index 62ba3ed..63d09b8 100644 --- a/src/Exceptions/SessionException.php +++ b/src/Exceptions/SessionException.php @@ -1,6 +1,6 @@