php-cs fix files

This commit is contained in:
Tim
2025-06-25 11:29:20 +02:00
parent 737060f0a5
commit 77c9a0ab62
5 changed files with 381 additions and 377 deletions

View File

@ -1,85 +1,87 @@
<?php <?php
class argParsed class argParsed
{ {
public function __construct( public function __construct(
private array $parsed = [], private array $parsed = [],
) {} ) {
}
public function initOptions(array $options, mixed $default = false): self
{ public function initOptions(array $options, mixed $default = false): self
foreach (array_keys($options) as $name) { {
$this->set($name, $default); foreach (array_keys($options) as $name) {
} $this->set($name, $default);
}
return $this;
} return $this;
}
public function get(string $key, mixed $default = null): mixed
{ public function get(string $key, mixed $default = null): mixed
return $this->parsed[$key] ?? $default; {
} return $this->parsed[$key] ?? $default;
}
public function set(string $key, mixed $value): self
{ public function set(string $key, mixed $value): self
$this->parsed[$key] = $value; {
return $this; $this->parsed[$key] = $value;
} return $this;
}
public function getRest(): string
{ public function getRest(): string
return $this->get('rest', ''); {
} return $this->get('rest', '');
} }
}
readonly class argvParser
{ readonly class argvParser
/** {
* @param array<string, string> $options /**
* @param array<string, string> $arguments * @param array<string, string> $options
*/ * @param array<string, string> $arguments
public function __construct( */
private array $options = [], public function __construct(
private array $arguments = [], private array $options = [],
) {} private array $arguments = [],
) {
public function getOptionsHelp(): string }
{
$line = ''; public function getOptionsHelp(): string
foreach ($this->options as $option => $description) { {
$line .= sprintf('[-%s (%s)] ', $option, $description); $line = '';
} foreach ($this->options as $option => $description) {
foreach ($this->arguments as $argument => $description) { $line .= sprintf('[-%s (%s)] ', $option, $description);
$line .= sprintf('<%s (%s)> ', $argument, $description); }
} foreach ($this->arguments as $argument => $description) {
return $line; $line .= sprintf('<%s (%s)> ', $argument, $description);
} }
return $line;
public function parseArgv(array $argv): argParsed|false }
{
array_shift($argv); // shift the script name public function parseArgv(array $argv): argParsed|false
{
$parsed = (new argParsed())->initOptions($this->options); array_shift($argv); // shift the script name
while (str_starts_with($argv[0] ?? '', '-')) {
$option = substr($argv[0], 1, 1); $parsed = (new argParsed())->initOptions($this->options);
if (substr($argv[0], 2, 1) === ':') { while (str_starts_with($argv[0] ?? '', '-')) {
$data = substr($argv[0], 3); $option = substr($argv[0], 1, 1);
} else { if (substr($argv[0], 2, 1) === ':') {
$data = true; $data = substr($argv[0], 3);
} } else {
if ($this->options[$option]) { $data = true;
$parsed->set($option, $data); }
} if ($this->options[$option]) {
array_shift($argv); $parsed->set($option, $data);
} }
if (count($argv) < count($this->arguments)) { array_shift($argv);
return false; }
} if (count($argv) < count($this->arguments)) {
foreach ($this->arguments as $arg => $description) { return false;
$parsed->set($arg, array_shift($argv)); }
} foreach ($this->arguments as $arg => $description) {
$parsed->set('rest', implode(' ', $argv)); $parsed->set($arg, array_shift($argv));
}
return $parsed; $parsed->set('rest', implode(' ', $argv));
}
} return $parsed;
}
}

View File

