spaceboy / config-slalom
使服务器依赖的配置滑翔变得简单易读。
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-12 05:08:03 UTC
README
在PHP中轻松进行配置魔法!
安装
对于PHP 5.3或更高版本,使用composer
composer require spaceboy/config-slalom
对于较低版本,复制PHP52
目录并需要autoload.php
文件
<?php
...
require_once('PHP52/autoload.php');
...
示例
use Spaceboy\ConfigSlalom\Slalom;
$configurator = Slalom::start(new Configurator)
->when()
->serverNameIs('localhost')
->andContinue()
->when()
->serverNameIsIn(['localhost', 'production-server.com'])
->execute(function ($configurator) {
// do something
})
->andContinue()
->when()
->requestUriStarts('/img/')
->execute(function ($configurator) {
$configurator->handleImages();
})
->when()
->requestUriIs('/upload')
->methodIs('POST')
->skip(file_exists('uploaded.file'))
->execute(function ($configurator) {
// do something
})
->otherwise()
->throwException(new \Exception('Wrong server host.'))
->finally()
->execute(
function ($configurator) {
// do something
}
)
->run();
公共方法
-
start([mixed $configurator]): Slalom
第一指令。
启动整个"滑翔"。
-
when([bool $apply = TRUE]): Slalom
指令。
启动一个选项。
每次您想用新的条件集启动新选项时,请使用它。
当
$apply
为TRUE
(或未设置)时,新选项开始。否则,忽略整个when
子句。
操作
-
throwException(throwable $throwable): Slalom
提供操作。
抛出
$throwable
。 -
execute(callable $callable[, mixed $args]): Slalom
提供操作。
执行
$callable
(自定义操作),可能带有$args
。传递给$callable
的第一个参数始终是$configurator
。 -
define(string $name, mixed $value): Slalom
-
defineNow(string $name, mixed $value): Slalom
条件
-
serverNameIs(string $serverName): Slalom
添加条件。
如果当前服务器名(
$_SERVER['SERVER_NAME']
)等于$serverName
,则继续。如果不等于,则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
serverNameIsNot(string $serverName): Slalom
添加条件。
如果当前服务器名(
$_SERVER['SERVER_NAME']
)不等于$serverName
,则继续。否则,继续到下一个意见(下一个when
、otherwise
或finally
)。 -
serverNameIsIn(array $serverNameArray): Slalom
添加条件。
如果当前服务器名(
$_SERVER['SERVER_NAME']
)在$serverNameArray
中,则继续。如果不等于,则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
serverNameNotIn(array $serverNameArray): Slalom
添加条件。
如果当前服务器名(
$_SERVER['SERVER_NAME']
)不在$serverNameArray
中,则继续。否则,继续到下一个意见(下一个when
、otherwise
或finally
)。 -
serverNameMatches(string $serverNamePattern): Slalom
添加条件。
如果当前服务器名(
$_SERVER['SERVER_NAME']
)匹配$serverNamePattern
,则继续。如果不等于,则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
serverNameNotMatches(string $serverNamePattern): Slalom
添加条件。
如果当前服务器名(
$_SERVER['SERVER_NAME']
)不匹配$serverNamePattern
,则继续。否则,继续到下一个意见(下一个when
、otherwise
或finally
)。 -
requestUriIs(string $requestUri): Slalom
添加条件。
如果当前请求URI(
$_SERVER['REQUEST_URI']
)等于$requestUri
,则继续。如果不等于,则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
requestUriStarts(string $requestUri): Slalom
添加条件。
如果当前请求URI(
$_SERVER['REQUEST_URI']
)以$requestUri
开头,则继续。如果不等于,则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
requestUriContains(string $requestUri): Slalom
添加条件。
如果当前请求URI(
$_SERVER['REQUEST_URI']
)包含$requestUri
,则继续。如果不等于,则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
requestUriNotContains(string $requestUri): Slalom
添加条件。
如果当前请求URI(
$_SERVER['REQUEST_URI']
)不包含$requestUri
,则继续。否则,继续到下一个意见(下一个when
、otherwise
或finally
)。 -
requestMethodIs(string $requestMethod): Slalom
添加条件。
如果当前请求方法(
$_SERVER['REQUEST_METHOD']
)等于$requestMethod
,则继续。如果不等于,则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
requestMethodIsNot(string $requestMethod): Slalom
添加条件。
如果当前请求方法(
$_SERVER['REQUEST_METHOD']
)不等于$requestMethod
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
requestMethodIsIn(array $requestMethodArray): Slalom
添加条件。
如果当前请求方法(
$_SERVER['REQUEST_METHOD']
)在$requestMethodArray
中,则继续。如果不在,则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
requestMethodNotIn(array $requestMethodArray): Slalom
添加条件。
如果当前请求方法(
$_SERVER['REQUEST_METHOD']
)不在$requestMethodArray
中,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
isTrue(callable $callable): Slalom
添加条件。
如果
$callable
的结果是布尔值true,则继续。如果不是,则继续到下一个意见(下一个when
,otherwise
或finally
)。$callable
期望1个参数:$configurator
。 -
portIs(string $port): Slalom
添加条件。
如果当前端口(
$_SERVER['SERVER_PORT']
)等于$port
,则继续。如果不等于,则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
portIsNot(string $port): Slalom
添加条件。
如果当前端口(
$_SERVER['SERVER_PORT']
)不等于$port
,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
portIsIn(array $portArray): Slalom
添加条件。
如果当前端口(
$_SERVER['SERVER_PORT']
)在$portArray
中,则继续。如果不在这个数组中,则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
portIsNotIn(array $portArray): Slalom
添加条件。
如果当前端口(
$_SERVER['SERVER_PORT']
)不在这个数组中,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
protocolIs(string $protocol): Slalom
添加条件。
如果当前协议(
$_SERVER['SERVER_PORT']
)等于$protocol
,则继续。如果不等于,则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
protocolIsNot(string $protocol): Slalom
添加条件。
如果当前协议(
$_SERVER['SERVER_PORT']
)不等于$protocol
,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
protocolIsIn(array $protocolArray): Slalom
添加条件。
如果当前协议(
$_SERVER['SERVER_PORT']
)在$protocolArray
中,则继续。如果不在这个数组中,则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
protocolIsNotIn(array $protocolArray): Slalom
添加条件。
如果当前协议(
$_SERVER['SERVER_PORT']
)不在这个数组中,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。 -
phpVersionIs(integer $phpVersion): Slalom
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)等于$phpVersion
,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,采用PHP_VERSION_ID
常量格式(例如,PHP 5.2.7为50207)。 -
phpVersionIsNot(integer $phpVersion): Slalom
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)不等于$phpVersion
,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,采用PHP_VERSION_ID
常量格式(例如,PHP 5.2.7为50207)。 -
phpVersionIsIn(integer[] $phpVersions): Slalom
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)在$phpVersions
中,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,采用PHP_VERSION_ID
常量格式(例如,PHP 5.2.7为50207)。 -
phpVersionNotIn(integer[] $phpVersions): Slalom
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)不在这个数组中,则继续。否则继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,采用PHP_VERSION_ID
常量格式(例如,PHP 5.2.7为50207)。 -
phpVersionLT(integer $phpVersion): 滑浪
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)小于$phpVersion
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,格式为PHP_VERSION_ID
常量(例如,50207表示PHP 5.2.7)。 -
phpVersionLE(integer $phpVersion): 滑浪
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)小于或等于$phpVersion
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,格式为PHP_VERSION_ID
常量(例如,50207表示PHP 5.2.7)。 -
phpVersionEQ(integer $phpVersion): 滑浪
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)等于$phpVersion
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,格式为PHP_VERSION_ID
常量(例如,50207表示PHP 5.2.7)。 -
phpVersionNE(integer $phpVersion): 滑浪
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)不等于$phpVersion
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,格式为PHP_VERSION_ID
常量(例如,50207表示PHP 5.2.7)。 -
phpVersionGE(integer $phpVersion): 滑浪
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)大于或等于$phpVersion
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,格式为PHP_VERSION_ID
常量(例如,50207表示PHP 5.2.7)。 -
phpVersionGT(integer $phpVersion): 滑浪
添加条件。
如果当前PHP版本(
PHP_VERSION_ID
)大于$phpVersion
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。参数$phpVersion
是整数,格式为PHP_VERSION_ID
常量(例如,50207表示PHP 5.2.7)。 -
phpSapiIs(string $sapi): 滑浪
添加条件。
如果当前PHP构建的服务器API(
PHP_SAPI
)等于$sapi
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
phpSapiIsNot(string $sapi): 滑浪
添加条件。
如果当前PHP构建的服务器API(
PHP_SAPI
)不等于$sapi
,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
phpSapiIsIn(string[] $sapi): 滑浪
添加条件。
如果当前PHP构建的服务器API(
PHP_SAPI
)在$sapi
中,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
phpSapiIsNotIn(string[] $sapi): 滑浪
添加条件。
如果当前PHP构建的服务器API(
PHP_SAPI
)不在$sapi
中,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
fileExists(string $fileName): 滑浪
添加条件。
如果文件
$fileName
存在,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
fileNotExists(string $fileName): 滑浪
添加条件。
如果文件
$fileName
不存在,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
fileIsReadable(string $fileName): 滑浪
添加条件。
如果文件
$fileName
可读,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
fileIsNotReadable(string $fileName): 滑浪
添加条件。
如果文件
$fileName
不可读,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
fileIsFile(string $fileName): 滑浪
添加条件。
如果文件
$fileName
是文件,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
fileIsNotFile(string $fileName): 滑浪
添加条件。
如果文件
$fileName
不是文件,则继续。否则,继续到下一个意见(下一个when
,otherwise
或finally
)。 -
fileIsDir(string $fileName): 滑浪
添加条件。
如果文件
$fileName
是目录,则继续。否则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
fileIsNotDir(string $fileName): Slalom
添加条件。
如果文件
$fileName
不是目录,则继续。否则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
fileIsLink(string $fileName): Slalom
添加条件。
如果文件
$fileName
是链接,则继续。否则继续到下一个意见(下一个when
、otherwise
或finally
)。 -
fileIsNotLink(string $fileName): Slalom
添加条件。
如果文件
$fileName
不是链接,则继续。否则继续到下一个意见(下一个when
、otherwise
或finally
)。
指令
-
andContinue(): Slalom
指令。
在指令执行后继续。
-
onExceptionContinue([bool $continue = TRUE]): Slalom
指令。
当抛出异常时,
Slalom
运行继续。不会覆盖
onException
。当设置为
TRUE
时,由throwException
抛出的异常也会被忽略。 -
onException(callable $callback): Slalom
指令。
当抛出异常时,执行
$callback
。当设置时,自动将
onExceptionContinue
设置为FALSE
。$callback
执行时带有两个参数Exception $exception
:抛出的异常mixed $configurator
:$configurator
当设置时,即使由
throwException
指令抛出异常也会运行。 -
otherwise(): Slalom
指令。
定义当没有其他动作符合条件时将执行的动作。
-
skip([bool $skip = TRUE]): Slalom
指令。
当
$skip
为TRUE
(或未设置)时,跳过此子句并继续。 -
run(): mixed
最后一个指令。
执行整个"slalom";如果没有此方法,则什么都不做。
返回
start()
方法中传入的$configurator
。
可能的冲突
对于版本低于"5.2.7-extra"的PHP,定义了常量PHP_VERSION_ID
。因此,如果您在包含ConfigSlalom
之后定义常量PHP_VERSION_ID
,则可能会遇到冲突。