AutoDiscoverySpot → AutoDiscoveryDirectory + manage AutoPrefix
parent
4ecf6b6869
commit
0002837bd6
@ -1,136 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace jrosset\CliProgram;
|
|
||||||
|
|
||||||
use jrosset\BetterPhpToken\BetterPhpToken;
|
|
||||||
use LogicException;
|
|
||||||
use ReflectionClass;
|
|
||||||
use ReflectionException;
|
|
||||||
use RuntimeException;
|
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A command auto discovery spot class
|
|
||||||
*/
|
|
||||||
class AutoDiscoverySpotClass implements IAutoDiscoverySpotClass {
|
|
||||||
/**
|
|
||||||
* @var class-string The class name
|
|
||||||
*/
|
|
||||||
private string $name;
|
|
||||||
/**
|
|
||||||
* @var string The class file path
|
|
||||||
*/
|
|
||||||
private string $path;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialization
|
|
||||||
*
|
|
||||||
* @param string $name The class name
|
|
||||||
* @param string $path The class file path
|
|
||||||
*
|
|
||||||
* @throws LogicException If the file path doesn't exist
|
|
||||||
* @throws RuntimeException If the file path isn't readable
|
|
||||||
*/
|
|
||||||
public function __construct (string $name, string $path) {
|
|
||||||
$this->setPath($path);
|
|
||||||
$this->setName($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create from file path only
|
|
||||||
*
|
|
||||||
* Get the first class name of the file
|
|
||||||
*
|
|
||||||
* @param string $path The class file path
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public static function createFromFile (string $path): self {
|
|
||||||
defined('T_NAME_QUALIFIED') || define('T_NAME_QUALIFIED', 10002);
|
|
||||||
|
|
||||||
$fileHandler = fopen($path, 'r');
|
|
||||||
|
|
||||||
$class = $namespace = $buffer = '';
|
|
||||||
$currTokenGlobal = 0;
|
|
||||||
while (!feof($fileHandler)) {
|
|
||||||
$buffer .= fread($fileHandler, 512);
|
|
||||||
if (mb_strpos($buffer, '{') === false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tokens = BetterPhpToken::tokenize($buffer);
|
|
||||||
$nbTokens = count($tokens);
|
|
||||||
for (; $currTokenGlobal < $nbTokens; $currTokenGlobal++) {
|
|
||||||
$token = $tokens[$currTokenGlobal];
|
|
||||||
if ($token->is(T_NAMESPACE)) {
|
|
||||||
for ($currTokenSub = $currTokenGlobal + 1; $currTokenSub < $nbTokens; $currTokenSub++) {
|
|
||||||
$subToken = $tokens[$currTokenSub];
|
|
||||||
|
|
||||||
if ($subToken->is(T_STRING, T_NAME_QUALIFIED)) {
|
|
||||||
$namespace .= '\\' . $subToken->getText();
|
|
||||||
}
|
|
||||||
elseif ($subToken->getText() === '{' || $subToken->getText() === ';') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif ($token->is(T_CLASS) && ($currTokenGlobal === 0 || !$tokens[$currTokenGlobal - 1]->is(T_DOUBLE_COLON))) {
|
|
||||||
for ($currTokenSub = $currTokenGlobal + 1; $currTokenSub < $nbTokens; $currTokenSub++) {
|
|
||||||
$subToken = $tokens[$currTokenSub];
|
|
||||||
if ($subToken->getText() === '{') {
|
|
||||||
$class = $tokens[$currTokenGlobal + 2]->getText();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new static((mb_strlen($namespace) > 0 ? $namespace . '\\' : '') . $class, $path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getName (): string {
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Set the class name
|
|
||||||
*
|
|
||||||
* @param class-string $name The class name
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setName (string $name): self {
|
|
||||||
$this->name = $name;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getPath (): string {
|
|
||||||
return $this->path;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Set the class file path
|
|
||||||
*
|
|
||||||
* @param string $path The class file path
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @throws LogicException If the file path doesn't exist
|
|
||||||
* @throws RuntimeException If the file path isn't readable
|
|
||||||
*/
|
|
||||||
public function setPath (string $path): self {
|
|
||||||
if (!file_exists($path)) {
|
|
||||||
throw new LogicException('Invalid class path: file is missing');
|
|
||||||
}
|
|
||||||
if (!is_readable($path)) {
|
|
||||||
throw new RuntimeException('Invalid class path: file is not readable');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->path = realpath($path);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace jrosset\CliProgram;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for a command auto discovery spot class
|
|
||||||
*/
|
|
||||||
interface IAutoDiscoverySpotClass {
|
|
||||||
/**
|
|
||||||
* The class name
|
|
||||||
*
|
|
||||||
* @return class-string The class name
|
|
||||||
*/
|
|
||||||
public function getName (): string;
|
|
||||||
/**
|
|
||||||
* The class file path
|
|
||||||
*
|
|
||||||
* @return string The class file path
|
|
||||||
*/
|
|
||||||
public function getPath(): string;
|
|
||||||
}
|
|
Loading…
Reference in New Issue