corex/support

此包已被弃用,不再维护。没有建议的替代包。

支持类和辅助函数

3.3.4 2019-03-17 09:50 UTC

README

支持类和辅助函数。此包的目的是拥有一个包含最基本类和辅助函数的包。部分代码灵感来源于Laravel、Yii和其他框架。

License Build Status codecov

System/Cache

缓存。

一些示例。

// Generate key based on string + array.
$key = Cache::key('test', ['param1' => 'Something']);

// Set path for cache stores.
Cache::path('/path/cache/stores');

// Set lifetime for cache in seconds.
Cache::lifetime(600);

// Set lifetime for cache in minutes.
Cache::lifetime('60m');

// Set lifetime for cache in hours.
Cache::lifetime('1h');

// Get from cache from 'custom-store'.
$data = Cache::get('test', 'default.value', 'custom-store');

// Put data in cache to 'custom-store'.
Cache::put('test', 'data', 'custom-store');

// Flush cache 'custom-store'.
Cache::flush('custom-store');

System/Console

各种控制台辅助函数。

一些示例。

// Writeln text.
Console::writeln('this is a test');

// Writeln texts.
Console::writeln(['this is a test', 'this is line 2']);

// Show header.
Console::header('this is a test');

// Ask question.
$answer = Console::ask('Enter name');

// Enter password.
$password = Console::secret('Enter password');

// Show table.
Console::table($items, ['Header 1', 'Header 2']);

// Throw error (exception).
Console::throwError('this is an error');

System/Directory

各种目录辅助函数。

一些示例。

// Test if directory exists.
$exist = Directory::exist('/my/path');

// Check if directory is writeable.
$isWriteable = Directory::isWritable('/my/path');

// Make directory.
Directory::make('/my/path');

// Get entries of a directory.
$entries = Directory::entries('/my/path', '*', true, true, true);

System/File

各种文件辅助函数(例如stub,json等)

一些示例。

// Check if file exists.
$exist = File::exist($filename);

// Get from file.
$content = File::get($filename);

// Load lines.
$lines = File::getLines($filename);

// Save content.
File::put($filename, $content);

// Save lines.
File::putLines($filename, $lines);

// Get stub.
$stub = File::getStub($filename, [
    'firstname' => 'Roger',
    'lastname' => 'Moore'
]);

// Get template.
$template = File::getTemplate($filename, [
    'firstname' => 'Roger',
    'lastname' => 'Moore'
]);

// Get json.
$array = File::getJson($filename);

// Put json.
File::putJson($filename, $array);

// Get temp filename.
$filename = File::getTempFilename();

// Delete file.
File::delete($filename);

System/Input

各种从环境获取信息的辅助函数。

一些示例。

// Get base url.
$baseUrl = Input::getBaseUrl();

// Get user agent.
$userAgent = Input::getUserAgent();

// Get remote address.
$remoteAddress = Input::getRemoteIp();

// Get headers.
$headers = Input::getHeaders();

System/Path

基本路径获取器(可以通过覆盖getPackagePath()在其他包中使用)。

一些示例。

// Get root of project.
$pathRoot = Path::root();

// Get config-path of project-root.
$pathConfig = Path::root(['config']);

// Get name of package.
$package = Path::packageName();

// Get name of vendor.
$package = Path::vendorName();

System/Session

会话处理器。

一些示例。

// Set session variable.
Session::set('actor', 'Roger Moore');

// Get session variable.
$actor = Session::get('actor');

// Check if session variable exists.
if (!Session::has('actor')) {
}

System/Token

令牌处理器(使用会话处理器)。

一些示例。

// Create csrf token.
$csrfToken = Token::create('csrf');

// Check csrf token.
if (!Token::isValid($csrfToken)) {
}

Arr

各种数组辅助函数。

一些示例。

// Get firstname from array via dot notation.
$firstname = Arr::get($array, 'actor.firstname');

// Set firstname on array via dot notation.
Arr::set($array, 'actor.firstname', $firstname);

// Pluck firstnames from list of actors.
$firstnames = Arr::pluck($actors, 'firstname');

Collection

元素(集合)操作的辅助函数。

一些示例。

// Update each element in collection.
$collection = new Collection($actors);
$collection->each(function (&$actor) {
    $actor->firstname = 'Mr. ' . $actor->firstname;
});

// Get sum of value.
$collection = new Collection($values);
$sum = $collection->sum('amount');

// Loop through actors.
$collection = new Collection($actors);
foreach ($collection => $actor) {
    var_dump($actor->firstname);
};

// Get last element.
$collection = new Collection($actors);
$lastElement = $collection->last();

Bag

简单的包结构。

一些示例。

// Get json.
$bag = new Bag();
$bag->set('actor.firstname', 'Roger');

// Get firstname of actor using dot notation.
$firstname = $bag->get('actor.firstname');

Base/BaseProperties (抽象)

简单的抽象类,可以选择解析数组数据,该数据将被解析为类上的现有属性(私有、受保护和公共)。

class BaseProperties extends \CoRex\Support\Base\BaseProperties
{
    private $privateValue;
    protected $protectedValue;
    public $publicValue;
}
$properties = new BaseProperties([
    'privateValue' => 'something',
    'protectedValue' => 'something',
    'publicValue' => 'something'
]);

Str

各种字符串辅助函数(多字节)。

一些示例。

// Get first 4 characters of string.
$left = Str::left($string, 4);

// Check if string starts with 'Test'.
$startsWith = Str::startsWith($string, 'Test');

// Limit text to 20 characters with '...' at the end.
$text = Str::limit($text, 20, '...');

// Replace tokens.
$text = Str::replaceToken($text, [
    'firstname' => 'Roger',
    'lastname' => 'Moore'
]);

// Create a unique string.
$identifier = Str::unique();

// Convert to pascal case.
$data = Convention::pascalCase($data);

// Convert to camel case.
$data = Convention::camelCase($data);

// Convert to snake case.
$data = Convention::snakeCase($data);

// Convert to kebab case.
$data = Convention::kebabCase($data);

StrList

各种字符串列表辅助函数(多字节)。

一些示例。

// Add 'test' to string with separator '|'.
$string = StrList::add($string, 'test', '|');

// Remove 'test' from string.
$string = StrList::remove($string, 'test', '|');

// Check if 'test' exist in string.
$exist = StrList::exist($string, 'test');