Skip to content

Commit acd0e2b

Browse files
committed
Prepare for skeleton
1 parent c1c31a7 commit acd0e2b

File tree

10 files changed

+447
-18
lines changed

10 files changed

+447
-18
lines changed

skeleton-test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use StaticPHP\Skeleton\ArtifactGenerator;
4+
use StaticPHP\Skeleton\PackageGenerator;
5+
6+
7+
require_once 'vendor/autoload.php';
8+
9+
$package_generator = new PackageGenerator('foo', 'library')
10+
->addDependency('bar')
11+
->addStaticLib('libfoo.a', 'unix')
12+
->addStaticLib('libfoo.a', 'unix')
13+
->addArtifact($artifact_generator = new ArtifactGenerator('foo')->setSource(['type' => 'url', 'url' => 'https://example.com/foo.tar.gz']));
14+
15+
$pkg_config = $package_generator->generateConfig();
16+
$artifact_config = $artifact_generator->generateConfig();
17+
18+
echo "===== pkg.json =====" . PHP_EOL;
19+
echo json_encode($pkg_config, 64|128|256) . PHP_EOL;
20+
echo "===== artifact.json =====" . PHP_EOL;
21+
echo json_encode($artifact_config, 64|128|256) . PHP_EOL;

src/Package/Artifact/zig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use StaticPHP\Artifact\Downloader\DownloadResult;
99
use StaticPHP\Attribute\Artifact\AfterBinaryExtract;
1010
use StaticPHP\Attribute\Artifact\CustomBinary;
11+
use StaticPHP\Attribute\Artifact\CustomSource;
1112
use StaticPHP\Exception\DownloaderException;
1213
use StaticPHP\Runtime\SystemTarget;
1314

src/StaticPHP/Config/ArtifactConfig.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@
55
namespace StaticPHP\Config;
66

77
use StaticPHP\Exception\WrongUsageException;
8+
use StaticPHP\Registry\Registry;
89

