Move into wp-content path

Signed-off-by: Adrian Nöthlich <git@promasu.tech>
This commit is contained in:
2019-08-31 00:48:20 +02:00
parent f523d8ccc0
commit 3724cc6edd
342 changed files with 108652 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Andy Fragen
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.

View File

@@ -0,0 +1,35 @@
# singleton
This is a singleton static proxy generator that I use in several projects instead of creating true Singletons. It was inspired by [Alain Schlessers post on Singletons](https://www.alainschlesser.com/singletons-shared-instances/).
Ive moved this library into its own repository so that I will be better able to include it via composer.
I have written it to work with PSR-4.
`composer require afragen/singleton:dev-master`
When using this Singleton class in your project you will create an array of class instances.
## Usage
```php
@param string $class_name
@param object $caller Originating object.
@param null|array|\stdClass $options
Singleton::get_instance( $class_name, $calling_class, $options );
```
This will usually be called as follows.
`Singleton::get_instance( 'MyClass', $this );`
The class object created will also pass the calling object as `$instance[$class_name]->caller`.
I do my best to automatically determine the namespace of the class. If the class is in a subfolder of `src` it will need to be designated in the call as follows.
If PSR-4 is set for the `src` directory and the class lives in `src/MySubDir/MyClass` the corresponding call would be as follows.
`Singleton::get_instance( 'MySubDir\MyClass', $this );`
Im still learning how to properly set up using composer so this may be updated along the way.

View File

@@ -0,0 +1,108 @@
<?php
/**
* Singleton Static Proxy
*
* @author Andy Fragen
* @license MIT
* @link https://github.com/afragen/singleton
* @package singleton
*/
namespace Fragen;
/*
* Exit if called directly.
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Fragen\\Singleton' ) ) {
/**
* Class Singleton
*
* A static proxy for creating Singletons from passed class names.
*
* @version 1.0.0
*/
final class Singleton {
/**
* Get instance of class.
*
* @param string $class_name
* @param object $caller Originating object.
* @param null|array|\stdClass $options
*
* @return array $instance
*/
public static function get_instance( $class_name, $caller = null, $options = null ) {
static $instance = null;
$class = get_class( $caller );
$class = self::get_class( $class_name, $class );
if ( null === $instance || ! isset( $instance[ $class ] ) ) {
$instance[ $class ] = new $class( $options );
}
// Add calling object.
$instance[ $class ]->caller = $caller;
return $instance[ $class ];
}
/**
* Determine correct class name with namespace and return.
*
* @param string $class_name
* @param string $class
*
* @return string Namespaced class name.
*/
private static function get_class( $class_name, $class ) {
$reflection = self::get_reflection( $class );
$namespace = $reflection->getNamespaceName();
$namespace_parts = explode( '\\', $namespace );
$count = count( $namespace_parts );
$classes[-1] = null;
for ( $i = 0; $i < $count; $i++ ) {
$classes[ $i ] = ltrim( $classes[ $i - 1 ] . '\\' . $namespace_parts[ $i ], '\\' );
}
$classes = array_reverse( $classes );
foreach ( $classes as $namespace ) {
$namespaced_class = $namespace . '\\' . $class_name;
if ( class_exists( $namespaced_class ) ) {
return $namespaced_class;
}
}
try {
throw new \Exception( "Undefined class '{$class_name}'" );
} catch ( \Exception $e ) {
$message = "PHP Fatal error: {$e->getMessage()}\nPHP Stack trace:\n";
$trace = $e->getTraceAsString();
error_log( $message . $trace );
die( "<pre><strong>{$message}</strong>{$trace}</pre>" );
}
}
/**
* Get ReflectionClass of passed class name.
*
* @param string $class
*
* @return \ReflectionClass $reflection
*/
private static function get_reflection( $class ) {
try {
$reflection = new \ReflectionClass( $class );
} catch ( \ReflectionException $Exception ) {
die( '<table>' . $Exception->xdebug_message . '</table>' );
}
return $reflection;
}
}
}

View File

@@ -0,0 +1,33 @@
{
"name": "afragen/singleton",
"description": "A singleton static proxy generator.",
"type": "library",
"keywords": [
"wordpress",
"singleton"
],
"license": "MIT",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/afragen/singleton"
}
],
"authors": [
{
"name": "Andy Fragen",
"email": "andy@thefragens.com",
"homepage": "https://github.com/afragen/singleton",
"role": "Developer"
}
],
"prefer-stable": true,
"require": {
"php": ">=5.4"
},
"autoload": {
"classmap": [
"Singleton.php"
]
}
}