热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

centos开发套件_替代的Laravel套件开发工作流程

centos开发套件ThisarticlewaspeerreviewedbyFrancescoMalatesta.ThankstoallofSitePoint’speerrevie

centos 开发套件

This article was peer reviewed by Francesco Malatesta. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

本文由Francesco Malatesta进行同行评审。 感谢所有SitePoint的同行评审人员使SitePoint内容达到最佳状态!



Every framework gives developers a way to extend the system using packages / extensions. We can generally hook in our logic at any point where we want to provide specific functionality, and Laravel is no exception! Following the article of my fellow author Francesco Malatesta about his Laravel package development workflow, I noticed that mine is a bit different and I wanted to share it with you!

每个框架都为开发人员提供了使用包/扩展来扩展系统的方法。 通常,我们可以在希望提供特定功能的任何地方加入我们的逻辑,Laravel也不例外! 下面的文章我的同胞作家弗朗西斯马爱德他Laravel包的开发流程,我注意到我的是一个有点不同,我想与大家分享吧!

Laravel Package

演示包 (Demo Package)

As a demo package, we’re going to build a Laravel two factor authentication package for this article. The package will handle the user authentication with a verification code being sent through a medium like Nexmo, Twilio, etc. You can check out the final package here.

作为一个演示程序包,我们要建立一个Laravel双因素身份验证软件包这一文章 。 该软件包将通过通过Nexmo , Twilio等介质发送的验证码来处理用户身份验证。您可以在此处查看最终软件包。

设置存储库 (Setting up the Repository)

Before we start the development, we need to create a new repository inside GitHub so we can push/pull from it through the development phase.

在开始开发之前,我们需要在GitHub内创建一个新的存储库,以便我们可以在开发阶段将其推入/拉出。

Composer supports a repositories key inside the composer.json file. We can use this to specify our custom packages that don’t yet exist on Packagist.

Composer在composer.json文件中支持repositories密钥。 我们可以使用它来指定Packagist尚不存在的自定义软件包。

{
// ....
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Whyounes/laravel-two-factor-auth-demo"
}
],
// ....
}

Now, we can require our package as usual.

现在,我们可以照常要求包装。

{
// ....
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"Whyounes/laravel-two-factor-auth-demo": "dev-master"
},
// ....
}

We can specify a branch or a version number. In case we want to use a branch, it should be prefixed with dev-. You can read more about configuring repositories on the Packagist website.

我们可以指定分支或版本号。 如果我们要使用分支,则应在其前面加上dev- 。 您可以在Packagist网站上阅读有关配置存储库的更多信息 。

包装骨架 (Package Skeleton)

Since Composer identifies packages using the composer.json file, we need to create one for our package and push it to the repository.

由于Composer使用composer.json文件识别软件包,因此我们需要为我们的软件包创建一个并将其推送到存储库。

{
"name": "whyounes/laravel-two-factor-auth",
"type": "library",
"description": "Laravel two factor authentication",
"keywords": [
"auth",
"passwordless"
],
"homepage": "https://github.com/whyounes/laravel-two-factor-auth",
"license": "MIT",
"authors": [
{
"name": "Rafie Younes",
"email": "younes.rafie@gmail.com",
"homepage": "http://younesrafie.com",
"role": "Developer"
}
],
"require": {
"php": "^5.5.9 || ^7.0",
"illuminate/database": "5.1.* || 5.2.* || 5.3.* || 5.4.*",
"illuminate/config": "5.1.* || 5.2.* || 5.3.* || 5.4.*",
"illuminate/events": "5.1.* || 5.2.* || 5.3.* || 5.4.*",
"illuminate/auth": "5.1.* || 5.2.* || 5.3.* || 5.4.*",
"twilio/sdk": "^5.4"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0",
"orchestra/testbench": "~3.0",
"mockery/mockery": "~0.9.4"
},
"autoload": {
"psr-4": {
"Whyounes\\TFAuth\\": "src"
},
"classmap": [
"tests"
]
}
}

After running the composer update command inside the Laravel project’s root folder, we see an error that no tests were found under the tests folder. This won’t halt the installation process, so we’re fine to continue!

在Laravel项目的根文件夹中运行composer update命令后,我们看到一个错误,即在tests文件夹下找不到任何测试。 这不会停止安装过程,因此我们可以继续!

Composer update failing tests

The last part of our package configuration is to link the downloaded package to the GitHub repository.

软件包配置的最后一部分是将下载的软件包链接到GitHub存储库。

// assuming you're inside your project folder
cd vendor/whyounes/laravel-two-factor-auth
git init origin git@github.com:Whyounes/laravel-two-factor-auth.git
git commit -am "Init repo"
git push origin master -f // force push for the first time

