Symfony 4中的自定义YAML配置文件

时间:2019-02-18 09:32:58

标签: php symfony

我正在尝试在config/packages之外创建自定义配置文件,因为我需要此文件可写。我了解Symfony 4并未像以前的版本那样定义AppBundle。我在解决方案中也遇到了类似的问题,这些问题不适用于我的特定情况。

到目前为止,我已经创建了以下文件:

# config/settings.yaml

settings:
    foo: 3
<?php

namespace App\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $tree_builder = new TreeBuilder();
        $root_node = $tree_builder->root('settings');

        $root_node
            ->addDefaultsIfNotSet()
            ->children()
                ->scalarNode('foo')
                    ->defaultValue(1)
                ->end()
            ->end()
        ;

        return $tree_builder;
    }
}
<?php

namespace App\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class SettingsExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
        $loader->load('settings.yaml');

        $container->setParameter('settings.foo', $config['foo']);
    }
}

我已将扩展名加载到Kernel.php中:


    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
    {
        $container->registerExtension(new SettingsExtension());
        $container->loadFromExtension('settings');

        ...
    }

但是如果出现以下错误:

There is no extension able to load the configuration for "settings" (in src/DependencyInjection/../../config/settings.yaml). Looked for namespace "settings", found none

1 个答案:

答案 0 :(得分:0)

您错误地解决了问题。

  

因为我需要此文件可写

如果在(部署?)过程中有人想要在生成缓存之前修改配置,则权限问题是OPS问题。当然,您找不到通过使用正确的权限系统将config / services.yml设置为可写的方法。
参数文件已替换为import org.apache.spark.SparkContext import org.apache.spark.sql._ import org.apache.spark.sql.types.{StringType, StructField, StructType} object DataFrameTest2 extends Serializable { var sparkSession: SparkSession = _ var sparkContext: SparkContext = _ var sqlContext: SQLContext = _ def main(args: Array[String]): Unit = { sparkSession = SparkSession.builder().appName("TestMaster").master("local").getOrCreate() sparkContext = sparkSession.sparkContext val sqlContext = new org.apache.spark.sql.SQLContext(sparkContext) val map1 = Map("EMP_NAME" -> "Ahmad", "DOB" -> "01-10-1991", "CITY" -> "Dubai") val map2 = Map("EMP_NAME" -> "Rahul", "DOB" -> "06-12-1991", "CITY" -> "Mumbai") val map3 = Map("EMP_NAME" -> "John", "DOB" -> "11-04-1996", "CITY" -> "Toronto") val list = List(map1, map2, map3) //create your rows val rows = list.map(m => Row(m.values.toSeq:_*)) //create the schema from the header val header = list.head.keys.toList val schema = StructType(header.map(fieldName => StructField(fieldName, StringType, true))) //create your rdd val rdd = sparkContext.parallelize(rows) //create your dataframe using rdd val df = sparkSession.createDataFrame(rdd, schema) df.show() } } 文件和环境变量,您可以编写它们吗?

如果要放置更改全局参数,那么通过数据库将更加容易(请查看CraueConfigBundle)。
Yaml文件放入缓存中。这意味着动态编写yaml将在prod中失败。