Skip to content

Commit

Permalink
Starting tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
meuppt committed Mar 4, 2021
0 parents commit 712209a
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
index.php
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# tonton
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "carloswph/tonton",
"description": "Serie of tools based on traits that simplify the use of singletons, multitons and other class instances controllers.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "WP Helpers",
"email": "carlos@wp-helpers.com",
"homepage": "https://wp-helpers.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"TonTon\\": "src/"
}
}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions src/Multiton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace TonTon;

trait Multiton {

/**
* @var reference to multiton array of instances
*/
private static $instances = [];

/**
* Creates a new instance of a multiton class flagged with a key.
*
* @param $key the key which the instance should be stored/retrieved
*
* @return self
*/
final public static function instance($key)
{
if(!array_key_exists($key, self::$instances)) {

self::$instances[$key] = new self;
}

return self::$instances[$key];
}

/**
* Prevents calling the class using the new keyword, if the __construct is protected.
*
* @return void
*/
private function __construct() {}

/**
* Prevents cloning the multiton instances.
*
* @return void
*/
final private function __clone() {}

/**
* Prevents unserializing the multiton instances.
*
* @return void
*/
final private function __wakeup() {}
}
48 changes: 48 additions & 0 deletions src/Singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace TonTon;

trait Singleton {

/**
* @var reference to singleton instance
*/
private static $instance;

/**
* Creates a new instance of a singleton class (via late static binding),
* accepting a variable-length argument list.
*
* @return self
*/
final public static function instance()
{
if(!self::$instance) {

self::$instance = new self;
}

return self::$instance;
}

/**
* Prevents calling the class using the new keyword, if the __construct is protected.
*
* @return void
*/
private function __construct() {}

/**
* Prevents cloning the singleton instance.
*
* @return void
*/
final private function __clone() {}

/**
* Prevents unserializing the singleton instance.
*
* @return void
*/
final private function __wakeup() {}
}

0 comments on commit 712209a

Please sign in to comment.