在platform.sh

时间:2017-01-11 14:20:49

标签: php symfony redis composer-php cloud-hosting

我试图让我的应用程序由platform.sh托管,我的问题是我使用SncRedisBundle,使用此配置:

#config.yml

imports:
    - { resource: platformSh/parameters_platform.php } #platform.sh cloud provider configuration's

snc_redis:
    clients:
        doctrine:
            type: predis
            alias: doctrine
            dsn: "%redis_url%&database=4"
    doctrine:
        metadata_cache:
            client: doctrine
            entity_manager: default          # the name of your entity_manager connection
            document_manager: default        # the name of your document_manager connection
        result_cache:
            client: doctrine
            entity_manager: [default]  # you may specify multiple entity_managers
        query_cache:
            client: doctrine
            entity_manager: default
        second_level_cache:
            client: doctrine
            entity_manager: default
#platformSh/parameters_platform.php
$relationships = getenv("PLATFORM_RELATIONSHIPS");

var_dump('$relationships : ', $relationships, @$_ENV['PLATFORM_RELATIONSHIPS']);

if (!$relationships) {
    return;
}

$relationships = json_decode(base64_decode($relationships), true);

foreach ($relationships['database'] as $endpoint) {
    if (empty($endpoint['query']['is_master'])) {
        continue;
    }

    $container->setParameter('database_driver', 'pdo_' . $endpoint['scheme']);
    $container->setParameter('database_host', $endpoint['host']);
    $container->setParameter('database_port', $endpoint['port']);
    $container->setParameter('database_name', $endpoint['path']);
    $container->setParameter('database_user', $endpoint['username']);
    $container->setParameter('database_password', $endpoint['password']);
    $container->setParameter('database_path', '');
}

if (!empty($relationships['redis'])) {
    $redisConstructedDsn = 'redis://'.$relationships['redis'][0]['host'].$relationships['redis'][0]['port'].'?password=';
    $container->setParameter('redis_url', $redisConstructedDsn);

    var_dump('$redisConstructedDsn : ', $redisConstructedDsn);
} else {
    var_dump('$relationships :', $relationships);
}


# Store session into /tmp.
ini_set('session.save_path', '/tmp/sessions');

当我推送到platform.sh时,他们的构建过程包括对作曲家更新的调用,结束如下:

  

生成优化的自动加载文件

     
    

Incenteev \ ParameterHandler \ ScriptHandler :: buildParameters     SENSIO \包\ DistributionBundle \作曲\ ScriptHandler :: buildBootstrap     SENSIO \包\ DistributionBundle \作曲\ ScriptHandler :: clearCache               脚本Sensio \ Bundle \ DistributionBundle \ Composer \ ScriptHandler :: clearCache     处理以异常终止的symfony-scripts事件

  
     

[RuntimeException的]
  执行"'缓存时发生错误:clear --no-warmup'"命令:
  string(17)" $ relations:"
  布尔(假)
  NULL

     

[Predis \连接\ ConnectionException]
  php_network_getaddresses:getaddrinfo failed:名称或服务未知[tcp:// localhost& database = 4:6379]

        .
     

我联系了他们的支持团队,他告诉我:

  

您好,

     

我是否知道你是否在尝试获得PLATFORM_RELATIONSHIPS   建立阶段?

     

由于构建与环境无关,因此无法使用。

     

如果您需要连接DB / Redis /其他服务,请执行此操作   部署阶段(即部署挂钩)。

编辑:支持已经回答我问题是这个composer.json部分:

"scripts": { 
  "symfony-scripts": [ 
    "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters", 
    "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-install-cmd": [ 
  "@symfony-scripts" 
], 
"post-update-cmd": [ 
  "@symfony-scripts" 
], 
"compile": [ 
  "app/console assetic:dump" 
] 
},

例如,"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache"以及"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets"正在导致symfony清除缓存,因为它太快了(在build.sh不提供环境的构建阶段)。对链接的redis和mysql来说是必不可少的......)

我不知道如何解决这个问题,platform.sh希望我改变composer.json文件,但我发现它已经过了危险的。你会怎么做?

1 个答案:

答案 0 :(得分:2)

在你的platform.app.yaml中,设置

flavor: 'none'

并使用假值创建一个parameters.dist.yml(在此文件中添加redis_url:xxx)。这些假值将在您的parameters_platform.php

中被覆盖

这是我的platform.app.yaml:

的钩子的代码
hooks:
# Build script; can modify the filesystem, but cannot access services
build: |
    set -x -e

    # remove the development front-controller if present
    (>&2 rm web/app_dev.php || true)

    SYMFONY_ENV=prod composer install --prefer-dist --optimize-autoloader --classmap-authoritative --no-progress --no-ansi --no-interaction --no-dev
    (>&2 SYMFONY_ENV=prod app/console cache:clear --no-warmup)
    (>&2 SYMFONY_ENV=prod app/console cache:warmup)
    # Keep the cache in a persistant directory
    # If your cache can be readonly, you can skip this step
    (>&2 mkdir -p tmp/cache && mv app/cache/prod tmp/cache/ && mv app/bootstrap.php.cache tmp/bootstrap.php.cache)
    yarn
    gulp

# Deploy script, can access services, but the filesystem is read-only
deploy: |
    set -x -e

    # "install" cache
    # If your cache can be readonly, you can skip these steps
    rm -rf app/cache/prod
    (>&2 SYMFONY_ENV=prod app/console --env=prod cache:clear)
    (>&2 SYMFONY_ENV=prod app/console --env=prod doctrine:schema:update --dump-sql --force)
    cp -Rp tmp/bootstrap.php.cache app/bootstrap.php.cache
    cp -Rp tmp/cache/prod app/cache/

我们项目的platform.php:

if (empty($relationships['redis'])) {
    return;
}

$redis = array_shift($relationships['redis']);

$container->setParameter('redis_host', $redis['host']);
$container->setParameter('redis_port', $redis['port']);