3

2020年最新整理20个必备PHP开源类库,武装生产力

 3 years ago
source link: https://www.ffeeii.com/2333.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

2020年最新整理20个必备PHP开源类库,武装生产力

php.jpg
PHP

2020年上半年已完结,PHP依旧是Z好的语言,推荐20个必备的PHP开源类库,只推荐综合排名第一的类库,其他第二第三的就不考虑了,有些类库已经年久失修,推荐就推荐这条街最靓的仔。本文覆盖了框架、单元测试、文件(excel、pdf)、日志、应用等最常用的类库,排名不分先后,请收藏,总有一天你用得着。

1、数据表格 Excel处理类库PhpSpreadsheet

Github star 8.5K,推荐指数 ★★★★☆ Github地址:https://github.com/PHPOffice/PhpSpreadsheet

PhpSpreadsheet是最新的PHP excle类型,在命名空间、PSR规范、采用新的PHP语言特性,老的 PHPExcel 已经很久没有 维护了。

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

2、单元测试框架 phpunit

Github star 16.3k,推荐指数 ★★★★☆ Github地址:https://github.com/sebastianbergmann/phpunit PHPunit官网:https://phpunit.de/

鉴于优秀的能力,很多开源框架已经内置此单元测试类库,PHPUnit 9已经支持PHP7.3和PHP7.4。

3、宇宙第一框架 Laravel

Github star 21.4k,推荐指数 ★★★★★ Github地址:https://github.com/laravel/framework 一个框架的优秀不仅是从Google指数,github star,相关的生态非常重要。因为有一个好的生态,在各应用领域会有解决方案,比如你想用Elasticsearch、想用serverless,总有程序猿帮你解决了。

4、数据库ORM框架Doctrine

Github star 21.4k,推荐指数 ★★★☆☆ Github地址:https://github.com/doctrine/orm 官网:https://www.doctrine-project.org/projects/orm.html 如果你不像使用任何框架,只想使用数据库相关操作,Doctrine是个合适的选择。对象关系映射器(ORM)和数据库抽象层(DBAL)底层类库,像Lavaral、Symfony都有代码集成,目前最稳定为2.7.3,下个大版本3.0马上也要到来。

5、Aws 服务 aws-sdk-php

Github star 4.8K,推荐指数★★★☆☆ Github地址: https://github.com/aws/aws-sdk-php 如果你的服务使用的是 Aws,那这个全家桶不错。

6、日志管理 Monolog

Github star 17.6K,推荐指数★★★★★ Github地址:https://github.com/Seldaek/monolog Monolog可以把你的日志发送到文件,sockets,收件箱,数据库和各种web服务器上。一些特殊的组件可以给你带来特殊的日志策略,包括Symfony 、Laravel、 CakePHP等诸多知名php框架都内置了Monolog

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

// add records to the log
$log->warning('Foo');
$log->error('Bar');

7、格式化php的代码 PHP-CS-Fixer

Github star 9.1K,推荐指数★★★☆☆ Github地址:https://github.com/FriendsOfPHP/PHP-CS-Fixer

php-cs-fixer fix  /path/dir 文件目录或者指定文件

8、数据填充工具faker

Github star 24.1K,推荐指数★★★★★ Github地址: https://github.com/fzaninotto/Faker 凡是用过Faker的,那绝对是神器,没有数据再也不用操心了。Faker是一个很神奇的项目,会自动生成拟真的数据,包括用户资料、长文本、IP、日期等等,在网站上线前测试时非常好用。

<?php
// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();

// generate data by accessing properties
echo $faker->name;
  // 'Lucy Cechtelar';
echo $faker->address;
  // "426 Jordy Lodge
  // Cartwrightshire, SC 88120-6700"
echo $faker->text;

9、邮件服务PHPmailer

Github star 15.1K,推荐指数★★★★☆ Github地址:https://github.com/PHPMailer/PHPMailer PHP界的扛把子,使用最广泛的PHP邮件类库, WordPress, Drupal, 1CRM, SugarCRM, Yii, Joomla都集成,支持50多个语言,发邮件,抄送,发送附件,毫无疑问,首选首选。来看用例。

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '[email protected]';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    // Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

10、html转为PDF类库dompdf

