Skip to content

Commit

Permalink
Merge pull request #3 from clickalicious/story-2-refactoring-general
Browse files Browse the repository at this point in the history
Refactored // Updated documentation // Cleanup // Polyfill added
  • Loading branch information
clickalicious GmbH committed Apr 3, 2016
2 parents 2b1c655 + 8cba74e commit 45fdf79
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 211 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ env:
- PREFER_LOWEST=""

before_script:
- composer update $PREFER_LOWEST
- composer update --prefer-dist $PREFER_LOWEST
- composer --optimize-autoloader --no-interaction

script:
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ echo $number;

## Requirements

- PHP >= 5.3 (compatible up to version 5.6)
- PHP >= 5.4 (compatible up to version 5.6 as well as 7.x and HHVM)


## Philosophy
Expand All @@ -64,8 +64,11 @@ For a consistent versioning i decided to make use of `Semantic Versioning 2.0.0`

## Roadmap

- [ ] More tests
- [ ] Better visualization
- [x] Focus on stable version 1.0.0
- [x] with more tests
- [x] with better visualization
- [x] with integrated polyfill
- [ ] Security check


## Security Issues
Expand Down
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@
}
],
"require": {
"php": ">=5.4.0"
"php": ">=5.4.0",
"paragonie/random_compat": "~2.0.2"
},
"require-dev": {
"phpunit/phpunit": "~4.8.9"
"phpunit/phpunit": "~4.8.9",
"symfony/var-dumper": "~2.8.4"
},
"autoload": {
"psr-4": {
"Clickalicious\\": "src/Clickalicious/"
}
},
"minimum-stability": "dev",
"keywords": [
"RNG",
"PRNG",
"Random",
"Randomize",
"Number",
"mersenne"
"Generator",
"mersenne",
"mtcrypt",
"openssl"
],
"homepage": "https://github.com/clickalicious/Rng",
"license": [
Expand Down
67 changes: 36 additions & 31 deletions src/Clickalicious/Rng/Autoloader.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

namespace Clickalicious\Rng;

/**
* Rng
* Rng.
*
* Autoloader.php - Autoloader of Rng.
*
*
* PHP versions 5.3
* PHP versions 5.4
*
* LICENSE:
* Rng - Random number generator for PHP
*
* Copyright (c) 2015, Benjamin Carl
* Copyright (c) 2015 - 2016, Benjamin Carl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,27 +46,29 @@
* Please feel free to contact us via e-mail: [email protected]
*
* @category Clickalicious
* @package Clickalicious_Rng
* @subpackage Clickalicious_Rng_Autoloader
*
* @author Benjamin Carl <[email protected]>
* @copyright 2015 Benjamin Carl
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright 2015 - 2016 Benjamin Carl
* @license https://opensource.org/licenses/BSD-2-Clause BSD-2-Clause
*
* @version Git: $Id$
*
* @link https://github.com/clickalicious/Rng
*/

/**
* Rng
* Rng.
*
* Autoloader.php - Autoloader of Rng.
*
* @category Clickalicious
* @package Clickalicious_Rng
* @subpackage Clickalicious_Rng_Autoloader
*
* @author Benjamin Carl <[email protected]>
* @copyright 2015 Benjamin Carl
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright 2015 - 2016 Benjamin Carl
* @license https://opensource.org/licenses/BSD-2-Clause BSD-2-Clause
*
* @version Git: $Id$
*
* @link https://github.com/clickalicious/Rng
*/
class Autoloader
Expand All @@ -76,40 +79,37 @@ class Autoloader
*
* @var array
*/
protected $prefixes = array();
protected $prefixes = [];

/**
* Register loader with SPL autoloader stack.
*
* @return void
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
spl_autoload_register([$this, 'loadClass']);
}

/**
* Adds a base directory for a namespace prefix.
*
* @param string $prefix The namespace prefix.
* @param string $prefix The namespace prefix.
* @param string $base_dir A base directory for class files in the
* namespace.
* @param bool $prepend If true, prepend the base directory to the stack
* instead of appending it; this causes it to be searched first rather
* than last.
* @return void
* namespace.
* @param bool $prepend If true, prepend the base directory to the stack
* instead of appending it; this causes it to be searched first rather
* than last.
*/
public function addNamespace($prefix, $base_dir, $prepend = false)
{
// normalize namespace prefix
$prefix = trim($prefix, '\\') . '\\';
$prefix = trim($prefix, '\\').'\\';

// normalize the base directory with a trailing separator
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;

// initialize the namespace prefix array
if (isset($this->prefixes[$prefix]) === false) {
$this->prefixes[$prefix] = array();
$this->prefixes[$prefix] = [];
}

// retain the base directory for the namespace prefix
Expand All @@ -124,8 +124,9 @@ public function addNamespace($prefix, $base_dir, $prepend = false)
* Loads the class file for a given class name.
*
* @param string $class The fully-qualified class name.
*
* @return mixed The mapped file name on success, or boolean false on
* failure.
* failure.
*/
public function loadClass($class)
{
Expand Down Expand Up @@ -161,10 +162,11 @@ public function loadClass($class)
/**
* Load the mapped file for a namespace prefix and relative class.
*
* @param string $prefix The namespace prefix.
* @param string $prefix The namespace prefix.
* @param string $relative_class The relative class name.
*
* @return mixed Boolean false if no mapped file can be loaded, or the
* name of the mapped file that was loaded.
* name of the mapped file that was loaded.
*/
protected function loadMappedFile($prefix, $relative_class)
{
Expand All @@ -180,8 +182,8 @@ protected function loadMappedFile($prefix, $relative_class)
// replace namespace separators with directory separators
// in the relative class name, append with .php
$file = $base_dir
. str_replace('\\', '/', $relative_class)
. '.php';
.str_replace('\\', '/', $relative_class)
.'.php';

// if the mapped file exists, require it
if ($this->requireFile($file)) {
Expand All @@ -198,14 +200,17 @@ protected function loadMappedFile($prefix, $relative_class)
* If a file exists, require it from the file system.
*
* @param string $file The file to require.
*
* @return bool True if the file exists, false if not.
*/
protected function requireFile($file)
{
if (file_exists($file)) {
require $file;
include_once $file;

return true;
}

return false;
}
}
33 changes: 17 additions & 16 deletions src/Clickalicious/Rng/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

namespace Clickalicious\Rng;

/**
* Rng
* Rng.
*
* Bootstrap.php - Bootstrapping of directories and so on.
*
*
* PHP versions 5.3
* PHP versions 5.4
*
* LICENSE:
* Rng - Plain vanilla PHP Rng client with full support of Rng protocol.
*
* Copyright (c) 2015, Benjamin Carl
* Copyright (c) 2015 - 2016, Benjamin Carl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,28 +46,29 @@
* Please feel free to contact us via e-mail: [email protected]
*
* @category Clickalicious
* @package Clickalicious_Rng
* @subpackage Clickalicious_Rng_Bootstrap
*
* @author Benjamin Carl <[email protected]>
* @copyright 2015 Benjamin Carl
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright 2015 - 2016 Benjamin Carl
* @license https://opensource.org/licenses/BSD-2-Clause BSD-2-Clause
*
* @version Git: $Id$
*
* @link https://github.com/clickalicious/Rng
*/

// Include autoloader
require_once 'Autoloader.php';

/**
* Detects composer in global scope
* Detects composer in global scope.
*
* @author Benjamin Carl <[email protected]>
*
* @return bool TRUE if composer is active, otherwise FALSE
* @access public
*/
function composer_running()
{
$result = false;
$result = false;
$classes = get_declared_classes();
natsort($classes);
foreach ($classes as $class) {
Expand All @@ -83,17 +85,16 @@ function composer_running()
// The base path to /src/ if we don't have Composer we need to know root path
define(
'CLICKALICIOUS_RNG_BASE_PATH',
realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) .
realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR).
DIRECTORY_SEPARATOR
);

// Root node
$root = realpath(CLICKALICIOUS_RNG_BASE_PATH . '../');
$root = realpath(CLICKALICIOUS_RNG_BASE_PATH.'../');

// Check for composer existence
if (true === $composerExist = $composerRunning = file_exists($root . '/vendor/autoload.php')) {
include_once $root . '/vendor/autoload.php';

if (true === $composerExist = $composerRunning = file_exists($root.'/vendor/autoload.php')) {
include_once $root.'/vendor/autoload.php';
} else {
$composerExist = $composerRunning = composer_running();
}
Expand Down Expand Up @@ -122,4 +123,4 @@ function composer_running()
$loader->register();

// register the base directories for the namespace prefix
$loader->addNamespace('Clickalicious\Rng', CLICKALICIOUS_RNG_BASE_PATH . 'Clickalicious\Rng');
$loader->addNamespace('Clickalicious\Rng', CLICKALICIOUS_RNG_BASE_PATH.'Clickalicious\Rng');
27 changes: 15 additions & 12 deletions src/Clickalicious/Rng/Exception.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

namespace Clickalicious\Rng;

/**
* Rng
* Rng.
*
* Exception.php - Exception of Rng package.
*
*
* PHP versions 5.3
* PHP versions 5.4
*
* LICENSE:
* Rng - Random number generator for PHP
*
* Copyright (c) 2015, Benjamin Carl
* Copyright (c) 2015 - 2016, Benjamin Carl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,27 +46,29 @@
* Please feel free to contact us via e-mail: [email protected]
*
* @category Clickalicious
* @package Clickalicious_Rng
* @subpackage Clickalicious_Rng_Exception
*
* @author Benjamin Carl <[email protected]>
* @copyright 2015 Benjamin Carl
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright 2015 - 2016 Benjamin Carl
* @license https://opensource.org/licenses/BSD-2-Clause BSD-2-Clause
*
* @version Git: $Id$
*
* @link https://github.com/clickalicious/Rng
*/

/**
* Rng
* Rng.
*
* Exception of Rng package.
*
* @category Clickalicious
* @package Clickalicious_Rng
* @subpackage Clickalicious_Rng_Exception
*
* @author Benjamin Carl <[email protected]>
* @copyright 2015 Benjamin Carl
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright 2015 - 2016 Benjamin Carl
* @license https://opensource.org/licenses/BSD-2-Clause BSD-2-Clause
*
* @version Git: $Id$
*
* @link https://github.com/clickalicious/Rng
*/
class Exception extends \RuntimeException
Expand Down
Loading

0 comments on commit 45fdf79

Please sign in to comment.