@ -1,76 +1,76 @@
<?php <?php
require_once __DIR__ . '/functions.php'; require_once __DIR__ . '/functions.php';
readonly class homeConfig extends config readonly class homeConfig extends config
{ {
private const CONFIG_BASE_PATH = '.config'; private const CONFIG_BASE_PATH = '.config';
public function __construct( public function __construct(
string $name string $name
) ) {
{ parent::__construct(path(
parent::__construct(path( getenv('HOME'),
getenv('HOME'), self::CONFIG_BASE_PATH,
self::CONFIG_BASE_PATH, $name
$name ));
)); $this->createConfig();
$this->createConfig(); }
}
private function createConfig(): void
private function createConfig(): void {
{ if (file_exists($this->getConfigFile())) {
if (file_exists($this->getConfigFile())) { return;
return; } else {
} else { if (!is_dir($this->getConfigPath())) {
if (!is_dir($this->getConfigPath())) { mkdir($this->getConfigPath(), 0755, true);
mkdir($this->getConfigPath(), 0755, true); }
} $this->writeConfig([]);
$this->writeConfig([]); }
} }
} }
}
readonly class config
readonly class config {
{ public function __construct(
public function __construct( private string $path,
private string $path, private string $fileName = 'config.php',
private string $fileName = 'config.php', ) {
) {} }
public function configExists(): bool public function configExists(): bool
{ {
return file_exists($this->getConfigFile()); return file_exists($this->getConfigFile());
} }
public function get(string $key, mixed $default = null): mixed public function get(string $key, mixed $default = null): mixed
{ {
if (!$this->configExists()) { if (!$this->configExists()) {
return $default; return $default;
} }
$config = include $this->getConfigFile(); $config = include $this->getConfigFile();
return $config[$key] ?? $default; return $config[$key] ?? $default;
} }
public function set(string $key, mixed $value): void public function set(string $key, mixed $value): void
{ {
$config = include $this->getConfigFile(); $config = include $this->getConfigFile();
$config[$key] = $value; $config[$key] = $value;
$this->writeConfig($config); $this->writeConfig($config);
} }
protected function getConfigFile(): string protected function getConfigFile(): string
{ {
return path($this->getConfigPath(), $this->fileName); return path($this->getConfigPath(), $this->fileName);
} }
protected function getConfigPath(): string protected function getConfigPath(): string
{ {
return $this->path; return $this->path;
} }
protected function writeConfig(array $config): void protected function writeConfig(array $config): void
{ {
file_put_contents($this->getConfigFile(), '<?php return ' . var_export($config, true) . ';'); file_put_contents($this->getConfigFile(), '<?php return ' . var_export($config, true) . ';');
} }
} }

View File

@ -1,24 +1,24 @@
<?php <?php
use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\NoReturn;
function path(...$segments): string function path(...$segments): string
{ {
return implode(DIRECTORY_SEPARATOR, $segments); return implode(DIRECTORY_SEPARATOR, $segments);
} }
function line(string $line): void function line(string $line): void
{ {
echo $line . PHP_EOL; echo $line . PHP_EOL;
} }
#[NoReturn] function lexit(string $line, int $code = 0): void #[NoReturn] function lexit(string $line, int $code = 0): void
{ {
line($line); line($line);
exit($code); exit($code);
} }
function execInDirectory(string $dir, string $command): ?string function execInDirectory(string $dir, string $command): ?string
{ {
return shell_exec(sprintf('cd %s && %s', $dir, $command)); return shell_exec(sprintf('cd %s && %s', $dir, $command));
} }

View File

