让我们谈谈如何停止项目之间的复制粘贴并将代码转移到可重复使用的插件包Symfony 5中。在实践中总结了一系列关于包的经验的文章,将从创建最小的包,重构演示应用程序到测试以及包发布周期的实践中进行。
在第一部分中:
- 为什么需要捆绑包?
- 示例项目:日历
- 定制环境:2种开发方式
- 创建一个最小的捆绑
- 将捆绑包连接到项目
什么是捆绑包,为什么需要呢?
当您(从那时到之后)厌倦了从项目到项目的复制粘贴代码并考虑重新使用它时,将需要Symfony软件包。迟早会有一种理解,那就是将代码隔离到可重用的插件中会更加方便。Symfony Bundle-这是Symfony生态系统中的一个模块。
该捆绑软件是在类固醇Symfony Framework上可重复使用的PHP代码的软件包。
捆绑包与常规的PHP包不同,它们使用的组件和公认的约定简化了与Symfony应用程序的集成,与之不同。通过遵循特定于生态系统的命名约定和代码结构,捆绑软件可以在Symfony主机应用程序中自动连接,配置和扩展。捆绑包通过依赖项管理器连接到项目composer
。
, — . , DDD- .
Example Project: Calendar
.

.
, . . , . - , .
, 10 .
: .
, , 12 .
, .
, , , . , , , composer update
.
.
README.md
.
:
.
, ?
— , File -> New... -> Symfony Project
. -, .
2 :
, Symfony . , .
, . , ./bundles
.
, .
./bundles
CalendarBundle
,
:
src/CalendarBundle.php
composer.json
2 !
composer.json
composer.json
:
{
"name": "bravik/calendar-bundle",
"version": "0.1.0",
"type": "symfony-bundle",
"description": "Symfony bundles tutorial example project",
"license": "proprietary",
"require": {
"php": "^7.3"
},
"require-dev": {
},
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"bravik\\CalendarBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"bravik\\CalendarBundle\\Tests\\": "tests/"
}
},
"scripts": {
"test" : "./vendor/bin/simple-phpunit"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "5.0.*"
}
}
}
:
"name": "bravik/calendar-bundle",
"description": "Health check bundle",
. . composer
, vendor/bravik/calendar-bundle
"type": "symfony-bundle"
Symfony, . composer
, — Symfony Flex
, — -, ( bundles.php
), "".
— Symfony Flex, , .
"version": "0.1.0",
. composer . .
"autoload": {
"psr-4": {
"bravik\\CalendarBundle\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"bravik\\CalendarBundle\\Tests\\": "tests"
}
},
composer. <VendorName>/<CategoryName>/<BundleName>Bundle
.
composer, bravik\\CalendarBundle
./src
composer.json
. -, , use <BundleNamespace>/<BundleClass>
.
dev
-. .
"require": {
"php": "^7.3"
},
"require-dev": {},
require
require-dev
prod dev . - . composer
. php
.
.
./src
CalendarBundle.php
:
<?php
namespace bravik\CalendarBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class CalendarBundle extends Bundle
{
}
. .
: <BundleName>Bundle.php
, — Bundle
. , Symfony , ( ).
,
.
bravik\CalendarBundle
, composer.json
. .
-
composer
:
composer require bravik/calendar-bundle
, composer
: .
, repositories
composer.json
.
, bundles/CalendarBundle
:
"repositories": [
{
"type" : "path",
"url" : "./bundles/CalendarBundle"
}
],
vendor
, bravik/calendar-bundle
, . vendors
, .
, git- :
"repositories": [
{
"type" : "vcs",
"url" : "git@bitbucket.org:bravik/calendarbundle.git"
}
],
composer
git- vendors/bravik/calendar-bundle
.
composer.
composer require bravik/calendar-bundle
composer.json
, . , config/bundles.php
:
<?php
return [
bravik\CalendarBundle\CalendarBundle::class => ['all' => true],
];
, !
-, ./bundles
.gitignore
, bundles/CalendarBundle
: composer init
. , .
, " " - - , .
, .
- Symfony 2 :
composer.json
MyBundle
. - - IDE GIT-, composer.
- , .
- , .
Example Project 1-bundle-mockup.
, , , DI-.
:
1.部分最小包
2的一部分,我们拿出包中的代码和模板
第3部分集成与主机捆绑的:模板,样式,JS
第4部分接口扩展包
第5部分参数及配置
部分6.测试,该包内microapplication
第7发布周期,安装和更新