複数ネームスペースを可能に

This commit is contained in:
2026-02-27 03:15:17 +09:00
parent b723230818
commit a1bd29cff6
32 changed files with 95 additions and 73 deletions

View File

@@ -1,9 +1,11 @@
<?php <?php
spl_autoload_register(function ($class): void { spl_autoload_register(function ($class): void {
$prefix = 'Site\\'; $prefixes = ['Site\\', 'Std\\'];
$base = realpath(__DIR__.'/src'); $base = realpath(__DIR__.'/src');
$prefixExist = false;
if (strncmp($prefix, $class, strlen($prefix)) !== 0) return; foreach ($prefixes as $prefix) { if (strncmp($prefix, $class, strlen($prefix)) === 0) $prefixExist = true; }
if (!$prefixExist) return;
$ps = str_replace('\\', DIRECTORY_SEPARATOR, $class); $ps = str_replace('\\', DIRECTORY_SEPARATOR, $class);
$file = $base.DIRECTORY_SEPARATOR.$ps.'.php'; $file = $base.DIRECTORY_SEPARATOR.$ps.'.php';
if (file_exists($file)) require $file; if (file_exists($file)) require $file;

View File

@@ -1,6 +1,6 @@
# ルートの仕組み # ルートの仕組み
`route.php` では、 `use Site\Lib\Route; `というインクルードもある事に `route.php` では、 `use Std\Lib\Route; `というインクルードもある事に
気づくでしょう。 気づくでしょう。
これが全てのルートを処理します。 これが全てのルートを処理します。
ルートの基本的な流れは次の通りです: ルートの基本的な流れは次の通りです:

View File

@@ -2,7 +2,7 @@
require_once __DIR__.DIRECTORY_SEPARATOR.'/autoload.php'; require_once __DIR__.DIRECTORY_SEPARATOR.'/autoload.php';
use Site\Lib\Route; use Std\Lib\Route;
use Site\Controller\Atom; use Site\Controller\Atom;
use Site\Controller\Fediverse; use Site\Controller\Fediverse;
@@ -20,7 +20,6 @@ if (DEBUG_MODE) {
} }
include(ROOT.'/util.php'); include(ROOT.'/util.php');
$routes = [ $routes = [
Route::add('GET', 'blog/{page}', Home::class.'@article'), Route::add('GET', 'blog/{page}', Home::class.'@article'),
Route::add('GET', 'about', Page::class.'@about'), Route::add('GET', 'about', Page::class.'@about'),

View File

@@ -2,7 +2,7 @@
namespace Site\Controller; namespace Site\Controller;
use Site\Controller\BlogPost; use Site\Controller\BlogPost;
use Site\Lib\Markdown; use Std\Lib\Markdown;
class Atom extends BlogPost { class Atom extends BlogPost {
private string $domain = 'technicalsuwako.moe'; private string $domain = 'technicalsuwako.moe';

View File

@@ -3,7 +3,7 @@ namespace Site\Controller;
use Site\Controller\BlogPost; use Site\Controller\BlogPost;
use Site\Controller\Mods; use Site\Controller\Mods;
use Site\Lib\Activitypub; use Std\Lib\Activitypub;
class Fediverse extends BlogPost { class Fediverse extends BlogPost {
use Mods; use Mods;

View File

@@ -3,10 +3,10 @@ namespace Site\Controller;
use Site\Controller\BlogPost; use Site\Controller\BlogPost;
use Site\Controller\Mods; use Site\Controller\Mods;
use Site\Lib\Activitypub; use Std\Lib\Activitypub;
use Site\Lib\Auth; use Std\Lib\Auth;
use Site\Lib\Markdown; use Std\Lib\Markdown;
use Site\Lib\Template; use Std\Lib\Template;
class Home extends BlogPost { class Home extends BlogPost {
use Mods; use Mods;

View File

@@ -2,8 +2,8 @@
namespace Site\Controller; namespace Site\Controller;
use Site\Controller\Mods; use Site\Controller\Mods;
use Site\Lib\Auth; use Std\Lib\Auth;
use Site\Lib\Template; use Std\Lib\Template;
class Notfound { class Notfound {
use Mods; use Mods;

View File

@@ -2,8 +2,8 @@
namespace Site\Controller; namespace Site\Controller;
use Site\Controller\Mods; use Site\Controller\Mods;
use Site\Lib\Auth; use Std\Lib\Auth;
use Site\Lib\Template; use Std\Lib\Template;
class Page { class Page {
use Mods; use Mods;

View File

@@ -2,8 +2,8 @@
namespace Site\Controller; namespace Site\Controller;
use Site\Controller\Mods; use Site\Controller\Mods;
use Site\Lib\Auth; use Std\Lib\Auth;
use Site\Lib\Template; use Std\Lib\Template;
class User { class User {
use Mods; use Mods;

View File

@@ -0,0 +1,19 @@
<?php
namespace Site\Test;
require_once __DIR__.'/../../../autoload.php';
use Site\Controller\User;
use Std\Lib\Tester;
$test = new Tester([
'colorOutput' => true,
'verboseOutput' => true
]);
$test->describe('', function($test): void {
$test->it('', function($test): void {
// TODO
});
});

View File

@@ -1,7 +1,7 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
use Site\Lib\Curl; use Std\Lib\Curl;
/** /**
* ActivityPubプロトコルの実装クラス * ActivityPubプロトコルの実装クラス

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
class Auth { class Auth {
private int $id; private int $id;

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
/** /**
* php_curlへの依存を排除するための独自のCURL実装 * php_curlへの依存を排除するための独自のCURL実装

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
class DiffViewer { class DiffViewer {
private $diffContent; private $diffContent;

View File

@@ -1,11 +1,11 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
use Site\Lib\Image\ImageInterface; use Std\Lib\Image\ImageInterface;
use Site\Lib\Image\Gif; use Std\Lib\Image\Gif;
use Site\Lib\Image\Jpeg; use Std\Lib\Image\Jpeg;
use Site\Lib\Image\Png; use Std\Lib\Image\Png;
use Site\Lib\Image\Targa; use Std\Lib\Image\Targa;
class RGB { class RGB {
public int $r; public int $r;

View File

@@ -1,7 +1,7 @@
<?php <?php
namespace Site\Lib\Image; namespace Std\Lib\Image;
use Site\Lib\Image\ImageInterface; use Std\Lib\Image\ImageInterface;
class Gif implements ImageInterface { class Gif implements ImageInterface {
public int $width = 0; public int $width = 0;

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib\Image; namespace Std\Lib\Image;
interface ImageInterface { interface ImageInterface {
public function parse(string $file): \stdClass; public function parse(string $file): \stdClass;

View File

@@ -1,7 +1,7 @@
<?php <?php
namespace Site\Lib\Image; namespace Std\Lib\Image;
use Site\Lib\Image\ImageInterface; use Std\Lib\Image\ImageInterface;
class Jpeg implements ImageInterface { class Jpeg implements ImageInterface {
public int $width = 0; public int $width = 0;

View File

@@ -1,7 +1,7 @@
<?php <?php
namespace Site\Lib\Image; namespace Std\Lib\Image;
use Site\Lib\Image\ImageInterface; use Std\Lib\Image\ImageInterface;
class Png implements ImageInterface { class Png implements ImageInterface {
public \stdClass $IHDR; // 画像ヘッダー public \stdClass $IHDR; // 画像ヘッダー
@@ -151,7 +151,7 @@ next_chunk:
if ($nextChunk === "\x50\x4c\x54\x45") { // PLTE if ($nextChunk === "\x50\x4c\x54\x45") { // PLTE
$palette = []; $palette = [];
for ($i = 0; $i < $length; $i += 3) { for ($i = 0; $i < $length; $i += 3) {
$colors = new \Site\Lib\RGB(ord($data[$i]), ord($data[$i+1]), ord($data[$i+2])); $colors = new \Std\Lib\RGB(ord($data[$i]), ord($data[$i+1]), ord($data[$i+2]));
$palette[] = $colors->rgb; $palette[] = $colors->rgb;
} }
@@ -173,7 +173,7 @@ next_chunk:
$bytes = $this->IHDR->colorType === 2 ? 3 : 1; $bytes = $this->IHDR->colorType === 2 ? 3 : 1;
for ($i = 0; $i < $length; $i += $bytes) { for ($i = 0; $i < $length; $i += $bytes) {
if ($this->IHDR->colorType === 2) { if ($this->IHDR->colorType === 2) {
$colors = new \Site\Lib\RGB(ord($data[$i]), ord($data[$i+1]), ord($data[$i+2])); $colors = new \Std\Lib\RGB(ord($data[$i]), ord($data[$i+1]), ord($data[$i+2]));
$vals[] = $colors->rgb; $vals[] = $colors->rgb;
} else { } else {
$vals[] = ord($data[$i]); $vals[] = ord($data[$i]);

View File

@@ -1,7 +1,7 @@
<?php <?php
namespace Site\Lib\Image; namespace Std\Lib\Image;
use Site\Lib\Image\ImageInterface; use Std\Lib\Image\ImageInterface;
class Targa implements ImageInterface { class Targa implements ImageInterface {
public int $id = 0; public int $id = 0;

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
class Mailer { class Mailer {
private $socket; private $socket;

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
class Markdown { class Markdown {
private string $content; private string $content;

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
class Route { class Route {
protected static array $routes = []; protected static array $routes = [];

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
class Template { class Template {
private string $tmplExt = '.maron'; private string $tmplExt = '.maron';

View File

@@ -1,5 +1,5 @@
<?php <?php
namespace Site\Lib; namespace Std\Lib;
/** /**
* アサーション失敗用のカスタム例外 * アサーション失敗用のカスタム例外

View File

@@ -1,10 +1,10 @@
<?php <?php
namespace Site\Test; namespace Std\Test;
require_once __DIR__.'/../../../autoload.php'; require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester; use Std\Lib\Tester;
use Site\Lib\Activitypub; use Std\Lib\Activitypub;
use Site\Controller\BlogPost; use Site\Controller\BlogPost;
if (ACTIVITYPUB_ENABLED) { if (ACTIVITYPUB_ENABLED) {

View File

@@ -1,10 +1,10 @@
<?php <?php
namespace Site\Test; namespace Std\Test;
require_once __DIR__.'/../../../autoload.php'; require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester; use Std\Lib\Tester;
use Site\Lib\Auth; use Std\Lib\Auth;
if (AUTH_ENABLED) { if (AUTH_ENABLED) {
$test = new Tester([ $test = new Tester([
@@ -156,7 +156,7 @@ if (AUTH_ENABLED) {
}); });
} }
// TODO: CLIからクッキーを設定出来ないから、「Site\Lib\Auth::getUserData(): Return value must be of type stdClass, null returned」って発生する・・・ // TODO: CLIからクッキーを設定出来ないから、「Lib\Auth::getUserData(): Return value must be of type stdClass, null returned」って発生する・・・
// $test->describe('ログインとログアウト', function($test): void { // $test->describe('ログインとログアウト', function($test): void {
// $user = "admin1234"; // $user = "admin1234";
// $pass = "testTEST1234!#$%@[]:;"; // $pass = "testTEST1234!#$%@[]:;";

View File

@@ -1,10 +1,10 @@
<?php <?php
namespace Site\Test; namespace Std\Test;
require_once __DIR__.'/../../../autoload.php'; require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester; use Std\Lib\Tester;
use Site\Lib\Curl; use Std\Lib\Curl;
if (CURL_ENABLED) { if (CURL_ENABLED) {
$test = new Tester([ $test = new Tester([

View File

@@ -1,10 +1,10 @@
<?php <?php
namespace Site\Test; namespace Std\Test;
require_once __DIR__.'/../../../autoload.php'; require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester; use Std\Lib\Tester;
use Site\Lib\Markdown; use Std\Lib\Markdown;
$test = new Tester([ $test = new Tester([
'colorOutput' => true, 'colorOutput' => true,
@@ -80,7 +80,7 @@ $test->describe('マークダウン', function($test): void {
$body = $md->parse(); $body = $md->parse();
$test->assertNotNull($body); $test->assertNotNull($body);
$expect = "<h1>ケロケロ</h1>"; $expect = "<h1 id=\"section-1\">ケロケロ</h1>";
$actual = trim($body); $actual = trim($body);
$test->assertEquals($expect, $actual); $test->assertEquals($expect, $actual);
@@ -95,7 +95,7 @@ $test->describe('マークダウン', function($test): void {
$body = $md->parse(); $body = $md->parse();
$test->assertNotNull($body); $test->assertNotNull($body);
$expect = "<h2>ケロケロ</h2>"; $expect = "<h2 id=\"section-1\">ケロケロ</h2>";
$actual = trim($body); $actual = trim($body);
$test->assertEquals($expect, $actual); $test->assertEquals($expect, $actual);
@@ -110,7 +110,7 @@ $test->describe('マークダウン', function($test): void {
$body = $md->parse(); $body = $md->parse();
$test->assertNotNull($body); $test->assertNotNull($body);
$expect = "<h3>ケロケロ</h3>"; $expect = "<h3 id=\"section-1\">ケロケロ</h3>";
$actual = trim($body); $actual = trim($body);
$test->assertEquals($expect, $actual); $test->assertEquals($expect, $actual);
@@ -125,7 +125,7 @@ $test->describe('マークダウン', function($test): void {
$body = $md->parse(); $body = $md->parse();
$test->assertNotNull($body); $test->assertNotNull($body);
$expect = "<h4>ケロケロ</h4>"; $expect = "<h4 id=\"section-1\">ケロケロ</h4>";
$actual = trim($body); $actual = trim($body);
$test->assertEquals($expect, $actual); $test->assertEquals($expect, $actual);
@@ -140,7 +140,7 @@ $test->describe('マークダウン', function($test): void {
$body = $md->parse(); $body = $md->parse();
$test->assertNotNull($body); $test->assertNotNull($body);
$expect = "<h5>ケロケロ</h5>"; $expect = "<h5 id=\"section-1\">ケロケロ</h5>";
$actual = trim($body); $actual = trim($body);
$test->assertEquals($expect, $actual); $test->assertEquals($expect, $actual);
@@ -155,7 +155,7 @@ $test->describe('マークダウン', function($test): void {
$body = $md->parse(); $body = $md->parse();
$test->assertNotNull($body); $test->assertNotNull($body);
$expect = "<h6>ケロケロ</h6>"; $expect = "<h6 id=\"section-1\">ケロケロ</h6>";
$actual = trim($body); $actual = trim($body);
$test->assertEquals($expect, $actual); $test->assertEquals($expect, $actual);

View File

@@ -1,10 +1,10 @@
<?php <?php
namespace Site\Test; namespace Std\Test;
require_once __DIR__.'/../../../autoload.php'; require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester; use Std\Lib\Tester;
use Site\Lib\Route; use Std\Lib\Route;
$test = new Tester([ $test = new Tester([
'colorOutput' => true, 'colorOutput' => true,
@@ -25,7 +25,7 @@ $test->describe('ルート', function($test): void {
[ [
'method' => 'GET', 'method' => 'GET',
'path' => 'about', 'path' => 'about',
'class' => 'Site\Test\Webpage@about', 'class' => 'Std\Test\Webpage@about',
'params' => [], 'params' => [],
] ]
]; ];

View File

@@ -8,15 +8,17 @@ if (!CURL_ENABLED) define('ACTIVITYPUB_ENABLED', false);
mb_internal_encoding('UTF-8'); mb_internal_encoding('UTF-8');
header('Content-Type: text/plain; charset=UTF-8'); header('Content-Type: text/plain; charset=UTF-8');
$testDir = ROOT.'/src/Site/Test'; $testDirs = [ROOT.'/src/Site/Test', ROOT.'/src/Std/Test'];
$testFiles = glob($testDir.'/*.php'); $testFiles = [];
foreach ($testDirs as $d) $testFiles[] = glob($d.'/*.php');
$testFiles = array_merge(...$testFiles);
if (empty($testFiles)) { if (empty($testFiles)) {
echo "テストファイルは{$testDir}ありません\n"; echo "テストファイルはありません\n";
exit(1); exit(1);
} }
echo "{$testDir}にある".count($testFiles)."個テストファイルを実行中:\n"; echo count($testFiles)."個テストファイルを実行中:\n";
echo "------------------------------------------------\n"; echo "------------------------------------------------\n";
$totalFiles = 0; $totalFiles = 0;

View File

@@ -384,8 +384,8 @@ function countmatch(string $str): bool {
return $len == $sum; return $len == $sum;
} }
function getImageInfo(string $url): \Site\Lib\Image { function getImageInfo(string $url): \Std\Lib\Image {
$img = new \Site\Lib\Image($url); $img = new \Std\Lib\Image($url);
return $img; return $img;
} }