Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "library",
"license": "MIT",
"require": {
"apache/log4php": "2.3.0"
"apache/log4php": "2.3.0",
"topthink/think-log": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion src/App/Demo.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

class HttpRequest {
function get($url) {
$ch = curl_init($url);
Expand All @@ -9,6 +10,7 @@ function get($url) {
return $result;
}
}

class Demo {
private $_logger;
private $_req;
Expand All @@ -28,7 +30,7 @@ function get_user_info() {
$result_arr = json_decode($result, true);
if (in_array('error', $result_arr) && $result_arr['error'] == 0) {
if (in_array('data', $result_arr)) {
return $result_arr['data']
return $result_arr['data'];
}
} else {
$this->_logger->error("fetch data error.");
Expand Down
46 changes: 27 additions & 19 deletions src/Service/AppLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@

namespace App\Service;

class AppLogger
abstract class AppLogger
{
const TYPE_LOG4PHP = 'log4php';
/**
* 打印信息日志
* @param $message
* @return mixed
*/
abstract function info($message = '');

private $logger;
/**
* 调试信息
* @param $message
* @return mixed
*/
abstract function debug($message = '');

public function __construct($type = self::TYPE_LOG4PHP)
{
if ($type == self::TYPE_LOG4PHP) {
$this->logger = \Logger::getLogger("Log");
}
}
/**
* 错误信息
* @param $message
* @return mixed
*/
abstract function error($message = '');

public function info($message = '')
{
$this->logger->info($message);
}

public function debug($message = '')
public static function getInstance($className)
{
$this->logger->debug($message);
}
$className = ucfirst(trim($className));
$classFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'interface' . DIRECTORY_SEPARATOR . $className . ".php";
if (!file_exists($classFile))
exit("Logger file $classFile is not exist");
include_once($classFile);

public function error($message = '')
{
$this->logger->error($message);
$cl = new \ReflectionClass($className);
return $cl->newInstance();
}
}
56 changes: 56 additions & 0 deletions src/Service/ProductHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,61 @@

class ProductHandler
{
/**
* 统计商品总额
* @param $products
* @return int|mixed
*/
public static function countAllPrice($products)
{
$totalPrice = 0;
foreach ($products as $product) {
$price = $product['price'] ?: 0;
$totalPrice += $price;
}
return $totalPrice;
}

/**
* 排序并归类
* @param $products
* @param string $type
* @return array
*/
public static function sortAndGroup($products, string $type = ''): array
{
if (!is_array($products)) {
return [];
}
$array_column = array_column($products, 'price'); // 根据产品排序
array_multisort($array_column, SORT_DESC, $products);
if ($type) {
$data = [];
foreach ($products as $v) {
if ($type == $v['type']) {
$data[] = $v;
}
}
return $data;
}
return $products;
}

/**
* 转换时间
* @param $products
* @param string $index
* @return array
*/
public static function transformTime($products, string $index = 'create_at'): array
{
if (!is_array($products)) {
return [];
}
foreach ($products as &$row) {
$row[$index] = strtotime($row[$index]);
}
return $products;
}

}
35 changes: 35 additions & 0 deletions src/Service/interface/Log4php.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php


use App\Service\AppLogger;

class Log4Php extends AppLogger
{

/**
* @var \Logger
*/
private $logger;

function __construct()
{
$this->logger = \Logger::getLogger("Log");
}

function info($message = '')
{
$this->logger->info($message);
}

function debug($message = '')
{
$this->logger->debug($message);

}

function error($message = '')
{
$this->logger->error($message);

}
}
38 changes: 38 additions & 0 deletions src/Service/interface/ThinkLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


use App\Service\AppLogger;
use think\facade\Log;

class ThinkLog extends AppLogger
{

function __construct()
{
Log::init([
'default' => 'file',
'channels' => [
'file' => [
'type' => 'file',
'path' => './logs/',
],
],
]);
}

function info($message = '')
{
Log::info($message);
}

function debug($message = '')
{
Log::debug($message);

}

function error($message = '')
{
Log::error($message);
}
}
22 changes: 22 additions & 0 deletions tests/App/DemoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Test\App;

use App\Service\AppLogger;
use HttpRequest;
use PHPUnit\Framework\TestCase;

/**
* Class ProductHandlerTest
*/
class DemoTest extends TestCase
{

public function testGetUserInfo()
{
$demo = new \Demo(AppLogger::getInstance('log4php'), new HttpRequest());
$data = $demo->get_user_info();
print_r($data);
}

}
2 changes: 1 addition & 1 deletion tests/Service/AppLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AppLoggerTest extends TestCase

public function testInfoLog()
{
$logger = new AppLogger('log4php');
$logger = AppLogger::getInstance('log4php');
$logger->info('This is info log message');
}
}
15 changes: 9 additions & 6 deletions tests/Service/ProductHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ class ProductHandlerTest extends TestCase

public function testGetTotalPrice()
{
$totalPrice = 0;
foreach ($this->products as $product) {
$price = $product['price'] ?: 0;
$totalPrice += $price;
}
$this->assertEquals(143, ProductHandler::countAllPrice($this->products));
}

public function testOrderProduct(){
print_r(ProductHandler::sortAndGroup($this->products));
print_r(ProductHandler::sortAndGroup($this->products,'Dessert'));
}

$this->assertEquals(143, $totalPrice);
public function testTransformTime(){
print_r(ProductHandler::transformTime($this->products));
}
}