@ -1,66 +1,67 @@
<?php <?php
readonly class snips readonly class snips
{ {
public function __construct( public function __construct(
private string $baseUrl, private string $baseUrl,
private string $apiKey, private string $apiKey,
) {} ) {
}
public function getSnip(int $id): string
{ public function getSnip(int $id): string
$url = $this->baseUrl . 'snip/' . $id; {
$headers = [ $url = $this->baseUrl . 'snip/' . $id;
'X-AUTH-TOKEN: ' . $this->apiKey, $headers = [
'Accept: application/json', 'X-AUTH-TOKEN: ' . $this->apiKey,
]; 'Accept: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_close($ch); $response = curl_exec($ch);
curl_close($ch);
$response = $this->getResponse($response);
$response = $this->getResponse($response);
if (isset($response['success']) && $response['success'] === true) {
return $response['data']['content']; if (isset($response['success']) && $response['success'] === true) {
} else { return $response['data']['content'];
throw new Exception('Error fetching snip: ' . json_encode($response)); } else {
} throw new Exception('Error fetching snip: ' . json_encode($response));
// rewrite to curl }
} // rewrite to curl
}
public function postSnip(int $id, string $content): string
{ public function postSnip(int $id, string $content): string
$url = $this->baseUrl . 'snip/' . $id; {
$headers = [ $url = $this->baseUrl . 'snip/' . $id;
'X-AUTH-TOKEN: ' . $this->apiKey, $headers = [
'Accept: application/json', 'X-AUTH-TOKEN: ' . $this->apiKey,
]; 'Accept: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['content' => $content]); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$response = curl_exec($ch); curl_setopt($ch, CURLOPT_POSTFIELDS, ['content' => $content]);
curl_close($ch); $response = curl_exec($ch);
$response = $this->getResponse($response); curl_close($ch);
$response = $this->getResponse($response);
if (isset($response['success']) && $response['success'] === true) {
return $response['data']['content']; if (isset($response['success']) && $response['success'] === true) {
} else { return $response['data']['content'];
throw new Exception('Error updating snip: ' . json_encode($response)); } else {
} throw new Exception('Error updating snip: ' . json_encode($response));
} }
}
private function getResponse(bool|string $response): array
{ private function getResponse(bool|string $response): array
$response = json_decode($response, true); {
if (json_last_error() !== JSON_ERROR_NONE) { $response = json_decode($response, true);
throw new Exception('Error decoding JSON response: ' . json_last_error_msg()); if (json_last_error() !== JSON_ERROR_NONE) {
} throw new Exception('Error decoding JSON response: ' . json_last_error_msg());
return $response; }
} return $response;
} }
}

View File

@ -1,126 +1,127 @@
<?php <?php
readonly class stow readonly class stow
{ {
public const ACTION_STOW = 'stow'; public const ACTION_STOW = 'stow';
public const ACTION_UNSTOW = 'unstow'; public const ACTION_UNSTOW = 'unstow';
public const STOW_IGNORE_FILE = '.stowignore'; public const STOW_IGNORE_FILE = '.stowignore';
public const CONFIG_FILE_NAME = 'config.stow.php'; public const CONFIG_FILE_NAME = 'config.stow.php';
public function __construct( public function __construct(
private string $action = self::ACTION_STOW, private string $action = self::ACTION_STOW,
private bool $backup = false, private bool $backup = false,
private bool $force = false, private bool $force = false,
private bool $unwrap = false, private bool $unwrap = false,
) {} ) {
}
private function getNoStow(string $path): array
{ private function getNoStow(string $path): array
if (file_exists(path($path, self::STOW_IGNORE_FILE))) { {
$noStow = file_get_contents(path($path, self::STOW_IGNORE_FILE)); if (file_exists(path($path, self::STOW_IGNORE_FILE))) {
if ($noStow === false) { $noStow = file_get_contents(path($path, self::STOW_IGNORE_FILE));
return []; if ($noStow === false) {
} return [];
return array_values(array_filter(array_map(trim(...), explode(PHP_EOL, $noStow)))); }
} else { return array_values(array_filter(array_map(trim(...), explode(PHP_EOL, $noStow))));
return []; } else {
} return [];
} }
}
function run(string $sourcePath, string $targetPath): void
{ public function run(string $sourcePath, string $targetPath): void
$noStow = $this->getNoStow($sourcePath); {
$sourceFiles = array_diff(scandir($sourcePath), ['..', '.', self::CONFIG_FILE_NAME, self::STOW_IGNORE_FILE, ...$noStow]); $noStow = $this->getNoStow($sourcePath);
$sourceFiles = array_diff(scandir($sourcePath), ['..', '.', self::CONFIG_FILE_NAME, self::STOW_IGNORE_FILE, ...$noStow]);
foreach ($sourceFiles as $sourceFile) {
$targetFile = path($targetPath, $sourceFile); foreach ($sourceFiles as $sourceFile) {
$stowFile = path($sourcePath, $sourceFile); $targetFile = path($targetPath, $sourceFile);
$stowFile = path($sourcePath, $sourceFile);
switch ($this->action) {
case self::ACTION_STOW: switch ($this->action) {
$this->stow($stowFile, $targetFile); case self::ACTION_STOW:
break; $this->stow($stowFile, $targetFile);
case self::ACTION_UNSTOW: break;
$this->unstow($stowFile, $targetFile); case self::ACTION_UNSTOW:
break; $this->unstow($stowFile, $targetFile);
default: break;
line("Unknown action: {$this->action}"); default:
return; line("Unknown action: {$this->action}");
} return;
} }
} }
}
public function stow(string $stowFile, string $targetFile): void
{ public function stow(string $stowFile, string $targetFile): void
if (is_link($targetFile)) { {
$targetLink = readlink($targetFile); if (is_link($targetFile)) {
if ($targetLink === $stowFile) { $targetLink = readlink($targetFile);
line("File $targetFile is already stowed from $stowFile"); if ($targetLink === $stowFile) {
} else { line("File $targetFile is already stowed from $stowFile");
if (is_dir($targetFile)) { } else {
if ($this->unwrap) { if (is_dir($targetFile)) {
line("Unwrapping $targetFile from $targetLink"); if ($this->unwrap) {
unlink($targetFile); line("Unwrapping $targetFile from $targetLink");
mkdir($targetFile); unlink($targetFile);
$this->run($targetLink, $targetFile); mkdir($targetFile);
$this->run($stowFile, $targetFile); $this->run($targetLink, $targetFile);
} else { $this->run($stowFile, $targetFile);
line("File $targetFile is linked from $targetLink, ignoring, add -w to unwrap"); } else {
} line("File $targetFile is linked from $targetLink, ignoring, add -w to unwrap");
}else{ }
line("File $targetFile is already linked from $targetLink"); } else {
} line("File $targetFile is already linked from $targetLink");
} }
return; }
} return;
if (is_dir($targetFile)) { }
if (is_dir($stowFile)) { if (is_dir($targetFile)) {
line("File $targetFile and $stowFile are folders, recurse into existing folders"); if (is_dir($stowFile)) {
$this->run($stowFile, $targetFile); line("File $targetFile and $stowFile are folders, recurse into existing folders");
} else { $this->run($stowFile, $targetFile);
line("File $targetFile is a folder, but stow file $stowFile is not a folder"); } else {
} line("File $targetFile is a folder, but stow file $stowFile is not a folder");
return; }
} return;
if (is_file($targetFile)) { }
if ($this->backup) { if (is_file($targetFile)) {
$backFile = $targetFile . '.bak'; if ($this->backup) {
if (is_file($backFile)) { $backFile = $targetFile . '.bak';
line("Backup file $backFile already exists"); if (is_file($backFile)) {
return; line("Backup file $backFile already exists");
} return;
rename($targetFile, $backFile); }
line("Backup file $targetFile to $backFile"); rename($targetFile, $backFile);
} elseif ($this->force) { line("Backup file $targetFile to $backFile");
line("Todo: delete file $targetFile"); } elseif ($this->force) {
return; line("Todo: delete file $targetFile");
} else { return;
line("File $targetFile already exists"); } else {
return; line("File $targetFile already exists");
} return;
} }
line("Stow $targetFile from $stowFile"); }
symlink($stowFile, $targetFile); line("Stow $targetFile from $stowFile");
} symlink($stowFile, $targetFile);
}
public function unstow(string $stowFile, string $targetFile): void
{ public function unstow(string $stowFile, string $targetFile): void
if (is_link($targetFile) && readlink($targetFile) === $stowFile) { {
line("Unstow $targetFile from $stowFile"); if (is_link($targetFile) && readlink($targetFile) === $stowFile) {
unlink($targetFile); line("Unstow $targetFile from $stowFile");
return; unlink($targetFile);
} return;
if (is_dir($targetFile)) { }
if (is_dir($stowFile)) { if (is_dir($targetFile)) {
line("File $targetFile and $stowFile are folders, recurse into existing folders"); if (is_dir($stowFile)) {
$this->run($stowFile, $targetFile); line("File $targetFile and $stowFile are folders, recurse into existing folders");
return; $this->run($stowFile, $targetFile);
} else { return;
line("File $targetFile is a folder, but stow file $stowFile is not a folder"); } else {
} line("File $targetFile is a folder, but stow file $stowFile is not a folder");
} }
line("File $targetFile is not stowed from $stowFile"); }
} line("File $targetFile is not stowed from $stowFile");
} }
}