| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Disco |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 8 |
* @link http://domain.tld |
| 9 |
* @license GPL 2.0+ |
| 10 |
* @copyright 2022 WebAppick |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Disco\Engine; |
| 14 |
|
| 15 |
use Disco\Engine; |
| 16 |
use function apply_filters; |
| 17 |
|
| 18 |
/** |
| 19 |
* Disco Initializer |
| 20 |
*/ |
| 21 |
class Initialize { |
| 22 |
|
| 23 |
/** |
| 24 |
* List of class to initialize. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
public $classes = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Instance of this Context. |
| 32 |
* |
| 33 |
* @var object |
| 34 |
*/ |
| 35 |
protected $content = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Composer autoload file list. |
| 39 |
* |
| 40 |
* @var \Composer\Autoload\ClassLoader |
| 41 |
*/ |
| 42 |
private $composer; |
| 43 |
|
| 44 |
/** |
| 45 |
* The Constructor that load the entry classes |
| 46 |
* |
| 47 |
* @param \Composer\Autoload\ClassLoader $composer Composer autoload output. |
| 48 |
* @since 1.0.0 |
| 49 |
*/ |
| 50 |
public function __construct( \Composer\Autoload\ClassLoader $composer ) { |
| 51 |
$this->content = new Engine\Context; |
| 52 |
$this->composer = $composer; |
| 53 |
|
| 54 |
// $this->get_classes( 'Internals' ); |
| 55 |
// $this->get_classes( 'Integrations' ); |
| 56 |
|
| 57 |
if ( $this->content->request( 'rest' ) ) { |
| 58 |
$this->get_classes( 'Rest' ); |
| 59 |
} |
| 60 |
|
| 61 |
// if ( $this->content->request( 'cli' ) ) { |
| 62 |
// $this->get_classes( 'Cli' ); |
| 63 |
// } |
| 64 |
|
| 65 |
if ( $this->content->request( 'ajax' ) ) { |
| 66 |
$this->get_classes( 'Ajax' ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $this->content->request( 'backend' ) ) { |
| 70 |
$this->get_classes( 'Backend' ); |
| 71 |
$this->get_classes( 'Notice' ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( $this->content->request( 'frontend' ) ) { |
| 75 |
$this->get_classes( 'Frontend' ); |
| 76 |
} |
| 77 |
|
| 78 |
$this->load_classes(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Initialize all the classes. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
* @throws \Exception If the class does not have the initialize method. |
| 86 |
* @since 1.0.0 |
| 87 |
*/ |
| 88 |
private function load_classes() { |
| 89 |
$this->classes = apply_filters( 'disco_classes_to_execute', $this->classes ); |
| 90 |
|
| 91 |
foreach ( $this->classes as $class ) { |
| 92 |
try { |
| 93 |
$this->initialize_plugin_class( $class ); |
| 94 |
} catch ( \Throwable $err ) { |
| 95 |
\do_action( 'disco_initialize_failed', $err ); |
| 96 |
|
| 97 |
if ( WP_DEBUG ) { |
| 98 |
throw new \Exception( esc_html( $err->getMessage() ) ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Validate the class and initialize it. |
| 106 |
* |
| 107 |
* @param class-string $classtovalidate Class name to validate. |
| 108 |
* @return void |
| 109 |
* @since 1.0.0 |
| 110 |
* @SuppressWarnings("MissingImport") |
| 111 |
*/ |
| 112 |
private function initialize_plugin_class( $classtovalidate ) { |
| 113 |
$reflection = new \ReflectionClass( $classtovalidate ); |
| 114 |
|
| 115 |
if ( $reflection->isAbstract() ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
$temp = new $classtovalidate; |
| 120 |
|
| 121 |
if ( ! \method_exists( $temp, 'initialize' ) ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$temp->initialize(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Based on the folder loads the classes automatically using the Composer autoload to detect the classes of a |
| 130 |
* Namespace. |
| 131 |
* |
| 132 |
* @param string $namespacetofind Class name to find. |
| 133 |
* @return array Return the classes. |
| 134 |
* @since 1.0.0 |
| 135 |
*/ |
| 136 |
private function get_classes( string $namespacetofind ) { |
| 137 |
$prefix = $this->composer->getPrefixesPsr4(); |
| 138 |
$classmap = $this->composer->getClassMap(); |
| 139 |
$namespacetofind = 'Disco\\' . $namespacetofind; |
| 140 |
|
| 141 |
// In case composer has autoload optimized |
| 142 |
if ( isset( $classmap['Disco\\Engine\\Initialize'] ) ) { |
| 143 |
$classes = \array_keys( $classmap ); |
| 144 |
|
| 145 |
foreach ( $classes as $class ) { |
| 146 |
if ( 0 !== \strncmp( (string) $class, $namespacetofind, \strlen( $namespacetofind ) ) ) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
$this->classes[] = $class; |
| 151 |
} |
| 152 |
|
| 153 |
return $this->classes; |
| 154 |
} |
| 155 |
|
| 156 |
$namespacetofind .= '\\'; |
| 157 |
|
| 158 |
// In case composer is not optimized |
| 159 |
if ( isset( $prefix[ $namespacetofind ] ) ) { |
| 160 |
$folder = $prefix[ $namespacetofind ][0]; |
| 161 |
$php_files = $this->scandir( $folder ); |
| 162 |
$this->find_classes( $php_files, $folder, $namespacetofind ); |
| 163 |
|
| 164 |
if ( ! WP_DEBUG ) { |
| 165 |
\wp_die( \esc_html__( 'Disco is on production environment with missing `composer dumpautoload -o` that will improve the performance on autoloading itself.', 'disco' ) ); |
| 166 |
} |
| 167 |
|
| 168 |
return $this->classes; |
| 169 |
} |
| 170 |
|
| 171 |
return $this->classes; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get php files inside the folder/subfolder that will be loaded. |
| 176 |
* This class is used only when Composer is not optimized. |
| 177 |
* |
| 178 |
* @param string $folder Path. |
| 179 |
* @param string $exclude_str Exclude all files whose filename contain this. Defaults to `~`. |
| 180 |
* @return array List of files. |
| 181 |
* @since 1.0.0 |
| 182 |
*/ |
| 183 |
private function scandir( string $folder, string $exclude_str = '~' ) { |
| 184 |
// Also exclude these specific scandir findings. |
| 185 |
$blacklist = array( |
| 186 |
'..', |
| 187 |
'.', |
| 188 |
'index.php', |
| 189 |
); |
| 190 |
// Scan for files. |
| 191 |
$temp_files = \scandir( $folder ); |
| 192 |
|
| 193 |
$files = array(); |
| 194 |
|
| 195 |
if ( \is_array( $temp_files ) ) { |
| 196 |
foreach ( $temp_files as $temp_file ) { |
| 197 |
// Only include filenames that DO NOT contain the excluded string and ARE NOT on the scandir result blacklist. |
| 198 |
if ( \is_string( $exclude_str ) && false !== \mb_strpos( $temp_file, $exclude_str ) |
| 199 |
|| $temp_file[0] === '.' |
| 200 |
|| \in_array( $temp_file, $blacklist, true ) |
| 201 |
) { |
| 202 |
continue; |
| 203 |
} |
| 204 |
|
| 205 |
$files[] = $temp_file; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return $files; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Load namespace classes by files. |
| 214 |
* |
| 215 |
* @param array $php_files List of files with the Class. |
| 216 |
* @param string $folder Path of the folder. |
| 217 |
* @param string $base Namespace base. |
| 218 |
* @return void |
| 219 |
* @since 1.0.0 |
| 220 |
*/ |
| 221 |
private function find_classes( array $php_files, string $folder, string $base ) { |
| 222 |
foreach ( $php_files as $php_file ) { |
| 223 |
$class_name = \substr( $php_file, 0, -4 ); |
| 224 |
$path = $folder . '/' . $php_file; |
| 225 |
|
| 226 |
if ( \is_file( $path ) ) { |
| 227 |
$this->classes[] = $base . $class_name; |
| 228 |
|
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
// Verify the Namespace level |
| 233 |
if ( \substr_count( $base . $class_name, '\\' ) < 2 ) { |
| 234 |
continue; |
| 235 |
} |
| 236 |
|
| 237 |
if ( ! \is_dir( $path ) || \strtolower( $php_file ) === $php_file ) { |
| 238 |
continue; |
| 239 |
} |
| 240 |
|
| 241 |
$sub_php_files = $this->scandir( $folder . '/' . $php_file ); |
| 242 |
$this->find_classes( $sub_php_files, $folder . '/' . $php_file, $base . $php_file . '\\' ); |
| 243 |
}//end foreach |
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
|