910
class ArtifactConfig
1011
{
1112
private static array $artifact_configs = [];
1213

13-
public static function loadFromDir(string $dir): void
14+
public static function loadFromDir(string $dir, string $registry_name): void
1415
{
1516
if (!is_dir($dir)) {
1617
throw new WrongUsageException("Directory {$dir} does not exist, cannot load artifact config.");
1718
}
1819
$files = glob("{$dir}/artifact.*.json");
1920
if (is_array($files)) {
2021
foreach ($files as $file) {
21-
self::loadFromFile($file);
22+
self::loadFromFile($file, $registry_name);
2223
}
2324
}
2425
if (file_exists("{$dir}/artifact.json")) {
25-
self::loadFromFile("{$dir}/artifact.json");
26+
self::loadFromFile("{$dir}/artifact.json", $registry_name);
2627
}
2728
}
2829

2930
/**
3031
* Load artifact configurations from a specified JSON file.
3132
*/
32-
public static function loadFromFile(string $file): void
33+
public static function loadFromFile(string $file, string $registry_name): void
3334
{
3435
$content = file_get_contents($file);
3536
if ($content === false) {
@@ -42,6 +43,7 @@ public static function loadFromFile(string $file): void
4243
ConfigValidator::validateAndLintArtifacts(basename($file), $data);
4344
foreach ($data as $artifact_name => $config) {
4445
self::$artifact_configs[$artifact_name] = $config;
46+
Registry::_bindArtifactConfigFile($artifact_name, $registry_name, $file);
4547
}
4648
}
4749

src/StaticPHP/Config/PackageConfig.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace StaticPHP\Config;
66

77
use StaticPHP\Exception\WrongUsageException;
8+
use StaticPHP\Registry\Registry;
89
use StaticPHP\Runtime\SystemTarget;
910

1011
class PackageConfig
@@ -15,19 +16,19 @@ class PackageConfig
1516
* Load package configurations from a specified directory.
1617
* It will look for files matching the pattern 'pkg.*.json' and 'pkg.json'.
1718
*/
18-
public static function loadFromDir(string $dir): void
19+
public static function loadFromDir(string $dir, string $registry_name): void
1920
{
2021
if (!is_dir($dir)) {
2122
throw new WrongUsageException("Directory {$dir} does not exist, cannot load pkg.json config.");
2223
}
2324
$files = glob("{$dir}/pkg.*.json");
2425
if (is_array($files)) {
2526
foreach ($files as $file) {
26-
self::loadFromFile($file);
27+
self::loadFromFile($file, $registry_name);
2728
}
2829
}
2930
if (file_exists("{$dir}/pkg.json")) {
30-
self::loadFromFile("{$dir}/pkg.json");
31+
self::loadFromFile("{$dir}/pkg.json", $registry_name);
3132
}
3233
}
3334

@@ -36,7 +37,7 @@ public static function loadFromDir(string $dir): void
3637
*
3738
* @param string $file the path to the json package configuration file
3839
*/
39-
public static function loadFromFile(string $file): void
40+
public static function loadFromFile(string $file, string $registry_name): void
4041
{
4142
$content = file_get_contents($file);
4243
if ($content === false) {
@@ -49,6 +50,7 @@ public static function loadFromFile(string $file): void
4950
ConfigValidator::validateAndLintPackages(basename($file), $data);
5051
foreach ($data as $pkg_name => $config) {
5152
self::$package_configs[$pkg_name] = $config;
53+
Registry::_bindPackageConfigFile($pkg_name, $registry_name, $file);
5254
}
5355
}
5456

src/StaticPHP/ConsoleApplication.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use StaticPHP\Command\Dev\EnvCommand;
1010
use StaticPHP\Command\Dev\IsInstalledCommand;
1111
use StaticPHP\Command\Dev\ShellCommand;
12+
use StaticPHP\Command\Dev\SkeletonCommand;
1213
use StaticPHP\Command\DoctorCommand;
1314
use StaticPHP\Command\DownloadCommand;
1415
use StaticPHP\Command\ExtractCommand;
@@ -27,12 +28,12 @@ class ConsoleApplication extends Application
2728

2829
public function __construct()
2930
{
30-
parent::__construct('static-php-cli', self::VERSION);
31+
parent::__construct('StaticPHP', self::VERSION);
3132

3233
require_once ROOT_DIR . '/src/bootstrap.php';
3334

34-
// check registry
35-
Registry::checkLoadedRegistries();
35+
// resolve registry
36+
Registry::resolve();
3637

3738
/**
3839
* @var string $name
@@ -59,6 +60,7 @@ public function __construct()
5960
new ShellCommand(),
6061
new IsInstalledCommand(),
6162
new EnvCommand(),
63+
new SkeletonCommand(),
6264
]);
6365

6466
// add additional commands from registries

src/StaticPHP/Registry/Registry.php

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313

1414
class Registry
1515
{
16-
/** @var string[] List of loaded registry names */
16+
/** @var array<string, Registry> List of loaded registries */
1717
private static array $loaded_registries = [];
1818

19+
/** @var array<string, array{registry: string, config: string}> Maps of package and artifact names to their registry config file paths (for reverse lookup) */
20+
private static array $package_reversed_registry_files = [];
21+
private static array $artifact_reversed_registry_files = [];
22+
1923
/**
2024
* Load a registry from file path.
2125
* This method handles external registries that may not be in composer autoload.
@@ -85,9 +89,9 @@ public static function loadRegistry(string $registry_file, bool $auto_require =
8589
foreach ($data['package']['config'] as $path) {
8690
$path = self::fullpath($path, dirname($registry_file));
8791
if (is_file($path)) {
88-
PackageConfig::loadFromFile($path);
92+
PackageConfig::loadFromFile($path, $registry_name);
8993
} elseif (is_dir($path)) {
90-
PackageConfig::loadFromDir($path);
94+
PackageConfig::loadFromDir($path, $registry_name);
9195
}
9296
}
9397
}
@@ -97,9 +101,9 @@ public static function loadRegistry(string $registry_file, bool $auto_require =
97101
foreach ($data['artifact']['config'] as $path) {
98102
$path = self::fullpath($path, dirname($registry_file));
99103
if (is_file($path)) {
100-
ArtifactConfig::loadFromFile($path);
104+
ArtifactConfig::loadFromFile($path, $registry_name);
101105
} elseif (is_dir($path)) {
102-
ArtifactConfig::loadFromDir($path);
106+
ArtifactConfig::loadFromDir($path, $registry_name);
103107
}
104108
}
105109
}
@@ -187,7 +191,12 @@ public static function loadFromEnvOrOption(?string $registries = null): void
187191
}
188192
}
189193

190-
public static function checkLoadedRegistries(): void
194+
/**
195+
* Resolve loaded registries.
196+
* This method finalizes the loading process by registering default stages
197+
* and validating stage events.
198+
*/
199+
public static function resolve(): void
191200
{
192201
// Register default stages for all PhpExtensionPackage instances
193202
// This must be done after all registries are loaded to ensure custom stages take precedence
@@ -217,6 +226,42 @@ public static function reset(): void
217226
self::$loaded_registries = [];
218227
}
219228

229+
/**
230+
* Bind a package name to its registry config file for reverse lookup.
231+
*
232+
* @internal
233+
*/
234+
public static function _bindPackageConfigFile(string $package_name, string $registry_name, string $config_file): void
235+
{
236+
self::$package_reversed_registry_files[$package_name] = [
237+
'registry' => $registry_name,
238+
'config' => $config_file,
239+
];
240+
}
241+
242+
/**
243+
* Bind an artifact name to its registry config file for reverse lookup.
244+
*
245+
* @internal
246+
*/
247+
public static function _bindArtifactConfigFile(string $artifact_name, string $registry_name, string $config_file): void
248+
{
249+
self::$artifact_reversed_registry_files[$artifact_name] = [
250+
'registry' => $registry_name,
251+
'config' => $config_file,
252+
];
253+
}
254+
255+
public static function getPackageConfigInfo(string $package_name): ?array
256+
{
257+
return self::$package_reversed_registry_files[$package_name] ?? null;
258+
}
259+
260+
public static function getArtifactConfigInfo(string $artifact_name): ?array
261+
{
262+
return self::$artifact_reversed_registry_files[$artifact_name] ?? null;
263+
}
264+
220265
/**
221266
* Parse a class entry from the classes array.
222267
* Supports two formats:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace StaticPHP\Skeleton;
4+
5+
class ArtifactGenerator
6+
{
7+
protected ?array $source = null;
8+
9+
protected bool $generate_class = false;
10+
11+
protected bool $generate_custom_source_func = false;
12+
13+
protected bool $generate_custom_binary_func_for_unix = false;
14+
15+
protected bool $generate_custom_binary_func_for_windows = false;
16+
17+
public function __construct(protected string $name) {}
18+
19+
/**
20+
* Get the artifact name.
21+
*/
22+
public function getName(): string
23+
{
24+
return $this->name;
25+
}
26+
27+
public function setSource(array $source): static
28+
{
29+
$clone = clone $this;
30+
$clone->source = $source;
31+
return $clone;
32+
}
33+
34+
public function setCustomSource(): static
35+
{
36+
$clone = clone $this;
37+
$clone->source = ['type' => 'custom'];
38+
$clone->generate_class = true;
39+
$clone->generate_custom_source_func = true;
40+
return $clone;
41+
}
42+
43+
public function getSource(): ?array
44+
{
45+
return $this->source;
46+
}
47+
48+
public function generateConfig(): array
49+
{
50+
$config = [];
51+
52+
if ($this->source) {
53+
$config['source'] = $this->source;
54+
}
55+
return $config;
56+
}
57+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace StaticPHP\Skeleton;
4+
5+
use StaticPHP\Exception\ValidationException;
6+
use StaticPHP\Runtime\Executor\Executor;
7+
8+
class ExecutorGenerator
9+
{
10+
public function __construct(protected string $class)
11+
{
12+
if (!is_a($class, Executor::class, true)) {
13+
throw new ValidationException('Executor class must extend ' . Executor::class);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)