melisplatform/melis-platform-framework-symfony

v5.0.1 2023-05-24 09:00 UTC

README

此包是Symfony的入口,以便连接到Melis平台,例如访问已注册的服务和自动使用平台的数据库连接。

入门指南

以下说明将在您的机器上安装并运行项目的副本。

先决条件

为了运行此模块,您需要安装以下内容

  • melisplatform/melis-platform-frameworks

当使用composer时,这会自动完成。

安装

运行composer命令

composer require melisplatform/melis-platform-framework-symfony

运行代码

激活模块

激活此包的方法与在symfony应用内部激活包相同。您只需要将其包类包含在symfony应用内部的包列表中(最可能在bundles.php文件中)。

return [
    //All of the symfony activated bundles here
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    ...
    ...
    etc.
    //Melis Platform Custom Bundles
    MelisPlatformFrameworkSymfony\MelisPlatformFrameworkSymfonyBundle::class => ['all' => true]
];

访问Melis平台服务

MelisServiceManager类
  • 此类是Symfony连接到Melis平台的入口。因此,通过使用此类,我们可以获取Melis平台的所有已注册服务。
  • 您可以在symfony应用中通过调用其已注册服务键melis_platform.service_manager或使用依赖注入来调用此类(请参见以下示例)。

示例

//Using Dependency Injection

//Assuming we are inside a controller
protected $melisServiceManager;

//Inject MelisServiceManager in the controller constructor function
public function __construct(MelisServiceManager $melisServiceManager)
{
    $this->melisServiceManager = $melisServiceManager;
}
//And then we can use the melis provider like this
//to get the language list of the Back Office
$melisCoreTableLang = $this->melisServiceManager->getService('MelisCoreTableLang');
$melisCorelangList = $melisCoreTableLang->fetchAll()->toArray();

//Using service key (melis_platform.service_manager)

//Assuming we are inside of any custom Symfony controller that extends AbstractController of Symfony
//Calling the service
$melisServices = $this->get('melis_platform.service_manager');
//Calling the MelisCoreTableLang service registered in Melis Platform
$languageTable = $melisServices->getService('MelisCoreTableLang');
//Calling fetchAll function inside MelisCoreTableLang service and convert the result to array
$languageList = $languageTable->fetchAll()->toArray();
  • 位于symfony骨架内部的包可能会在扩展AbstractController的Controller中访问melis_platform.service_manager服务键时出现问题,因为AbstractController只使用一个包含一些服务的有限容器。

    但我们可以通过在我们的Controller中重写AbstractController的getSubscribedServices函数来注册我们的服务,仍然使用melis_platform.service_manager服务键。

public static function getSubscribedServices()
{
    return array_merge(parent::getSubscribedServices(),
        [
            'melis_platform.service_manager' => MelisServiceManager::class,
        ]);
}

事件监听器

DatabaseSwitcherListener
  • 此监听器将强制Symfony使用Melis平台数据库。
SymfonyTranslationsListener
  • 此监听器将获取所有Symfony翻译并将其存储在文件中(Resources/translations/melis/symfony-translations.phtml),以便Melis平台可以使用这些翻译。此文件必须可写。

作者

还可以查看参与此项目的贡献者列表

许可

此项目根据OSL-3.0许可证授权 - 请参阅LICENSE