2016-09-17 68 views
0

在我的Symfony 3應用程序中,我使用的是從其他api獲取數據的服務。Guzzle和Symfony 3:未解決的基礎URL斷開作曲家安裝

在這個服務中,我注入了「session」和一個字符串,代表了我的rest api的基礎url。

public function __construct(Session $session, $magentoRestUrl) 
{ 
    $this->session = $session; 
    $this->client = new \GuzzleHttp\Client([ 
     'base_uri' => $magentoRestUrl, // http://domain.com/rest/V1/ 
     'timeout' => 2.0, 
    ]); 

    $this->cache = new ApcuAdapter(); 

    $this->groupedIngredients = []; 
} 

到目前爲止,所有事情在我的本地機器上按預期工作。但現在,在與$magentoRestUrl部署過程不存在,我得到運行composer install --no-dev當這個錯誤:

... 
Writing lock file 
Generating autoload files 

[GuzzleHttp\Exception\ConnectException]                    
cURL error 6: Could not resolve host: domain.com (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) 

Build step 'Execute shell' marked build as failure 
Finished: FAILURE 

我的問題是:爲什麼composer install觸發狂飲來解決$magentoRestUrl?是嗎,因爲它在服務類的構造函數中?

這裏是我的composer.json:

{ 
"name": "alex/symfony", 
"license": "proprietary", 
"type": "project", 
"autoload": { 
    "psr-4": { 
     "": "src/" 
    }, 
    "classmap": [ 
     "app/AppKernel.php", 
     "app/AppCache.php" 
    ] 
}, 
"autoload-dev": { 
    "psr-4": { 
     "Tests\\": "tests/" 
    } 
}, 
"require": { 
    "php": ">=5.5.9", 
    "symfony/symfony": "3.1.*", 
    "doctrine/orm": "^2.5", 
    "doctrine/doctrine-bundle": "^1.6", 
    "doctrine/doctrine-cache-bundle": "^1.2", 
    "symfony/swiftmailer-bundle": "^2.3", 
    "symfony/monolog-bundle": "^2.8", 
    "sensio/distribution-bundle": "^5.0", 
    "sensio/framework-extra-bundle": "^3.0.2", 
    "incenteev/composer-parameter-handler": "^2.0", 
    "symfony/assetic-bundle": "^2.8", 
    "leafo/scssphp": "^0.6.3", 
    "guzzlehttp/guzzle": "^6.2" 
}, 
"require-dev": { 
    "sensio/generator-bundle": "^3.0", 
    "symfony/phpunit-bridge": "^3.0" 
}, 
"scripts": { 
    "post-install-cmd": [ 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget" 
    ], 
    "post-update-cmd": [ 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget" 
    ] 
}, 
"extra": { 
    "symfony-app-dir": "app", 
    "symfony-bin-dir": "bin", 
    "symfony-var-dir": "var", 
    "symfony-web-dir": "web", 
    "symfony-tests-dir": "tests", 
    "symfony-assets-install": "relative" 
} 

回答

0

Symfony框架採用post-install-cmdpost-update-cmd腳本從composer.json由作曲家自動調用初始化緩存,打造bootstrap.php.cache等。

我認爲其中一個內部必須與依賴注入器一起工作,其中一個依賴關係還需要解析引發此錯誤的服務(這取決於您的服務配置)。

+0

似乎無法做到這一點,因爲在部署過程中,服務構造函數中的GuzzleClient初始化中設置的base_url永遠不可解析。我如何創建服務有什麼問題嗎? – alexbartsch

+0

也許你可以使依賴項可選https://symfony.com/doc/current/service_container/optional_dependencies.html在DI配置中寫一個表達式,其中包含一些檢查當前環境的邏輯https://symfony.com/doc/current/ service_container/expression_language.html – martin