Get Symfony configs from file without using Symfony

This config file is from a Symfony 6.4 application:

<?php

namespace SymfonyComponentDependencyInjectionLoaderConfigurator;

return static function (ContainerConfigurator $containerConfig): void 
{
    $scrapePatterns = [
        'default' => [
            'regex' => '/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+)/i',
            'first_only' => false,
            'cancel' => false,
        ],
    ];

    $templates = [
        'header' => 'Some header',
        'footer' => 'Some footer',
    ];

    $containerConfig->parameters()->set(
        'my.sample_configs',
        [
            'scrape_patterns' => $scrapePatterns,
            'templates' => $templates,
        ]
    );
};

How can I read those definition arrays from a different PHP app that does not use Symfony at all?

I don’t have the ContainerConfigurator class… I was wondering if I can create my own class with that name and use it to get the values.

A few notes about my (admittedly hacky) question:

  • I’m not looking for answers explaining better ways to put the problem, although I fully understand they exist; I am more interested in the actual hacky challenge as described here.
  • I can make changes to the Symfony config file as long as the real Symfony app is still able to load it correctly.
  • one of the things that is confusing me is that, due to my technical limitations, I don’t understand how Symfony uses that return static function, so it’s hard for me to know how to invoke this, or even the limits of what can be done with this code. I think it’s done here, though I am not certain.

3

After understanding what Symfony was doing to run the function, I am able to answer my own question. Not as hard as I thought.

I change the code like this:

<?php

namespace SymfonyComponentDependencyInjectionLoaderConfigurator;

return static function (ContainerConfigurator $containerConfig = null): array {
    $scrapePatterns = [
        'default' => [
            'regex' => '/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+)/i',
            'first_only' => false,
            'cancel' => false,
        ],
    ];

    $templates = [
        'header' => 'Some header',
        'footer' => 'Some footer',
    ];

    $configs = [
        'scrape_patterns' => $scrapePatterns,
        'templates' => $templates,
    ];

    if ($containerConfig) {
        $containerConfig->parameters()->set('my.sample_configs', $configs);
    }

    return $configs;
};

The changes are:

  • allow the function argument to be missing (null)
  • return an array instead of void
  • check if $containerConfig exists before using it (it exists when called from Symfony, doesn’t exist when called from elsewhere)
  • return the arrays I want (Symfony doesn’t seem to have any problems with this)

Then my code on the other app only needs this:


   $fn = include 'path/to/the/symfony/configs/processor_configs.php';

   if ($fn)
      $ret = $fn();

include, just like require, doesn’t just execute the code, it evaluates the code. This means that the code results in a value you can store in a variable. In this case, that return static function makes the included code return a function (a closure), which can then be invoked.

This way I can get the arrays on my app, and Symfony is also able to successfully load that same configuration file.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật