Skip to content

Vendor mode in a custom project, with custom extensions #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
8 changes: 4 additions & 4 deletions bin/spc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ if (PHP_OS_FAMILY !== 'Windows' && PHP_BINARY !== (__DIR__ . '/php') && file_exi
pcntl_exec(__DIR__ . '/php', $argv);
}

if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
// Current: ./bin (git/project mode)
require_once dirname(__DIR__) . '/vendor/autoload.php';
} else {
if (file_exists(dirname(__DIR__, 3) . '/autoload.php')) {
// Current: ./vendor/crazywhalecc/static-php-cli/bin (composer library mode)
require_once dirname(__DIR__, 3) . '/autoload.php';
} else {
// Current: ./bin (git/project mode)
require_once dirname(__DIR__) . '/vendor/autoload.php';
}

// 防止 Micro 打包状态下不支持中文的显示(虽然这个项目目前好像没输出过中文?)
Expand Down
2 changes: 1 addition & 1 deletion src/SPC/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* static-php-cli console app entry
*/
final class ConsoleApplication extends Application
class ConsoleApplication extends Application
{
public const VERSION = '2.5.2';

Expand Down
1 change: 1 addition & 0 deletions src/SPC/builder/BuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function makeBuilderByInput(InputInterface $input): BuilderBase
'BSD' => new BSDBuilder($input->getOptions()),
default => throw new WrongUsageException('Current OS "' . PHP_OS_FAMILY . '" is not supported yet'),
};

return self::$builder;
}

Expand Down
6 changes: 6 additions & 0 deletions src/SPC/builder/unix/UnixBuilderBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public function proveLibs(array $sorted_libraries): void
ROOT_DIR . '/src/SPC/builder/' . osfamily2dir() . '/library',
'SPC\builder\\' . osfamily2dir() . '\library'
);
if (file_exists(WORKING_DIR . '/src/builder/' . osfamily2dir() . '/library')) {
$classes = array_merge($classes, FileSystem::getClassesPsr4(
WORKING_DIR . '/src/builder/' . osfamily2dir() . '/library',
'App\builder\\' . osfamily2dir() . '\library'
));
}
foreach ($classes as $class) {
if (defined($class . '::NAME') && $class::NAME !== 'unknown' && Config::getLib($class::NAME) !== null) {
$support_lib_list[$class::NAME] = $class;
Expand Down
6 changes: 6 additions & 0 deletions src/SPC/builder/windows/WindowsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ public function proveLibs(array $sorted_libraries): void
ROOT_DIR . '\src\SPC\builder\\' . osfamily2dir() . '\library',
'SPC\builder\\' . osfamily2dir() . '\library'
);
if (file_exists(WORKING_DIR . '\src\builder\\' . osfamily2dir() . '\library')) {
$classes = array_merge($classes, FileSystem::getClassesPsr4(
WORKING_DIR . '\src\builder\\' . osfamily2dir() . '\library',
'App\builder\\' . osfamily2dir() . '\library'
));
}
foreach ($classes as $class) {
if (defined($class . '::NAME') && $class::NAME !== 'unknown' && Config::getLib($class::NAME) !== null) {
$support_lib_list[$class::NAME] = $class;
Expand Down
12 changes: 12 additions & 0 deletions src/SPC/store/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ public static function downloadPackage(string $name, ?array $pkg = null, bool $f
break;
case 'custom': // Custom download method, like API-based download or other
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/source', 'SPC\store\source');
if (file_exists(WORKING_DIR . '/src/store/source')) {
$classes = array_merge($classes, FileSystem::getClassesPsr4(
WORKING_DIR . '/src/store/source',
'App\store\source'
));
}
foreach ($classes as $class) {
if (is_a($class, CustomSourceBase::class, true) && $class::NAME === $name) {
(new $class())->fetch($force);
Expand Down Expand Up @@ -483,6 +489,12 @@ public static function downloadSource(string $name, ?array $source = null, bool
break;
}
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/source', 'SPC\store\source');
if (file_exists(WORKING_DIR . '/src/store/source')) {
$classes = array_merge($classes, FileSystem::getClassesPsr4(
WORKING_DIR . '/src/store/source',
'App\store\source'
));
}
foreach ($classes as $class) {
if (is_a($class, CustomSourceBase::class, true) && $class::NAME === $name) {
(new $class())->fetch($force, $source, $download_as);
Expand Down
13 changes: 11 additions & 2 deletions src/SPC/store/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,25 @@ public static function loadConfigArray(string $config, ?string $config_dir = nul
WORKING_DIR . '/config/' . $config . '.json',
ROOT_DIR . '/config/' . $config . '.json',
];

$all = [];
foreach ($tries as $try) {
if (file_exists($try)) {
$json = json_decode(self::readFile($try), true);
if (!is_array($json)) {
throw new FileSystemException('Reading ' . $try . ' failed');
}
return $json;
$all = array_merge($json, $all);
}
}
throw new FileSystemException('Reading ' . $config . '.json failed');

ksort($all);

if (count($all) === 0) {
throw new FileSystemException('Reading ' . $config . '.json failed');
}

return $all;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/SPC/util/CustomExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function __construct(protected string $ext_name) {}
public static function loadCustomExt(): void
{
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/builder/extension', 'SPC\builder\extension');
if (file_exists(WORKING_DIR . '/src/builder/extension')) {
$classes = array_merge($classes, FileSystem::getClassesPsr4(WORKING_DIR . '/src/builder/extension', 'App\builder\extension'));
}
foreach ($classes as $class) {
$reflection = new \ReflectionClass($class);
foreach ($reflection->getAttributes(CustomExt::class) as $attribute) {
Expand Down