| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageSeoWP; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use ImageSeoWP\Bootstrap; |
| 10 |
|
| 11 |
/** |
| 12 |
* Only use for get one context |
| 13 |
*/ |
| 14 |
abstract class Context |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* @static |
| 19 |
* @since 1.0 |
| 20 |
* @var Bootstrap|null |
| 21 |
*/ |
| 22 |
protected static $context; |
| 23 |
|
| 24 |
/** |
| 25 |
* Create context if not exist |
| 26 |
* |
| 27 |
* @static |
| 28 |
* @since 1.0 |
| 29 |
*/ |
| 30 |
public static function getContext() |
| 31 |
{ |
| 32 |
if (null !== self::$context) { |
| 33 |
return self::$context; |
| 34 |
} |
| 35 |
|
| 36 |
self::$context = new Bootstrap(); |
| 37 |
|
| 38 |
self::getClasses(__DIR__ . '/Services', 'services', 'Services\\'); |
| 39 |
self::getClasses(__DIR__ . '/Actions', 'actions', 'Actions\\'); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
return self::$context; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @static |
| 48 |
* @param string $path |
| 49 |
* @param string $type |
| 50 |
* @param string $namespace |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public static function getClasses($path, $type, $namespace = '') |
| 54 |
{ |
| 55 |
$files = array_diff(scandir($path), ['..', '.']); |
| 56 |
foreach ($files as $filename) { |
| 57 |
$pathCheck = $path . '/' . $filename; |
| 58 |
if (is_dir($pathCheck)) { |
| 59 |
self::getClasses($pathCheck, $type, $namespace . $filename . '\\'); |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
$data = '\\ImageSeoWP\\' . $namespace . str_replace('.php', '', $filename); |
| 64 |
|
| 65 |
switch ($type) { |
| 66 |
case 'services': |
| 67 |
self::$context->setService($data); |
| 68 |
break; |
| 69 |
case 'actions': |
| 70 |
self::$context->setAction($data); |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|