Nice programing

Symfony2-자체 공급 업체 번들 생성-프로젝트 및 Git 전략

nicepro 2020. 10. 13. 19:22
반응형

Symfony2-자체 공급 업체 번들 생성-프로젝트 및 Git 전략


common몇 가지 개별 앱 내에서 사용할 엔티티 매핑 및 서비스를위한 자체 번들을 만드는 것을 고려 하고 있습니다. 번들은 수정, 실행, 포함 및 테스트가 쉬워야합니다. 번들 구조화의 모범 사례에 대해 알고 있지만 git개발과 관련하여 어떤 전략을 사용 해야할지 모르겠습니다 .

common번들을 전체 프로젝트로 만들고 전체 저장소를 git 서버에 커밋 해야합니까 , 아니면 common번들의 루트에 대해서만 소스 제어를 시작 하고 해당 콘텐츠 만 푸시 하는 것이 더 낫 습니까? 에서 사용할 수있는 번들에서이 접근 방식을 볼 수 github있지만 이러한 방식으로 번들 을 개발하는 쉽고 편한 방법을 모르겠습니다.


비어있는 새 심포니 프로젝트 만들기

php composer.phar create-project symfony/framework-standard-edition demo/ 2.4.1
cd demo

새 번들 생성

(예 src/Company/DemoBundle:)

php app/console generate:bundle
cd src/Company/DemoBundle/

github 저장소 초기화 src/Company/DemoBundle

git init
touch README.md
git add .
git commit -m "initial commit"
git remote add origin https://github.com/YourAccount/DemoBundle.git
git push -u origin master

composer.json 파일 추가

src/Company/DemoBundle/composer.json:

{
    "name" : "company/demobundle",
    "description" : "A demo bundle",
    "type" : "symfony-bundle",
    "authors" : [{
        "name" : "demo",
        "email" : "demo@company.com"
    }],
    "keywords" : [
        "demo bundle"
    ],
    "license" : [
        "MIT"
    ],
    "require" : {
    },
    "autoload" : {
        "psr-0" : {
            "Company\\DemoBundle" : ""
        }
    },
    "target-dir" : "Company/DemoBundle",
    "repositories" : [{
    }],
    "extra" : {
    "branch-alias" : {
            "dev-master" : "some_version-dev"
        }
    }
}

이제 번들의 기본 구조가 있습니다.

다른 프로젝트에서 사용

composer.json :

    [...]
    "require" : {
        [...]
        "company/demobundle" : "dev-master"
    },
    "repositories" : [{
        "type" : "vcs",
        "url" : "https://github.com/Company/DemoBundle.git"
    }],
    [...]

하다:

curl -sS https://getcomposer.org/installer | php
php composer.phar update company/demobundle

app / AppKernel :

new Company\DemoBundle\CompanyDemoBundle(),

일하는 중입니다

  • DemoBundle을 src/Company폴더 에 복제 한 다음 수동으로 설치할 수 있습니다.
  • symlink를 사용할 수 있습니다.

결론

첫 번째 프로젝트에서 번들을 개발 및 테스트하고 두 번째 프로젝트에서 github 및 composer와 함께 사용할 수 있습니다.


알아야 할 중요한 점은 / vendor에서 저장소에 커밋 할 수 있다는 것입니다. 실제로 composer는 작업 컨텍스트에서 작업 할 수 있도록 패키지의 리포지토리를 참조하는 각 번들 (또는 패키지)에 대해 "composer"라는 두 번째 원격을 만듭니다. 따라서 모든 프로젝트에 대해 composer.json에 패키지를 등록하고 모든 프로젝트에서에서 커밋 /vendor/MyCompany/MyBundle하는 것이 좋습니다.

As a proof, just run git remote -v from any bundle in your vendor.

The bad practice would be to consider your bundle as a separate project and have symlinks with it. The main reason why this is the bad practice is that you won't be able to declare dependancies with your bundle. Moreover you'll have some difficulties with the deployment of your projects.


In Symfony4, generate:bundle command is no longer available. Instead, you may follow this tutorial.

First create a project with:

composer create-project symfony/website-skeleton my-project

Then, create a my-project/lib/AcmeFooBundle/src directory. Here will live your bundle. The namespace for this directory will be Acme\AcmeFooBundle, so if you create a service class at lib/AcmeFooBundle/src/Service/Foo.php, its namespace would be Acme\AcmeFooBundle\Service.

Now we need to tell composer autoloader to look for new classes at that new directory, so we need to edit composer.json autoload section:

"autoload": {
    "psr-4": {
        "Acme\\AcmeFooBundle\\": "lib/AcmeFooBundle/src/",
    }
}, 

and run composer dump-autoload.

Now you only need to add your bundle class to config/bundles.php:

return [
    ...
    Acme\AcmeFooBundle\AcmeFooBundle::class => ['all' => true],
];

and dependency injection to load configuration from your bundle.

If you want to check your services before adding dependency injection, you can just autowire them at config/services.yml:

services:
    ...
    Acme\AcmeFooBundle\Services\Foo: ~

That's all. Follow best practices and go on coding.

PS: I've published a post with a few tips for developing Symfony reusable bundles.

참고URL : https://stackoverflow.com/questions/21523481/symfony2-creating-own-vendor-bundle-project-and-git-strategy

반응형