From 41d80da85924e0c61fe38b6f8e627d3d6ca53160 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Thu, 4 May 2017 11:07:35 +0200 Subject: [PATCH] prepare to tag --- .gitignore | 3 + .phpcs.xml | 27 +++ .phpdoc.xml | 13 ++ .phpmd.xml | 40 ++++ .phpunit.xml | 22 +++ .scrutinizer.yml | 20 ++ .travis.yml | 55 ++++++ LICENSE.txt | 21 +++ Makefile | 227 +++++++++++++++++++++++ README.md | 27 +++ REVISION.md | 7 + circle.yml | 13 ++ composer.json | 33 ++++ composer.lock | 66 +++++++ config/session.php | 12 ++ src/Session/Session.php | 126 +++++++++++++ src/Session/SessionConfigurable.php | 33 ++++ test/Session/SessionConfigurableTest.php | 23 +++ test/Session/SessionTest.php | 62 +++++++ test/config.php | 19 ++ 20 files changed, 849 insertions(+) create mode 100644 .gitignore create mode 100644 .phpcs.xml create mode 100644 .phpdoc.xml create mode 100644 .phpmd.xml create mode 100644 .phpunit.xml create mode 100644 .scrutinizer.yml create mode 100644 .travis.yml create mode 100644 LICENSE.txt create mode 100644 Makefile create mode 100644 README.md create mode 100644 REVISION.md create mode 100644 circle.yml create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 config/session.php create mode 100644 src/Session/Session.php create mode 100644 src/Session/SessionConfigurable.php create mode 100644 test/Session/SessionConfigurableTest.php create mode 100644 test/Session/SessionTest.php create mode 100644 test/config.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ef15d2c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.bin +/build +/vendor diff --git a/.phpcs.xml b/.phpcs.xml new file mode 100644 index 0000000..be58486 --- /dev/null +++ b/.phpcs.xml @@ -0,0 +1,27 @@ + + + Custom rule set. + + src + test + + *.tpl.* + test/config.php + + + + + + + + + diff --git a/.phpdoc.xml b/.phpdoc.xml new file mode 100644 index 0000000..5aa8a53 --- /dev/null +++ b/.phpdoc.xml @@ -0,0 +1,13 @@ + + + Anax API Documentation + + doc/api + + + doc/api + + + src + + diff --git a/.phpmd.xml b/.phpmd.xml new file mode 100644 index 0000000..a881d35 --- /dev/null +++ b/.phpmd.xml @@ -0,0 +1,40 @@ + + + + Custom rule set for htmlphp course. + + + vendor + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.phpunit.xml b/.phpunit.xml new file mode 100644 index 0000000..520de75 --- /dev/null +++ b/.phpunit.xml @@ -0,0 +1,22 @@ + + + + + + test + + + + + + src + + + + + + + + + diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..c77a8fa --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,20 @@ +imports: + - php + +filter: + excluded_paths: [vendor/*, test/*] + +build: + + dependencies: + before: + - + command: "mkdir build" + + tests: + override: + - + command: "phpunit --configuration .phpunit.xml" + coverage: + file: "build/coverage.clover" + format: "php-clover" diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..765659d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,55 @@ +language: php + +php: + - 5.6 + - "7.0" + - "7.1" + - hhvm + - nightly + + + +sudo: false + + + +git: + submodules: false + + + +addons: + apt: + packages: + #- php-codesniffer + #- phpmd + #- shellcheck + + + +matrix: + allow_failures: + - php: "7.0" + - php: "7.1" + - php: nightly + - php: hhvm + + + +before_script: + - make install + + +script: + - make check + - make test + + + +notifications: + irc: "irc.freenode.org#dbwebb" + urls: + - https://webhooks.gitter.im/e/6e4660b0024de67de0af + on_success: change # options: [always|never|change] default: always + on_failure: always # options: [always|never|change] default: always + on_start: never # options: [always|never|change] default: always diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..5772a0e --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 - 2017 Mikael Roos (mos@dbwebb.se) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eb1c4e8 --- /dev/null +++ b/Makefile @@ -0,0 +1,227 @@ +# +# +# + +# Detect OS +OS = $(shell uname -s) + +# Defaults +ECHO = echo + +# Make adjustments based on OS +# http://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux/27776822#27776822 +ifneq (, $(findstring CYGWIN, $(OS))) + ECHO = /bin/echo -e +endif + +# Colors and helptext +NO_COLOR = \033[0m +ACTION = \033[32;01m +OK_COLOR = \033[32;01m +ERROR_COLOR = \033[31;01m +WARN_COLOR = \033[33;01m + +# Which makefile am I in? +WHERE-AM-I = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) +THIS_MAKEFILE := $(call WHERE-AM-I) + +# Echo some nice helptext based on the target comment +HELPTEXT = $(ECHO) "$(ACTION)--->" `egrep "^\# target: $(1) " $(THIS_MAKEFILE) | sed "s/\# target: $(1)[ ]*-[ ]* / /g"` "$(NO_COLOR)" + +# Add local bin path for test tools +#PATH := "./.bin:./vendor/bin:./node_modules/.bin:$(PATH)" +#SHELL := env PATH=$(PATH) $(SHELL) +PHPUNIT := .bin/phpunit +PHPLOC := .bin/phploc +PHPCS := .bin/phpcs +PHPCBF := .bin/phpcbf +PHPMD := .bin/phpmd +PHPDOC := .bin/phpdoc +BEHAT := .bin/behat + + + +# target: help - Displays help. +.PHONY: help +help: + @$(call HELPTEXT,$@) + @$(ECHO) "Usage:" + @$(ECHO) " make [target] ..." + @$(ECHO) "target:" + @egrep "^# target:" $(THIS_MAKEFILE) | sed 's/# target: / /g' + + + +# target: prepare - Prepare for tests and build +.PHONY: prepare +prepare: + @$(call HELPTEXT,$@) + [ -d .bin ] || mkdir .bin + [ -d build ] || mkdir build + rm -rf build/* + + + +# target: clean - Removes generated files and directories. +.PHONY: clean +clean: + @$(call HELPTEXT,$@) + rm -rf build + + + +# target: clean-all - Removes generated files and directories. +.PHONY: clean-all +clean-all: + @$(call HELPTEXT,$@) + rm -rf .bin build vendor composer.lock + + + +# target: check - Check version of installed tools. +.PHONY: check +check: check-tools-php + @$(call HELPTEXT,$@) + + + +# target: test - Run all tests. +.PHONY: test +test: phpunit phpcs phpmd phploc behat + @$(call HELPTEXT,$@) + composer validate + + + +# target: doc - Generate documentation. +.PHONY: doc +doc: phpdoc + @$(call HELPTEXT,$@) + + + +# target: build - Do all build +.PHONY: build +build: test doc #less-compile less-minify js-minify + @$(call HELPTEXT,$@) + + + +# target: install - Install all tools +.PHONY: install +install: prepare install-tools-php + @$(call HELPTEXT,$@) + + + +# target: update - Update the codebase and tools. +.PHONY: update +update: + @$(call HELPTEXT,$@) + git pull + composer update + + + +# target: tag-prepare - Prepare to tag new version. +.PHONY: tag-prepare +tag-prepare: + @$(call HELPTEXT,$@) + + + +# ------------------------------------------------------------------------ +# +# PHP +# + +# target: install-tools-php - Install PHP development tools. +.PHONY: install-tools-php +install-tools-php: + @$(call HELPTEXT,$@) + curl -Lso $(PHPDOC) https://www.phpdoc.org/phpDocumentor.phar && chmod 755 $(PHPDOC) + + curl -Lso $(PHPCS) https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && chmod 755 $(PHPCS) + + curl -Lso $(PHPCBF) https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar && chmod 755 $(PHPCBF) + + curl -Lso $(PHPMD) http://static.phpmd.org/php/latest/phpmd.phar && chmod 755 $(PHPMD) + + curl -Lso $(PHPUNIT) https://phar.phpunit.de/phpunit-5.7.9.phar && chmod 755 $(PHPUNIT) + + curl -Lso $(PHPLOC) https://phar.phpunit.de/phploc.phar && chmod 755 $(PHPLOC) + + curl -Lso $(BEHAT) https://github.com/Behat/Behat/releases/download/v3.3.0/behat.phar && chmod 755 $(BEHAT) + + composer install + + + + +# target: check-tools-php - Check versions of PHP tools. +.PHONY: check-tools-php +check-tools-php: + @$(call HELPTEXT,$@) + which $(PHPUNIT) && $(PHPUNIT) --version + which $(PHPLOC) && $(PHPLOC) --version + which $(PHPCS) && $(PHPCS) --version && echo + which $(PHPMD) && $(PHPMD) --version && echo + which $(PHPCBF) && $(PHPCBF) --version && echo + which $(PHPDOC) && $(PHPDOC) --version && echo + which $(BEHAT) && $(BEHAT) --version && echo + + + +# target: phpunit - Run unit tests for PHP. +.PHONY: phpunit +phpunit: prepare + @$(call HELPTEXT,$@) + $(PHPUNIT) --configuration .phpunit.xml + + + +# target: phpcs - Codestyle for PHP. +.PHONY: phpcs +phpcs: prepare + @$(call HELPTEXT,$@) + $(PHPCS) --standard=.phpcs.xml | tee build/phpcs + + + +# target: phpcbf - Fix codestyle for PHP. +.PHONY: phpcbf +phpcbf: + @$(call HELPTEXT,$@) + $(PHPCBF) --standard=.phpcs.xml + + + +# target: phpmd - Mess detector for PHP. +.PHONY: phpmd +phpmd: prepare + @$(call HELPTEXT,$@) + - $(PHPMD) . text .phpmd.xml | tee build/phpmd + + + +# target: phploc - Code statistics for PHP. +.PHONY: phploc +phploc: prepare + @$(call HELPTEXT,$@) + $(PHPLOC) src > build/phploc + + + +# target: phpdoc - Create documentation for PHP. +.PHONY: phpdoc +phpdoc: + @$(call HELPTEXT,$@) + $(PHPDOC) --config=.phpdoc.xml + + + +# target: behat - Run behat for feature tests. +.PHONY: behat +behat: + @$(call HELPTEXT,$@) + [ ! -d features ] || $(BEHAT) diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b22afa --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +Anax Session +================================== + +[![Latest Stable Version](https://poser.pugx.org/anax/session/v/stable)](https://packagist.org/packages/anax/session) +[![Join the chat at https://gitter.im/mosbth/anax](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/canax?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Build Status](https://travis-ci.org/canax/session.svg?branch=master)](https://travis-ci.org/canax/session) +[![CircleCI](https://circleci.com/gh/canax/session.svg?style=svg)](https://circleci.com/gh/canax/session) +[![Build Status](https://scrutinizer-ci.com/g/canax/session/badges/build.png?b=master)](https://scrutinizer-ci.com/g/canax/session/build-status/master) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/canax/session/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/canax/session/?branch=master) +[![Code Coverage](https://scrutinizer-ci.com/g/canax/session/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/canax/session/?branch=master) +[![SensioLabsInsight](https://insight.sensiolabs.com/projects/e473b0fe-4daf-492a-b228-33a25a4d9d8a/mini.png)](https://insight.sensiolabs.com/projects/e473b0fe-4daf-492a-b228-33a25a4d9d8a) + +Anax Session module. + + + +License +------------------ + +This software carries a MIT license. + + + +``` + . +..: Copyright (c) 2013 - 2017 Mikael Roos, mos@dbwebb.se +``` diff --git a/REVISION.md b/REVISION.md new file mode 100644 index 0000000..e5e817a --- /dev/null +++ b/REVISION.md @@ -0,0 +1,7 @@ +Revision history +================================= + +v1.0.0 (2017-05-04) +--------------------------------- + +* Extracted from anax to be its own module. diff --git a/circle.yml b/circle.yml new file mode 100644 index 0000000..ebda400 --- /dev/null +++ b/circle.yml @@ -0,0 +1,13 @@ +machine: + php: + #version: 5.6.18 + + + +test: + pre: + - make install + - make check + + override: + - make test diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1d007bc --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "anax/session", + "type": "library", + "description": "Anax Session module.", + "keywords": ["boilerplate", "framework", "mvc", "micro", "education"], + "homepage": "https://dbwebb.se/anax", + "license": "MIT", + "authors": [ + { + "name": "Mikael Roos", + "email": "mos@dbwebb.se", + "homepage": "https://dbwebb.se", + "role": "Developer" + } + ], + "support": { + "issues": "https://github.com/canax/session/issues" + }, + "autoload": { + "psr-4": { + "Anax\\": "src/" + } + }, + "require": { + "php": ">=5.6" + }, + "suggest": { + "anax/configure": "^1.0" + }, + "require-dev": { + "anax/configure": "^1.0" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..d729170 --- /dev/null +++ b/composer.lock @@ -0,0 +1,66 @@ +{ + "_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" + ], + "content-hash": "73e8b68296a4cdabe999d12e40f3b206", + "packages": [], + "packages-dev": [ + { + "name": "anax/configure", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/canax/configure.git", + "reference": "fdcac4f818c0331a06c83740e7eba99e2306ee9f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/canax/configure/zipball/fdcac4f818c0331a06c83740e7eba99e2306ee9f", + "reference": "fdcac4f818c0331a06c83740e7eba99e2306ee9f", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Anax\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mikael Roos", + "email": "mos@dbwebb.se", + "homepage": "https://dbwebb.se", + "role": "Developer" + } + ], + "description": "Anax Configure module for reading config files.", + "homepage": "https://dbwebb.se/anax", + "keywords": [ + "boilerplate", + "education", + "framework", + "micro", + "mvc" + ], + "time": "2017-05-04T08:43:29+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.6" + }, + "platform-dev": [] +} diff --git a/config/session.php b/config/session.php new file mode 100644 index 0000000..7595508 --- /dev/null +++ b/config/session.php @@ -0,0 +1,12 @@ + preg_replace("/[^a-z\d]/i", "", __DIR__), + "name" => preg_replace("/[^a-z\d]/i", "", ANAX_APP_PATH), + +]; diff --git a/src/Session/Session.php b/src/Session/Session.php new file mode 100644 index 0000000..711f0cb --- /dev/null +++ b/src/Session/Session.php @@ -0,0 +1,126 @@ +has($key) + ? $_SESSION[$key] + : $default; + } + + + + /** + * Read a value from the session and unset it, this is a form of read + * once flash memory using the session. + * + * @param string $key in session variable. + * @param mixed $default default value to return when key is not set + * in the session. + * + * @return mixed value from session and null if not set. + */ + public function getOnce($key, $default = null) + { + $read = $this->get($key, $default); + $this->delete($key); + return $read; + } + + + + /** + * Set values in session. + * + * @param string $key in session variable. + * @param mixed $value to set in session. + * + * @return self + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function set($key, $value) + { + $_SESSION[$key] = $value; + return $this; + } + + + + /** + * Unset session value of this key. + * + * @param string $key in session variable. + * + * @return self + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function delete($key) + { + unset($_SESSION[$key]); + return $this; + } +} diff --git a/src/Session/SessionConfigurable.php b/src/Session/SessionConfigurable.php new file mode 100644 index 0000000..b7e80dc --- /dev/null +++ b/src/Session/SessionConfigurable.php @@ -0,0 +1,33 @@ +config['name']) + ? $this->config['name'] + : $name; + + return parent::name($name); + } +} diff --git a/test/Session/SessionConfigurableTest.php b/test/Session/SessionConfigurableTest.php new file mode 100644 index 0000000..289e73b --- /dev/null +++ b/test/Session/SessionConfigurableTest.php @@ -0,0 +1,23 @@ +configure([ + "name" => "someName" + ]); + $session->name(); + $this->assertEquals("someName", session_name(), "Session name does not match."); + } +} diff --git a/test/Session/SessionTest.php b/test/Session/SessionTest.php new file mode 100644 index 0000000..1421358 --- /dev/null +++ b/test/Session/SessionTest.php @@ -0,0 +1,62 @@ +name($name); + $this->assertEquals($name, session_name(), "Session name does not match."); + } + + + + /** + * Test + */ + public function testGetSetHas() + { + $session = new Session(); + + $ret = $session->has("key"); + $this->assertFalse($ret, "Session should not have this entry."); + + $ret = $session->get("key"); + $this->assertNull($ret, "Session should return null for this entry."); + + $ret = $session->set("key", "value"); + $ret = $session->has("key"); + $this->assertTrue($ret, "Session should have this entry."); + + $ret = $session->get("key"); + $this->assertEquals($ret, "value", "Session should have a value for this entry."); + } + + + + /** + * Test + */ + public function testGetOnce() + { + $session = new Session(); + + $ret = $session->set("key", "value"); + $ret = $session->getOnce("key"); + $this->assertEquals($ret, "value", "Session should have a value for this entry."); + + $ret = $session->get("key"); + $this->assertNull($ret, "Session should return null for this entry."); + } +} diff --git a/test/config.php b/test/config.php new file mode 100644 index 0000000..9cea987 --- /dev/null +++ b/test/config.php @@ -0,0 +1,19 @@ +