Skip to content

Commit

Permalink
feat(asFileName): the function will now return an instance of CatPaw\…
Browse files Browse the repository at this point in the history
…Cpre\FileName

This new class allows you to manage file names located within the file system or within the .phar archive (if one exists).
  • Loading branch information
razshare committed Mar 16, 2024
1 parent a5f9c70 commit 7296b7e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 38 deletions.
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@
"dev:fix": "php -dxdebug.mode=off ./vendor/bin/php-cs-fixer fix .",
"dev:precommit": "pre-commit install && pre-commit autoupdate",
"dev:tips": "[ ! -f catpaw.phar ] && echo '' || php catpaw.phar --tips",
"download:psalm": "[ ! -f psalm.phar ] && wget https://github.com/vimeo/psalm/releases/latest/download/psalm.phar || echo ''",
"post-autoload-dump": [
"@download:psalm",
"@dev:tips"
],
"post-create-project-cmd": [
"@download:psalm",
"@dev:tips"
]
},
Expand Down
8 changes: 4 additions & 4 deletions dev-gui-example.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

pushd src/lib/Gui/lib &&\
go build -o main.so -buildmode=c-shared main.go &&\
cpp -P ./main.h ./main.static.h &&\
popd || exit
# pushd src/lib/Gui/lib &&\
# go build -o main.so -buildmode=c-shared main.go &&\
# cpp -P ./main.h ./main.static.h &&\
# popd || exit

php -dxdebug.mode=debug -dxdebug.start_with_request=yes -dphar.readonly=0 ./bin/start --libraries='./src/lib' --entry='./src/gui-example.php'
17 changes: 0 additions & 17 deletions psalm.xml

This file was deleted.

1 change: 1 addition & 0 deletions src/gui-example.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
use function CatPaw\Core\error;

use function CatPaw\Core\goffi;
use CatPaw\Gui\Contract;

Expand Down
70 changes: 70 additions & 0 deletions src/lib/Core/FileName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace CatPaw\Core;

use Phar;
use Stringable;

class FileName implements Stringable {
/**
* Create a file name starting from a `$base` directory.
* @param array<string> $base
* @return FileName
*/
public static function create(array $path):self {
return new self($path);
}

private bool $checkPhar = false;

/**
* @param array<string> $path
*/
private function __construct(private array $path) {
}

/**
* Given a `$path`, create a file name.
* @param array<string> $path
* @return string
*/
private static function asFileName(array $path):string {
$parts = [];
$count = count($path);
for ($index = 0; $index < $count; $index++) {
$pathName = $path[$index];
if ($index < $count - 1 && !str_ends_with($pathName, '/')) {
$pathName = "$pathName/";
}
$parts[] = $pathName;
}
return join($parts)?:'';
}

/**
* Lookup the file in in the `.phar` before falling back to the file system.
* @return self
*/
public function withPhar(): self {
$this->checkPhar = true;
return $this;
}

public function __toString(): string {
if (isPhar()) {
$phar = Phar::running();
$localizedFileName = str_replace("$phar/", '', self::asFileName($this->path));

if ($this->checkPhar) {
$pharFileName = "$phar/$localizedFileName";
if (!file_exists($pharFileName)) {
return realpath($localizedFileName);
}
return $pharFileName;
}
return realpath($localizedFileName);
} else {
return realpath(self::asFileName($this->path));
}
}
}
20 changes: 6 additions & 14 deletions src/scripts/Core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,11 @@ function stop(string|Error $error) {

/**
* Given a `$path`, create a file name.
* @param string ...$path
* @return string
* @param string ...$path
* @return FileName
*/
function asFileName(string ...$path):string {
$parts = [];
$count = count($path);
for ($index = 0; $index < $count; $index++) {
$pathName = $path[$index];
if ($index < $count - 1 && !str_ends_with($pathName, '/')) {
$pathName = "$pathName/";
}
$parts[] = $pathName;
}
return realpath(join($parts))?:'';
function asFileName(string ...$path):FileName {
return FileName::create($path);
}

/**
Expand All @@ -372,7 +363,8 @@ function asFileName(string ...$path):string {
*/
function asPharFileName(string ...$path):string {
if (isPhar()) {
$phar = Phar::running();
$phar = Phar::running();

$path = [$phar, ...$path];
$parts = [];
$count = count($path);
Expand Down

0 comments on commit 7296b7e

Please sign in to comment.