包装结构 (Package Structure)

This part is a little bit controversial, we can’t all agree on one single structure. Here’s our package structure:

这部分有一点争议,我们不能在一个单一的结构上达成一致 。 这是我们的包结构:

▶ config/
auth.php
services.php
▶ migrations/
2014_10_1_000000_add_tfa_columns_to_users_table.php
2016_12_1_000000_create_tokens_table.php
▶ resources/
▶ views/
verification_code.blade.php
▶ src/
▶ Contracts/
VerificationCodeSenderInterface.php
▶ Controllers/
TFAController.php
▶ Exceptions/
TFANotEnabledException.php
UserNotFoundException.php
▶ Models/
TFAuthTrait.php
Token.php
▶ Providers/
TwoFAProvider.php
▶ Services/
Twilio.php
▶ tests/

To install the package we use a Laravel provider:

要安装软件包,我们使用Laravel提供程序:

// config/app.php
// ...
'providers' => [
// ...
Whyounes\TFAuth\TwoFAProvider::class,
};

The provider class looks like this:

提供程序类如下所示:

// src/Providers/TwoFAProvider.php
namespace Whyounes\TFAuth;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Twilio\Rest\Client;
use Whyounes\TFAuth\Contracts\VerificationCodeSenderInterface;
use Whyounes\TFAuth\Services\Twilio;
/**
* Class TwoFAProvider
*
* @package Whyounes\TFAuth
*/
class TwoFAProvider extends ServiceProvider
{
public function boot()
{
$this->loadMigrationsFrom(__DIR__ . '/../../migrations');
$this->mergeConfigFrom(
__DIR__ . '/../../config/auth.php', 'auth'
);
$this->mergeConfigFrom(
__DIR__ . '/../../config/services.php', 'services'
);
$this->loadViewsFrom(__DIR__.'/resources/views', 'tfa');
$this->publishes([
__DIR__.'/../../resources/views' => resource_path('views/vendor/tfa'),
]);
}
public function register()
{
$this->registerBindings();
$this->registerEvents();
$this->registerRoutes();
}
public function registerBindings()
{
$this->app->singleton(Client::class, function () {
return new Client(config('services.twilio.sid'), config('services.twilio.token'));
});
$this->app->bind(VerificationCodeSenderInterface::class, Twilio::class);
}
public function registerEvents()
{
// Delete user tokens after login
if (config('auth.delete_verification_code_after_auth', false) === true) {
Event::listen(
Authenticated::class,
function (Authenticated $event) {
$event->user->tokens()
->delete();
}
);
}
}
public function registerRoutes()
{
/** @var $router Router */
$router = App::make("router");
$router->get("/tfa/services/twilio/say/{text}", ["as" => "tfa.services.twilio.say", "uses" => function ($text) {
$response = "" . $text . "";
return $response;
}]);
}
}

Let’s go through every method here:

让我们在这里浏览每种方法:

  • The boot method will be publishing our package assets (config, migrations, views).

    boot方法将发布我们的程序包资产(配置,迁移,视图)。

  • The registerBindings method binds the VerificationCodeSenderInterface to a specific implementation. The package uses the Twilio service as a default, so we create a src/Services/Twilio class which implements our interface and defines the necessary methods.

    registerBindings方法将VerificationCodeSenderInterface绑定到特定的实现。 该软件包默认使用Twilio服务,因此我们创建了一个src/Services/Twilio类,该类实现了我们的接口并定义了必要的方法。

// src/Services/Twilio.php
namespace Whyounes\TFAuth\Services;
use Twilio\Rest\Client;
use Whyounes\TFAuth\Contracts\VerificationCodeSenderInterface;
class Twilio implements VerificationCodeSenderInterface
{
/**
* Twilio client
*
* @var Client
*/
protected $client;
/**
* Phone number to send from
*
* @var string
*/
protected $number;
public function __construct(Client $client)
{
$this->client = $client;
$this->number = config("services.twilio.number");
}
public function sendCodeViaSMS($code, $phone, $message = "Your verification code is %s")
{
try {
$this->client->messages->create($phone, [
"from" => $this->number,
"body" => sprintf($message, $code)
]);
} catch (\Exception $ex) {
return false;
}
return true;
}
public function sendCodeViaPhone($code, $phone, $message = "Your verification code is %s")
{
try {
$this->client->account->calls->create(
$phone,
$this->number,
["url" => route('tfa.services.twilio.say', ["text" => sprintf($message, $code)])]
);
} catch (\Exception $ex) {
return false;
}
return true;
}
// getters and setter
}

  • The registerEvents method will delete the previously used token upon authentication if the config value is set to true.

    如果config值设置为true ,则registerEvents方法将在身份验证后删除以前使用的令牌。

  • The registerRoutes method adds a route for sending a verification code via a phone call.

    registerRoutes方法添加了用于通过电话发送验证码的路由。

