Where do private, custom bundles live in Symfony 4?

早过忘川 提交于 2019-12-05 12:06:40

Update 30-Jan-2017:

Okay. As far as I can tell, Symfony 4 is, effectively, private bundle hostile.

Additional work just kept turning up more and more problems (like getting unit testing to work for a private bundle).

I am currently pursuing other options that won't result in too much additional daily work.

Please ignore my original answer below.

--

My original answer:

After some more digging I realized that the classes in my custom bundle directory tree were not being picked up by composer during dump-autoload.

I think this is because Symfony 4 is not expecting any bundles except in vendor/.

The solution was to add my library directory to composer.json.

So My project tree now contains a directory for my private, custom bundles.

<projectName>/
    assets/
    ...
    DSL/
        DSLLibraryBundle/
        DSLTelnetBundle/
        ...
    public/
    src/
    ...

My composer.json autoload.psr-4 entry now looks like this:

"autoload": {
    "psr-4": {
        "App\\": "src/",
        "DSL\\": "DSL/"
    }
},

Update April 12, 2019: In the end, I took a completely different approach than my initial attempts.
In a nutshell, I now use composer to include my custom bundles.

My custom bundles live in their own directory tree.
Each bundle must have a valid composer.json file defining the bundle. For example:

{
  "name": "dsl/base-bundle",
  "description": "Base bundle required by all other DSL bundles",
  "type": "symfony-bundle",
  "version": "2.1.0",
  "license": "proprietary",
  "authors": [{"name": "David M. Patterson", "email": "dpatterson@example.com"}],
  "minimum-stability": "stable",
  "require": {
  },
  "require-dev": {
  },
  "autoload": {
    "psr-4":  {
      "Dsl\\BaseBundle\\": "src/"
    }
  }
}

Then define a custom repository in the project's composer.json file:

"repositories":[
  {
    "type": "path",
    "url":  "/full/path/to/DslBaseBundle"
  },
 ], ...

Then do a composer require dsl/base-bundle
Composer will create a symlink in vendor/ to the bundle and everything works as expected from there on.

My personal library is a regular Symfony project with a lib sub-directory that contains my bundles, each in its own sub-directory below lib/.

The Symfony application provides me with a convenient test bed. Note that the custom bundles must be included in it the same as for any other Symfony project.

@Stnaire, hope that helps.

Symfony4 no longer uses bundles inside the src/ . Bundles are only used within vendor/ as dependencies.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!