Skip to content

Commit

Permalink
Merge pull request #19 from citrus-framework/fix_command
Browse files Browse the repository at this point in the history
Command用ゲートウェイ
  • Loading branch information
take64 authored Nov 3, 2020
2 parents a244ecf + 2da22e8 commit 1dc4ef2
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 100 deletions.
4 changes: 1 addition & 3 deletions src/Authentication/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Citrus\CitrusException;
use Citrus\Collection;
use Citrus\Database\Connection\Connection;
use Citrus\Database\DatabaseException;
use Citrus\Intersection;
use Citrus\Logger;
use Citrus\Query\Builder;
Expand All @@ -23,6 +22,7 @@

/**
* JWT認証
*
* @see https://jwt.io/
*/
class JWT extends Protocol
Expand Down Expand Up @@ -240,7 +240,6 @@ public function generateExpiredAt(int $timestamp): int

/**
* {@inheritdoc}
* @throws DatabaseException
*/
public function authorize(AuthItem $item): bool
{
Expand Down Expand Up @@ -307,7 +306,6 @@ public function deAuthorize(): bool
*
* @param AuthItem|null $item
* @return bool true:チェック成功, false:チェック失敗
* @throws DatabaseException
*/
public function isAuthenticated(AuthItem $item = null): bool
{
Expand Down
128 changes: 31 additions & 97 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Citrus;

use Citrus\Configure\Application;
use Citrus\Controller\WebController;
use Citrus\Controller\ApiController;
use Citrus\Http\Header;
use Citrus\Http\Server\Request;

Expand All @@ -32,16 +32,26 @@ class Gateway
/**
* gateway main logic
*
* @param string|null $type リクエストタイプ
* @param string|null $type リクエストタイプ
* @param array|null $configures 設定配列
*/
public static function main(string $type = null): void
public static function main(string $type = null, array $configures = []): void
{
// security null byte replace
$search = "\0";
$replace = '';
foreach ($_GET as &$one) { $one = str_replace($search, $replace, $one); }
foreach ($_POST as &$one) { $one = str_replace($search, $replace, $one); }
foreach ($_REQUEST as &$one) { $one = str_replace($search, $replace, $one); }
foreach ($_GET as &$one)
{
$one = str_replace($search, $replace, $one);
}
foreach ($_POST as &$one)
{
$one = str_replace($search, $replace, $one);
}
foreach ($_REQUEST as &$one)
{
$one = str_replace($search, $replace, $one);
}

// セッション処理開始
switch ($type)
Expand All @@ -52,7 +62,7 @@ public static function main(string $type = null): void
break;
case self::TYPE_COMMAND:
Session::part();
self::command();
self::command($configures);
break;
default:
}
Expand All @@ -74,7 +84,7 @@ protected static function controller(): void
// クラスパス
$class_path = $controller_namespace . '\\Controller' . $router->toClassPath('Controller');

/** @var WebController $controller */
/** @var ApiController $controller */
$controller = new $class_path();
$controller->run($router);

Expand All @@ -96,96 +106,20 @@ protected static function controller(): void

/**
* cli command main logic
*
* @param array $configures 設定配列
*/
protected static function command(): void
protected static function command(array $configures): void
{

// $command = new static();
// $command->configures = $configures;
// $command->options();
// $command->before();
// $command->execute();
// $command->after();


// try
// {
// $command = Command::callCommand();
// $command->before();
// $command->execute();
// $command->after();
// }
// catch (SqlmapException $e)
// {
// Logger::debug($e);
// }
// catch (AutoloaderException $e)
// {
// Logger::debug($e);
// }
// コマンドから指定したクラス
$options = getopt('', ['command:']);
$command_class = $options['command'];
// コントローラー名前空間
$controller_namespace = '\\' . ucfirst(Application::sharedInstance()->id);
// クラスパス
$class_path = $controller_namespace . '\\Command\\' . $command_class . 'Command';

/** @var Console $class_path */
$class_path::runner($configures);
}



// /**
// * ドキュメントのパスを取得する
// *
// * @param string $gateway_type ゲートウェイタイプ
// * @return string
// */
// protected static function documentPath(string $gateway_type): string
// {
// // コントローラー
// if (self::TYPE_CONTROLLER === $gateway_type)
// {
// // ルートアイテム
// $router_item = Router::sharedInstance()->factory();
//
// // プロトコル
// $protocol = $router_item->protocol;
// $protocol_code = ucfirst(strtolower($protocol->))
//
// $document = $router_item->document;
// $action = $router_item->action;
//// $device_code = $router_item->get('device');
//// $document_code = $router_item->get('document');
//
//
//
//
// // ドキュメントコード
// $ucfirst_document_codes = [];
// foreach (explode('-', $document_code) as $one)
// {
// $ucfirst_code = ucfirst($one);
// $ucfirst_document_codes[] = $ucfirst_code;
// }
//
// // 頭文字だけ大文字で後は小文字のterm
// $ucfirst_device_code = ucfirst(strtolower($device_code));
//
// // 頭文字だけ大文字で後は小文字のAPPLICATION_CD
// $ucfirst_application_id = ucfirst(Application::sharedInstance()->id);
//
// // 末尾を取り除く
// $ucfirst_document_code = array_pop($ucfirst_document_codes);
// $controller_namespace = '\\' . $ucfirst_application_id . '\\Controller\\' . $ucfirst_device_code;
// foreach ($ucfirst_document_codes as $one)
// {
// $controller_namespace .= ('\\' . $one);
// }
// $controller_class_name = $ucfirst_document_code . 'Controller';
//
// // I have control
// $controller_namespace_class_name = $controller_namespace . '\\' . $controller_class_name;
// /** @var Page $controller */
// $controller = new $controller_namespace_class_name();
// $controller->run();
//
//
//
// $request = new Request();
// $request->requestPath();
// }
// }
}
28 changes: 28 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright 2020, CitrusFramework. All Rights Reserved.
* @author take64 <[email protected]>
* @license http://www.citrus.tk/
*/

namespace Test;

use PHPUnit\Framework\TestCase;

/**
* ゲートウェイ処理のテスト
*/
class GatewayTest extends TestCase
{
/**
* @test
*/
public function main_command_想定通り()
{
$result = exec('cd tests; ./command.php --domain=example.com --command=Sample\\\SampleData');
$this->assertSame('execute!', $result);
}
}
37 changes: 37 additions & 0 deletions tests/Sample/Command/Sample/SampleDataCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright 2020, CitrusGeneration. All Rights Reserved.
* @author take64 <[email protected]>
* @license http://www.citrus.tk/
*/

namespace Test\Sample\Command\Sample;

use Citrus\Configure\ConfigureException;
use Citrus\Console;

/**
* Sampleデータコマンド
*/
class SampleDataCommand extends Console
{
/** @var array command options */
protected $options = [
];



/**
* {@inheritDoc}
*
* @throws ConfigureException
*/
public function execute(): void
{
parent::execute();
echo 'execute!';
}
}
34 changes: 34 additions & 0 deletions tests/command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

/**
* @copyright Copyright 2020, CitrusFramework. All Rights Reserved.
* @author take64 <[email protected]>
* @license http://www.citrus.tk/
*/

date_default_timezone_set('Asia/Tokyo');

require_once '../vendor/autoload.php';

use Citrus\Authentication;
use Citrus\Configure\Application;
use Citrus\Database\Connection\ConnectionPool;
use Citrus\Database\DSN;
use Citrus\Gateway;
use Citrus\Logger;
use Citrus\Router;

$configure_path = dirname(__FILE__).'/citrus-configure.php';
$configures = include($configure_path);
Application::sharedInstance()->loadConfigures($configures);
Authentication::sharedInstance()->loadConfigures($configures);
Logger::sharedInstance()->loadConfigures($configures);
Router::sharedInstance()->loadConfigures($configures);

// コネクションプール
ConnectionPool::callConnection(DSN::getInstance()->loadConfigures($configures), true);

Gateway::main(Gateway::TYPE_COMMAND, $configures);

0 comments on commit 1dc4ef2

Please sign in to comment.