If, for example, we wanted to switch from Twilio to Nexmo, we only need to create a new class that implements the VerificationCodeSenderInterface and bind it to the container.

例如,如果我们想从Twilio切换到Nexmo,我们只需要创建一个新类来实现VerificationCodeSenderInterface并将其绑定到容器即可。

Finally, we add a trait to the User model class:

最后,我们向User模型类添加一个特征:

// app/User.php
class User extends Authenticatable
{
use \Whyounes\TFAuth\Models\TFAuthTrait;
// ...
}

测验 (Tests)

Tests are mandatory before releasing any code publicly, not just for Laravel. DO NOT use any packages that don’t have tests. For Laravel, you may use the orchestra/testbench package to help you out, and the documentation provides a really nice flow for testing Laravel components like Storage, Eloquent, etc.

在公开发布任何代码之前(不仅限于Laravel),必须进行测试。 不要使用任何没有测试的软件包。 对于Laravel,您可以使用orchestra/testbench方案,帮助你,和文档提供了用于测试Laravel组件就像一个非常好的流动Storage , Eloquent

For our package, we have tests for our controller, model and service classes. Check the repository for more details about our tests.

对于我们的程序包,我们对控制器,模型和服务类进行了测试。 检查存储库以获取有关我们测试的更多详细信息。

标记 (Tagging)

We can’t keep just a master branch for our package. We need to tag a first version to start with. This depends on how far along are we in the feature development stage, and what the goal is here!

我们不能只为包保留master分支。 我们需要标记一个开始的版本。 这取决于我们在功能开发阶段的进展情况以及目标是什么!

Since our package is not that complicated and doesn’t have a lot of features to build, we can tag it as a first stable version (1.0.0). You can check the Git documentation website for more details about tagging, and here’s the list of commands for adding a new tag to our repository:

由于我们的软件包不是那么复杂,并且没有很多功能要构建,因此我们可以将其标记为第一个稳定版本( 1.0.0 )。 您可以访问Git文档网站以获取有关标记的更多详细信息,这是用于将新标记添加到我们的存储库的命令列表:

git tag
git tag -a v1.0.0 -m "First stable version"
git push origin --tags

Tagging

We should now see the tag on GitHub under the branch select box:

现在,我们应该在分支选择框下的GitHub上看到标签:

GitHub Tags

持续集成 (Continuous Integration)

Continuous Integration tools help us integrate new parts into our existing code and send a notification about any infringements. It also gives us a badge (image with a link) for the status of the code. For this package, we use TravisCI as our CI tool.

持续集成工具可帮助我们将新零件集成到现有代码中,并发送有关任何侵权的通知。 它还为我们提供了代码状态的标志(带有链接的图像)。 对于此软件包,我们使用TravisCI作为CI工具。

TravisCI Badge

// .travis.yml
language: php
php:
- 5.6
- 7.0
- 7.1
sudo: true
install:
- travis_retry composer install --no-interaction --prefer-source

This config file will be picked up by TravisCI to know how to test the code and what versions to test on. You can read more about how to integrate it here.

TravisCI将提取此配置文件,以了解如何测试代码以及​​要测试的版本。 您可以在此处阅读有关如何集成它的更多信息。

结论 (Conclusion)

The development workflow is not that complicated. In fact, we can develop a package without needing to install the Laravel framework; just include the necessary Illuminate components and test drive your package’s implementation. At the end, you can do an integration test as a demo that the package is working as expected. I hope this helps you create some packages to give back to the community :)

开发工作流程并不那么复杂。 实际上,我们可以开发一个软件包,而无需安装Laravel框架。 仅包括必要的Illuminate组件并测试驱动程序包的实现。 最后,您可以进行集成测试,以演示该软件包是否按预期工作。 我希望这可以帮助您创建一些可以回馈社区的软件包:)

If you have any questions or comments you can post them below! What’s your package development workflow?

如果您有任何疑问或意见,可以在下面发布! 您的包装开发工作流程是什么?

翻译自: https://www.sitepoint.com/alternative-laravel-package-development-workflow/

centos 开发套件



推荐阅读
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了在多平台下进行条件编译的必要性,以及具体的实现方法。通过示例代码展示了如何使用条件编译来实现不同平台的功能。最后总结了只要接口相同,不同平台下的编译运行结果也会相同。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
author-avatar
拂袖方言_633
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有