Airship\Engine\Gears::extract PHP Method

extract() public static method

Create a shim that extends a gear type
public static extract ( string $index, string $desired_name, string $namespace = '' ) : string
$index string
$desired_name string
$namespace string Namespace
return string
    public static function extract(string $index, string $desired_name, string $namespace = '') : string
    {
        $state = State::instance();
        if (!isset($state->gears[$index])) {
            throw new GearNotFound($index);
        }
        $latest = $state->gears[$index];
        $class_name = $desired_name;
        $testName = empty($namespace) ? $class_name : $namespace . '\\' . $class_name;
        $i = 0;
        while (\class_exists($testName, false)) {
            $class_name = $desired_name . '_' . \bin2hex('' . ++$i);
            $testName = empty($namespace) ? $class_name : $namespace . '\\' . $class_name;
        }
        // Create the shim
        if (!empty($namespace)) {
            self::coreEval('namespace ' . $namespace . ';' . "\n\n" . 'class ' . $class_name . ' extends ' . $latest . ' { }');
        } else {
            self::coreEval('class ' . $class_name . ' extends ' . $latest . ' { }');
        }
        return $class_name;
    }

Usage Example

Example #1
0
<?php

declare (strict_types=1);
namespace Airship\Cabin\Hull\Blueprint;

use Airship\Engine\Blueprint;
use Airship\Engine\Gears;
if (!\class_exists('BlueprintGear')) {
    Gears::extract('Blueprint', 'BlueprintGear', __NAMESPACE__);
    // Make autocomplete work with existing IDEs:
    if (IDE_HACKS) {
        /**
         * Class BlueprintGear
         * @package Airship\Cabin\Hull\Blueprint
         */
        class BlueprintGear extends Blueprint
        {
        }
    }
}