winternet-studio/jensenfw2

易于使用、常用功能框架

dev-master 2024-09-27 08:46 UTC

This package is auto-updated.

Last update: 2024-09-27 08:46:27 UTC


README

许多用于执行常见任务的PHP类和方法。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

php composer.phar require --prefer-dist winternet-studio/jensenfw2 "*"

或添加以下内容到您的 composer.json 文件的require部分。

"winternet-studio/jensenfw2": "*"

使用

安装后,只需在代码中使用它即可

<?php
require_once('vendor/autoload.php');

// Check if variable is an integer in any shape or form
$result = \winternet\jensenfw2\core::is_integer(45);  //returns true
$result = \winternet\jensenfw2\core::is_integer('45');  //returns true
$result = \winternet\jensenfw2\core::is_integer(45.3);  //returns false
$result = \winternet\jensenfw2\core::is_integer('45.3');  //returns false

// Convert an amount using today's exchange rate
echo \winternet\jensenfw2\currency::convert(100, 'USD', 'EUR');

[待办] 如何设置全局类默认值

全局类默认值可以通过全局配置文件设置。我需要完成并验证文档的这一部分...

创建配置文件,例如在 /config/jensenfw2.php

<?php
class jensenfw2 {
	public static function returnConfig($class_name) {
		$map = [  //[name of class to get config for ($class_name)] => [method in this class]
			'core' => 'core',
			'mail' => 'mail',
		];

		if (!$map[$class_name]) {
			return [];
		} else {
			$self = self::class;
			return call_user_func(array($self, $map[$class_name]));
		}
	}

	public static function core() {
		$cfg = [];

		// System
		$cfg['system_name'] = 'My system name';
		$cfg['administrator_name'] = 'John Doe';
		$cfg['administrator_email'] = 'john.doe@sample.com';
		$cfg['developer_name'] = 'Jane Smith';
		$cfg['developer_email'] = 'jane.smith@sample.com';

		// Core paths and files
		$cfg['path_filesystem'] = '/var/www/html/mysite.com/web';
		$cfg['path_webserver'] = '';

		// Databases
		$cfg['databases'] = [];

		//   1st and primary server
		$cfg['databases'][0] = [   //key 0 is the server ID (primary server must always be 0) (IDs must always be numeric)
			'db_host' => 'thehost.com',
			'db_port' => 3306,
			'db_user' => 'username',
			'db_pw'   => 'password',
			'db_name' => 'mydatabasename',
		];
		$cfg['databases'][1] = [
			'db_host' => 'thehost.com',
			'db_port' => 3306,
			'db_user' => 'username',
			'db_pw'   => 'password',
			'db_name' => 'myloggingdatabase',
		];

		// See class_defaults() in core.php for all options...

		return $cfg;
	}

	public static function mail() {
		$cfg = [];
		$cfg['swift_host'] = 'some.smtp.com';
		$cfg['swift_user'] = 'username';
		$cfg['swift_pass'] = 'password';

		// See class_defaults() in mail.php for all options...

		return $cfg;
	}
}

在您的启动文件中包含此段代码

<?php
// \winternet\jensenfw2\core::$is_dev = true;  //enable this to turn on some debugging features

// NOTE: this using class autoloader from Yii2 - so if you don't have a class autoloader some changes are needed...
\winternet\jensenfw2\core::$userconfig = ['\app\config\jensenfw2', 'returnConfig'];

测试

在用 phpunit 命令运行测试之前,您应该在 networkTest.php 中启动用于测试的内部网络服务器。要做到这一点,请运行命令 php -S localhost:8018 -t tests/webserver_docroot

运行特定测试: phpunit tests/datetimeTest.php

其他说明

是的,我知道,对不起没有使用驼峰命名法。这个库的开始远远早于它变得流行,现在我也没有时间来迁移所有内容,所以这个库将继续使用下划线。