最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to get correct composer vendor-dir? - Stack Overflow

programmeradmin2浏览0评论

I'm creating a symfony bundle that integrate another library into Symfony, by tagging some classes and loading the library translations.
This bundle is a composer package that requires another package (axi/mycalendar).

It works but I'm aware that /vendor dir shouldn't be hardcoded as it can be changed by composer property vendor-dir.

How can I can ensure / find the proper vendor dir ?
I was thinking about parsing the main composer.json file but it feels dirty.

PS: axi/mycalendar is psr-4 autoloaded by composer

<?php

namespace Axi\MyBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class MyBundle extends AbstractBundle
{
    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        // Declare vendor recipes to Sf
        $container->services()->load(
            'Axi\\MyCalendar\\Recipe\\',
            '%kernel.project_dir%/vendor/axi/mycalendar/src/Recipe/*'
        )->autoconfigure();

        // more code ...
    }

    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        // Add library translation path to translator pathes
        $builder->prependExtensionConfig(
            'framework',
            [
                'translator' => [
                    'paths' => [
                        '%kernel.project_dir%/vendor/axi/mycalendar/translations',
                    ],
                ],
            ]
        );
    }
}

I'm creating a symfony bundle that integrate another library into Symfony, by tagging some classes and loading the library translations.
This bundle is a composer package that requires another package (axi/mycalendar).

It works but I'm aware that /vendor dir shouldn't be hardcoded as it can be changed by composer property vendor-dir.

How can I can ensure / find the proper vendor dir ?
I was thinking about parsing the main composer.json file but it feels dirty.

PS: axi/mycalendar is psr-4 autoloaded by composer

<?php

namespace Axi\MyBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class MyBundle extends AbstractBundle
{
    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        // Declare vendor recipes to Sf
        $container->services()->load(
            'Axi\\MyCalendar\\Recipe\\',
            '%kernel.project_dir%/vendor/axi/mycalendar/src/Recipe/*'
        )->autoconfigure();

        // more code ...
    }

    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        // Add library translation path to translator pathes
        $builder->prependExtensionConfig(
            'framework',
            [
                'translator' => [
                    'paths' => [
                        '%kernel.project_dir%/vendor/axi/mycalendar/translations',
                    ],
                ],
            ]
        );
    }
}
Share Improve this question edited Feb 8 at 19:10 DarkBee 15.6k8 gold badges70 silver badges115 bronze badges asked Feb 7 at 17:40 AxiAxi 1,79516 silver badges22 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Another simple alternative: as you know the dir structure of your own bundle, use dirname($this->getPath()) in any of the bundle methods and then concat it. For example:

dirname($this->getPath()).'/axi/mycalendar/';

Composer in version ^2.1 (June 2021) offers a way: ¹

use Composer\InstalledVersions;

InstalledVersions::getInstallPath('axi/mycalendar');

This seems cleaner than using a hard-encoded /vendor or parsing the composer.json file.


¹ Compare Knowing the path in which a package is installed (getcomposer.org).

发布评论

评论列表(0)

  1. 暂无评论