Github star 7.1K,推荐指数★★★★☆ Github地址:https://github.com/dompdf/dompdf 做企业应用的,类似文档处理,html转为pdf是常用场景。dompdf是一个CSS 2.1兼容的HTML布局和呈现引擎,用PHP编写。它是一个样式驱动的呈现程序:它将下载和读取外部样式表、内联样式标记和单个HTML元素的样式属性。它还支持大多数表示HTML属性。

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

11、Debug工具xdebug

Github star 2.1K,推荐指数★★★★☆ Github地址:https://github.com/xdebug/xdebug Xdebug是一个PHP调试工具。它提供了步骤调试和一系列开发辅助工具,如堆栈跟踪、代码分析器、将脚本的完整执行转储到文件的功能等。

php run-xdebug-tests.php

12、自动生成文档 phpdocumentor

Github star 2.6K,推荐指数★★★★☆ Github地址:https://github.com/phpDocumentor/phpDocumentor 目前稳定版v2.0,PHPDocumentor是一个用PHP写的工具,对于有规范注释的php程序,它能够快速生成具有相互参照,索引等功能的API文档。

phpdoc run -d <SOURCE_DIRECTORY> -t <TARGET_DIRECTORY>

13、Http请求curl类库 Guzzle

Github star 19.5K,推荐指数★★★★★ Github地址:https://github.com/guzzle/guzzle Guzzle是一个PHP HTTP客户端,它使发送HTTP请求变得容易,与web服务的集成变得简单。Guzzle是目前Z好用的curl类库。

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle');

echo $response->getStatusCode(); // 200
echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
echo $response->getBody(); // '{"id": 1420053, "name": "guzzle", ...}'

// Send an asynchronous request.
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});

$promise->wait();

14、日期处理Carbon

Github star 14.5K,推荐指数★★★★★ Github地址:https://github.com/briannesbitt/Carbon 官网:https://carbon.nesbot.com/

<?php
use Carbon\Carbon;
printf("Now: %s", Carbon::now());
printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));  //implicit __toString()
$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();

15、图片处理类 Image

Github star 10.9K,推荐指数★★★☆☆ Github地址:https://github.com/Intervention/image Intervention Image是一个PHP图像处理和操作库,为创建、编辑和合成图像提供了一种更简单、更具表现力的方式。Laravel内部已集成。其实飞哥更为推荐的做法是是云储存,比如阿里云OSS,腾讯COS,把处理这些的资源释放出来。

// open an image file
$img = Image::make('public/foo.jpg');

// resize image instance
$img->resize(320, 240);

// insert a watermark
$img->insert('public/watermark.png');

// save image in desired format
$img->save('public/bar.jpg');

16、功能齐全的后台管理系统 laravel-admin

Github star 8.7K,推荐指数★★★★☆ Github地址:https://github.com/z-song/laravel-admin 一个功能齐全的管理后台,集成权限,数据处理,代码生成等。

17、搜索引擎 elasticsearch-php

Github star 4.1K,推荐指数★★★☆☆ Github地址:https://github.com/elastic/elasticsearch-php

18、词库的中文转拼音

Github star 3.4K,推荐指数★★★☆☆ Github地址:https://github.com/overtrue/pinyin 基于 CC-CEDICT 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案

use Overtrue\Pinyin\Pinyin;

// 小内存型
$pinyin = new Pinyin(); // 默认
// 内存型
// $pinyin = new Pinyin('\\Overtrue\\Pinyin\\MemoryFileDictLoader');
// I/O型
// $pinyin = new Pinyin('\\Overtrue\\Pinyin\\GeneratorFileDictLoader');

$pinyin->convert('带着希望去旅行,比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lyu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]

$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_TONE);
// ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]

$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII_TONE);
//["dai4","zhe","xi1","wang4","qu4","lyu3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]

19、Swagger生成API文档

Github star 3.5K,推荐指数★★★☆☆ Github地址:https://github.com/zircote/swagger-php Swagger是一种简单、强大的RESTful API表现形式。其拥有地球上最大的API工具生态环境,无数程序员在几乎所有主流语言和开发环境中添加了对Swagger的支持。那怎么可以少了PHP的支持呢。

<?php
require("vendor/autoload.php");
$openapi = \OpenApi\scan('/path/to/project');
header('Content-Type: application/x-yaml');
echo $openapi->toYaml();

20、微信开发easywechat

Github star 9K,推荐指数★★★☆☆ Github地址:https://github.com/overtrue/wechat 公众平台、小程序、微信支付,微信相关生态集成的SDK。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK