| 1 |
<?php |
| 2 |
/** |
| 3 |
* Libraries handling |
| 4 |
* |
| 5 |
* Handles all libraries (vendor) operations and versioning. |
| 6 |
* |
| 7 |
* @package Libraries |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\Library; |
| 13 |
|
| 14 |
use Decalog\System\L10n; |
| 15 |
|
| 16 |
/** |
| 17 |
* Define the libraries functionality. |
| 18 |
* |
| 19 |
* Handles all libraries (vendor) operations and versioning. |
| 20 |
* |
| 21 |
* @package System |
| 22 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Libraries { |
| 26 |
|
| 27 |
/** |
| 28 |
* The array of PSR-4 libraries used by the plugin. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @var array $libraries The PSR-4 libraries used by the plugin. |
| 32 |
*/ |
| 33 |
private static $psr4_libraries; |
| 34 |
|
| 35 |
/** |
| 36 |
* The array of mono libraries used by the plugin. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* @var array $libraries The mono libraries used by the plugin. |
| 40 |
*/ |
| 41 |
private static $mono_libraries; |
| 42 |
|
| 43 |
/** |
| 44 |
* Initializes the class and set its properties. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
*/ |
| 48 |
public function __construct() { |
| 49 |
self::init(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Defines all needed libraries. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
*/ |
| 57 |
public static function init() { |
| 58 |
self::$psr4_libraries = []; |
| 59 |
self::$psr4_libraries['monolog'] = [ |
| 60 |
'name' => 'Monolog', |
| 61 |
'prefix' => 'DLMonolog', |
| 62 |
'base' => DECALOG_VENDOR_DIR . 'monolog/', |
| 63 |
'version' => DECALOG_MONOLOG_VERSION, |
| 64 |
// phpcs:ignore |
| 65 |
'author' => 'Jordi Boggiano', |
| 66 |
'url' => 'https://github.com/Seldaek/monolog', |
| 67 |
'license' => 'mit', |
| 68 |
'langs' => 'en', |
| 69 |
]; |
| 70 |
self::$psr4_libraries['tracy'] = [ |
| 71 |
'name' => 'Tracy', |
| 72 |
'prefix' => 'Tracy', |
| 73 |
'base' => DECALOG_VENDOR_DIR . 'tracy/', |
| 74 |
'version' => '2.10.7', |
| 75 |
// phpcs:ignore |
| 76 |
'author' => 'David Grudl', |
| 77 |
'url' => 'https://github.com/nette/tracy', |
| 78 |
'license' => 'gpl3', |
| 79 |
'langs' => 'en', |
| 80 |
]; |
| 81 |
self::$psr4_libraries['prometheus'] = [ |
| 82 |
'name' => 'Prometheus PHP', |
| 83 |
'prefix' => 'Prometheus', |
| 84 |
'base' => DECALOG_VENDOR_DIR . 'prometheus/', |
| 85 |
'version' => '2.2.1', |
| 86 |
// phpcs:ignore |
| 87 |
'author' => 'Lukas Kämmerling, Rafael Dohms', |
| 88 |
'url' => 'https://github.com/PromPHP', |
| 89 |
'license' => 'apl2', |
| 90 |
'langs' => 'en', |
| 91 |
]; |
| 92 |
self::$psr4_libraries['thrift'] = [ |
| 93 |
'name' => 'Apache Thrift', |
| 94 |
'prefix' => 'Thrift', |
| 95 |
'base' => DECALOG_VENDOR_DIR . 'thrift/', |
| 96 |
'version' => '0.13.01', |
| 97 |
// phpcs:ignore |
| 98 |
'author' => 'The Apache Software Foundation', |
| 99 |
'url' => 'https://github.com/apache/thrift', |
| 100 |
'license' => 'apl2', |
| 101 |
'langs' => 'en', |
| 102 |
]; |
| 103 |
self::$psr4_libraries['jaeger'] = [ |
| 104 |
'name' => 'Jaeger Bindings for PHP OpenTracing API', |
| 105 |
'prefix' => 'Jaeger', |
| 106 |
'base' => DECALOG_VENDOR_DIR . 'jaeger/', |
| 107 |
'version' => '1.2.2', |
| 108 |
// phpcs:ignore |
| 109 |
'author' => 'Jonah George', |
| 110 |
'url' => 'https://github.com/jonahgeorge/jaeger-client-php', |
| 111 |
'license' => 'mit', |
| 112 |
'langs' => 'en', |
| 113 |
]; |
| 114 |
self::$psr4_libraries['influxdb'] = [ |
| 115 |
'name' => 'InfluxDB 2 client', |
| 116 |
'prefix' => 'InfluxDB2', |
| 117 |
'base' => DECALOG_VENDOR_DIR . 'influxdb/', |
| 118 |
'version' => '1.12.0', |
| 119 |
// phpcs:ignore |
| 120 |
'author' => 'InfluxData', |
| 121 |
'url' => 'https://github.com/influxdata/influxdb-client-php', |
| 122 |
'license' => 'mit', |
| 123 |
'langs' => 'en', |
| 124 |
]; |
| 125 |
self::$psr4_libraries['lock'] = [ |
| 126 |
'name' => 'Lock', |
| 127 |
'prefix' => 'malkusch\lock', |
| 128 |
'base' => DECALOG_VENDOR_DIR . 'lock/', |
| 129 |
'version' => '2.2', |
| 130 |
// phpcs:ignore |
| 131 |
'author' => 'Willem Stuursma-Ruwen', |
| 132 |
'url' => 'https://github.com/php-lock/lock', |
| 133 |
'license' => 'wtpfl', |
| 134 |
'langs' => 'en', |
| 135 |
]; |
| 136 |
self::$psr4_libraries['elasticsearch'] = [ |
| 137 |
'name' => 'Elasticsearch', |
| 138 |
'prefix' => 'Elastic\\Elasticsearch', |
| 139 |
'base' => DECALOG_VENDOR_DIR . 'elastic/elasticsearch/', |
| 140 |
'version' => '8.13.0', |
| 141 |
// phpcs:ignore |
| 142 |
'author' => 'Elastic', |
| 143 |
'url' => 'https://github.com/elastic/elasticsearch-php', |
| 144 |
'license' => 'apl2', |
| 145 |
'langs' => 'en', |
| 146 |
]; |
| 147 |
self::$psr4_libraries['elastictransport'] = [ |
| 148 |
'name' => 'Elastic Transport', |
| 149 |
'prefix' => 'Elastic\\Transport', |
| 150 |
'base' => DECALOG_VENDOR_DIR . 'elastic/transport/', |
| 151 |
'version' => '8.8.0', |
| 152 |
// phpcs:ignore |
| 153 |
'author' => 'Elastic', |
| 154 |
'url' => 'https://github.com/elastic/elasticsearch-php', |
| 155 |
'license' => 'apl2', |
| 156 |
'langs' => 'en', |
| 157 |
]; |
| 158 |
self::$psr4_libraries['guzzlehttp'] = [ |
| 159 |
'name' => 'GuzzleHttp', |
| 160 |
'prefix' => 'DLGuzzleHttp', |
| 161 |
'base' => DECALOG_VENDOR_DIR . 'guzzlehttp/', |
| 162 |
'version' => '7.8.1', |
| 163 |
// phpcs:ignore |
| 164 |
'author' => 'Michael Dowling', |
| 165 |
'url' => 'https://github.com/elastic/elasticsearch-php', |
| 166 |
'license' => 'mit', |
| 167 |
'langs' => 'en', |
| 168 |
]; |
| 169 |
self::$psr4_libraries['httpdiscovery'] = [ |
| 170 |
'name' => 'HTTPlug Discovery', |
| 171 |
'prefix' => 'Http\\Discovery', |
| 172 |
'base' => DECALOG_VENDOR_DIR . 'http/discovery/', |
| 173 |
'version' => '1.19.4', |
| 174 |
// phpcs:ignore |
| 175 |
'author' => 'The PHP HTTP group', |
| 176 |
'url' => 'https://github.com/php-http', |
| 177 |
'license' => 'mit', |
| 178 |
'langs' => 'en', |
| 179 |
]; |
| 180 |
self::$psr4_libraries['httpclient'] = [ |
| 181 |
'name' => 'HTTPlug', |
| 182 |
'prefix' => 'Http\\Client', |
| 183 |
'base' => DECALOG_VENDOR_DIR . 'http/client/', |
| 184 |
'version' => '2.4.0', |
| 185 |
// phpcs:ignore |
| 186 |
'author' => 'The PHP HTTP group', |
| 187 |
'url' => 'https://github.com/php-http', |
| 188 |
'license' => 'mit', |
| 189 |
'langs' => 'en', |
| 190 |
]; |
| 191 |
self::$psr4_libraries['httpclientcommon'] = [ |
| 192 |
'name' => 'HTTP Client Common', |
| 193 |
'prefix' => 'Http\\Client\\Common', |
| 194 |
'base' => DECALOG_VENDOR_DIR . 'http/client-common/', |
| 195 |
'version' => '2.7.1', |
| 196 |
// phpcs:ignore |
| 197 |
'author' => 'The PHP HTTP group', |
| 198 |
'url' => 'https://github.com/php-http', |
| 199 |
'license' => 'mit', |
| 200 |
'langs' => 'en', |
| 201 |
]; |
| 202 |
self::$psr4_libraries['httppromise'] = [ |
| 203 |
'name' => 'HTTP Promise', |
| 204 |
'prefix' => 'Http\\Promise', |
| 205 |
'base' => DECALOG_VENDOR_DIR . 'http/promise/', |
| 206 |
'version' => '1.3.1', |
| 207 |
// phpcs:ignore |
| 208 |
'author' => 'The PHP HTTP group', |
| 209 |
'url' => 'https://github.com/php-http', |
| 210 |
'license' => 'mit', |
| 211 |
'langs' => 'en', |
| 212 |
]; |
| 213 |
|
| 214 |
self::$psr4_libraries['symfonyoptionsresolver'] = [ |
| 215 |
'name' => 'Symfony Options Resolver', |
| 216 |
'prefix' => 'Symfony\\Component\\OptionsResolver', |
| 217 |
'base' => DECALOG_VENDOR_DIR . 'symfony/OptionsResolver/', |
| 218 |
'version' => '6.4', |
| 219 |
// phpcs:ignore |
| 220 |
'author' => 'Fabien Potencier', |
| 221 |
'url' => 'https://github.com/symfony/symfony', |
| 222 |
'license' => 'mit', |
| 223 |
'langs' => 'en', |
| 224 |
]; |
| 225 |
self::$psr4_libraries['react'] = [ |
| 226 |
'name' => 'ReactPHP Promise', |
| 227 |
'prefix' => 'React', |
| 228 |
'base' => DECALOG_VENDOR_DIR . 'react/', |
| 229 |
'version' => '2.7.1', |
| 230 |
// phpcs:ignore |
| 231 |
'author' => 'ReactPHP', |
| 232 |
'url' => 'https://github.com/elastic/elasticsearch-php', |
| 233 |
'license' => 'mit', |
| 234 |
'langs' => 'en', |
| 235 |
]; |
| 236 |
self::$psr4_libraries['sentry'] = [ |
| 237 |
'name' => 'Sentry SDK', |
| 238 |
'prefix' => 'Sentry', |
| 239 |
'base' => DECALOG_VENDOR_DIR . 'sentry/', |
| 240 |
'version' => '3.1.5', |
| 241 |
// phpcs:ignore |
| 242 |
'author' => 'Michael Van Tellingen', |
| 243 |
'url' => 'https://github.com/getsentry/sentry-php', |
| 244 |
'license' => 'ukn', |
| 245 |
'langs' => 'en', |
| 246 |
]; |
| 247 |
self::$psr4_libraries['feather'] = [ |
| 248 |
'name' => 'Feather', |
| 249 |
'prefix' => 'Feather', |
| 250 |
'base' => DECALOG_VENDOR_DIR . 'feather/', |
| 251 |
'version' => '4.24.1', |
| 252 |
// phpcs:ignore |
| 253 |
'author' => 'Cole Bemis', |
| 254 |
'url' => 'https://feathericons.com', |
| 255 |
'license' => 'mit', |
| 256 |
'langs' => 'en', |
| 257 |
]; |
| 258 |
if ( function_exists( 'decalog_get_psr_log_version' ) ) { |
| 259 |
$psrlog_version = decalog_get_psr_log_version(); |
| 260 |
} else { |
| 261 |
$psrlog_version = 1; |
| 262 |
} |
| 263 |
self::$psr4_libraries['psr-03'] = [ |
| 264 |
'name' => 'PSR-3', |
| 265 |
'prefix' => 'Psr\\Log', |
| 266 |
'base' => DECALOG_VENDOR_DIR . 'psr/log-v' . $psrlog_version . '/', |
| 267 |
'version' => $psrlog_version . '.0.0', |
| 268 |
'author' => 'PHP Framework Interop Group', |
| 269 |
'url' => 'https://www.php-fig.org/', |
| 270 |
'license' => 'mit', |
| 271 |
'langs' => 'en', |
| 272 |
]; |
| 273 |
self::$psr4_libraries['psr-07'] = [ |
| 274 |
'name' => 'PSR-7', |
| 275 |
'prefix' => 'Psr\\Http\\Message', |
| 276 |
'base' => DECALOG_VENDOR_DIR . 'psr/http-message/', |
| 277 |
'version' => '2.0', |
| 278 |
'author' => 'PHP Framework Interop Group', |
| 279 |
'url' => 'https://www.php-fig.org/', |
| 280 |
'license' => 'mit', |
| 281 |
'langs' => 'en', |
| 282 |
]; |
| 283 |
|
| 284 |
self::$psr4_libraries['psr-11'] = [ |
| 285 |
'name' => 'PSR-11', |
| 286 |
'prefix' => 'Psr\\Container', |
| 287 |
'base' => DECALOG_VENDOR_DIR . 'psr/container/', |
| 288 |
'version' => '', |
| 289 |
'author' => 'PHP Framework Interop Group', |
| 290 |
'url' => 'https://www.php-fig.org/', |
| 291 |
'license' => 'mit', |
| 292 |
'langs' => 'en', |
| 293 |
]; |
| 294 |
self::$psr4_libraries['psr-18'] = [ |
| 295 |
'name' => 'PSR-18', |
| 296 |
'prefix' => 'Psr\\Http\\Client', |
| 297 |
'base' => DECALOG_VENDOR_DIR . 'psr/http-client/', |
| 298 |
'version' => '1.0.3', |
| 299 |
'author' => 'PHP Framework Interop Group', |
| 300 |
'url' => 'https://www.php-fig.org/', |
| 301 |
'license' => 'mit', |
| 302 |
'langs' => 'en', |
| 303 |
]; |
| 304 |
self::$psr4_libraries['markdown'] = [ |
| 305 |
'name' => 'Markdown Parser', |
| 306 |
'prefix' => 'cebe\markdownparser', |
| 307 |
'base' => DECALOG_VENDOR_DIR . 'markdown/', |
| 308 |
'version' => '1.2.1', |
| 309 |
// phpcs:ignore |
| 310 |
'author' => 'Carsten Brandt', |
| 311 |
'url' => 'https://github.com/cebe/markdown', |
| 312 |
'license' => 'mit', |
| 313 |
'langs' => 'en', |
| 314 |
]; |
| 315 |
self::$psr4_libraries['spatie-ray'] = [ |
| 316 |
'name' => 'Ray', |
| 317 |
'prefix' => 'DLSpatie\\Ray', |
| 318 |
'base' => DECALOG_VENDOR_DIR . 'spatie/ray/src/', |
| 319 |
'version' => '1.33.0', |
| 320 |
'author' => 'Spatie', |
| 321 |
'url' => 'https://github.com/spatie/ray', |
| 322 |
'license' => 'mit', |
| 323 |
'langs' => 'en', |
| 324 |
]; |
| 325 |
self::$psr4_libraries['spatie-macroable'] = [ |
| 326 |
'name' => 'Macroable', |
| 327 |
'prefix' => 'DLSpatie\\Macroable', |
| 328 |
'base' => DECALOG_VENDOR_DIR . 'spatie/macroable/src/', |
| 329 |
'version' => '2.0.0', |
| 330 |
'author' => 'Spatie', |
| 331 |
'url' => 'https://github.com/spatie/macroable', |
| 332 |
'license' => 'mit', |
| 333 |
'langs' => 'en', |
| 334 |
]; |
| 335 |
self::$psr4_libraries['spatie-backtrace'] = [ |
| 336 |
'name' => 'Backtrace', |
| 337 |
'prefix' => 'DLSpatie\\Backtrace', |
| 338 |
'base' => DECALOG_VENDOR_DIR . 'spatie/backtrace/src/', |
| 339 |
'version' => '1.2.1', |
| 340 |
'author' => 'Spatie', |
| 341 |
'url' => 'https://github.com/spatie/backtrace', |
| 342 |
'license' => 'mit', |
| 343 |
'langs' => 'en', |
| 344 |
]; |
| 345 |
self::$mono_libraries = []; |
| 346 |
self::$mono_libraries['spyc'] = [ |
| 347 |
'name' => 'Spyc', |
| 348 |
'detect' => 'Spyc', |
| 349 |
'base' => DECALOG_VENDOR_DIR . 'spyc/', |
| 350 |
'version' => '0.6.2', |
| 351 |
// phpcs:ignore |
| 352 |
'author' => 'Vlad Andersen, Chris Wanstrath', |
| 353 |
'url' => 'https://github.com/mustangostang/spyc/', |
| 354 |
'license' => 'mit', |
| 355 |
'langs' => 'en', |
| 356 |
]; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Get PSR-4 libraries. |
| 361 |
* |
| 362 |
* @return array The list of defined PSR-4 libraries. |
| 363 |
* @since 1.0.0 |
| 364 |
*/ |
| 365 |
public static function get_psr4() { |
| 366 |
return self::$psr4_libraries; |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Get mono libraries. |
| 371 |
* |
| 372 |
* @return array The list of defined mono libraries. |
| 373 |
* @since 1.0.0 |
| 374 |
*/ |
| 375 |
public static function get_mono() { |
| 376 |
return self::$mono_libraries; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Compare two items based on name field. |
| 381 |
* |
| 382 |
* @param array $a The first element. |
| 383 |
* @param array $b The second element. |
| 384 |
* @return boolean True if $a>$b, false otherwise. |
| 385 |
* @since 1.0.0 |
| 386 |
*/ |
| 387 |
public function reorder_list( $a, $b ) { |
| 388 |
return strcmp( strtolower( $a['name'] ), strtolower( $b['name'] ) ); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Get the full license name. |
| 393 |
* |
| 394 |
* @param string $license The license id. |
| 395 |
* @return string The full license name. |
| 396 |
* @since 1.0.0 |
| 397 |
*/ |
| 398 |
private function license_name( $license ) { |
| 399 |
switch ( $license ) { |
| 400 |
case 'mit': |
| 401 |
$result = decalog_esc_html__( 'MIT license', 'decalog' ); |
| 402 |
break; |
| 403 |
case 'wtpfl': |
| 404 |
$result = decalog_esc_html__( 'WTFPL license', 'decalog' ); |
| 405 |
break; |
| 406 |
case 'apl2': |
| 407 |
$result = decalog_esc_html__( 'Apache license, version 2.0', 'decalog' ); |
| 408 |
break; |
| 409 |
case 'gpl2': |
| 410 |
$result = decalog_esc_html__( 'GPL-2.0 license', 'decalog' ); |
| 411 |
break; |
| 412 |
case 'gpl3': |
| 413 |
$result = decalog_esc_html__( 'GPL-3.0 license', 'decalog' ); |
| 414 |
break; |
| 415 |
default: |
| 416 |
$result = decalog_esc_html__( 'unknown license', 'decalog' ); |
| 417 |
break; |
| 418 |
} |
| 419 |
return $result; |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Get the libraries list. |
| 424 |
* |
| 425 |
* @param array $attributes 'style' => 'html'. |
| 426 |
* @return string The output of the shortcode, ready to print. |
| 427 |
* @since 1.0.0 |
| 428 |
*/ |
| 429 |
public function sc_get_list( $attributes ) { |
| 430 |
$_attributes = shortcode_atts( |
| 431 |
[ |
| 432 |
'style' => 'html', |
| 433 |
], |
| 434 |
$attributes |
| 435 |
); |
| 436 |
$style = $_attributes['style']; |
| 437 |
$result = ''; |
| 438 |
$list = []; |
| 439 |
foreach ( array_merge( self::get_psr4(), self::get_mono() ) as $library ) { |
| 440 |
$item = []; |
| 441 |
$item['name'] = $library['name']; |
| 442 |
$item['version'] = $library['version']; |
| 443 |
$item['author'] = $library['author']; |
| 444 |
$item['url'] = $library['url']; |
| 445 |
$item['license'] = $this->license_name( $library['license'] ); |
| 446 |
$item['langs'] = L10n::get_language_markup( explode( ',', $library['langs'] ) ); |
| 447 |
$list[] = $item; |
| 448 |
} |
| 449 |
$item = []; |
| 450 |
$item['name'] = 'Plugin Boilerplate'; |
| 451 |
$item['version'] = ''; |
| 452 |
$item['author'] = 'Pierre Lannoy'; |
| 453 |
// phpcs:ignore |
| 454 |
$item['url'] = 'https://github.com/Pierre-Lannoy/wp-' . 'plugin-' . 'boilerplate'; |
| 455 |
$item['license'] = $this->license_name( 'gpl3' ); |
| 456 |
$item['langs'] = L10n::get_language_markup( [ 'en' ] ); |
| 457 |
$list[] = $item; |
| 458 |
usort( |
| 459 |
$list, |
| 460 |
function ( $a, $b ) { |
| 461 |
return strcmp( strtolower( $a['name'] ), strtolower( $b['name'] ) ); |
| 462 |
} |
| 463 |
); |
| 464 |
if ( 'html' === $style ) { |
| 465 |
$items = []; |
| 466 |
foreach ( $list as $library ) { |
| 467 |
/* translators: as in the sentence "Product W version X by author Y (license Z)" */ |
| 468 |
$items[] = sprintf(decalog__( '<a href="%1$s">%2$s %3$s</a>%4$s by %5$s & contributors (%6$s)', 'decalog' ), $library['url'], $library['name'], 'v' . $library['version'], $library['langs'], $library['author'], $library['license'] ); |
| 469 |
} |
| 470 |
$result = implode( ', ', $items ); |
| 471 |
} |
| 472 |
return $result; |
| 473 |
} |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
Libraries::init(); |
| 478 |
|