有什么方法可以在带有 NelmioApiDocBundle 的 symfony 3 中使用 nelmio_api_doc 的多个 yml 文件?
Is there any way to use multiple yml file with nelmio_api_doc in symfony 3 with NelmioApiDocBundle?
NelmioApiDocBundle 只允许单个配置文件作为 .yml 作为。
nelmio_api_doc:
routes:
path_patterns: # an array of regexps
- ^/api
documentation:
paths:
/api/login_check:
...
/api/refresh_token:
...
但我有超过 200 个 URL 可供使用,并且全部用于不同的捆绑包。
它会正常工作但很难在同一个文件中处理所有内容。
所以如果有人有解决方案将 "paths" 分成不同的单独文件。
我在 configuration reference of NelmioApiDocBundle
中看不到配置选项 "routes" 和 "documentation"
您应该改用注释,因为 described in the docs。
问题解决了! :-)
我的一个朋友有解决这个问题的好主意。实际上,这不是正确的解决方案,但我这是解决此问题的唯一方法。
我们创造"api_index.yaml"
export_path: '/config/packages/api_doc.yaml'
import_paths:
- "@DomCoreBundle/Resources/config/api_doc/api_base_doc.yaml"
- "@DomCmsBundle/Resources/config/api_doc/static_page_path_doc.yaml"
- "@DomEntityBundle/Resources/config/api_doc/category_path_doc.yaml"
- "@DomCmsBundle/Resources/config/api_doc/carousel_path_doc.yaml"
- "@DomQuickLinkBundle/Resources/config/api_doc/quick_link_path_doc.yaml"
- "@DomUserBundle/Resources/config/api_doc/user_path_doc.yaml"
- "@DomUserBundle/Resources/config/api_doc/dealer_path_doc.yaml"
...
然后我们创建一个 Symfony 命令(脚本),它读取每个 "import_paths" 文件并将内容附加到 "export_path" 文件中。
$this->io = new SymfonyStyle($input, $output);
$path = $this->kernel->locateResource('@FaCoreBundle/Resources/config/api_index.yaml');
$paths = Yaml::parse(file_get_contents($path));
if (array_key_exists('import_paths', $paths)) {
$contentLength = $this->loadFilesByPath($input, $output, $paths);
if ($input->getOption('watch')) {
$contentLengthNew = [];
while (true) {
foreach ($paths['import_paths'] as $path) {
$ymlPath = $this->kernel->locateResource($path);
$contentLengthNew[$ymlPath] = md5((file_get_contents($ymlPath)));
}
if (!empty(array_diff($contentLength, $contentLengthNew)) || count($contentLength) != count($contentLengthNew)) {
$diff = array_diff($contentLengthNew, $contentLength);
if (!empty($diff)) {
$this->io->writeln(sprintf('<comment>%s</comment> <info>[file+]</info> %s', date('H:i:s'), current(array_keys($diff))));
}
$contentLength = $this->loadFilesByPath($input, $output, $paths);
}
sleep($input->getOption('period'));
}
}
}
一个简单的方法是从非自动加载区域读取 yaml 文件,合并内容并写入一个将被加载的文件。
顺丰4.x
/config
/NELMIOApiDocDefinitions // out of autowire
/one.yaml
/two.yaml
/packages
/_NELMIOApiDocDefinitions.yaml // file_put_content()
one.yaml
nelmio_api_doc:
areas:
path_patterns:
- /one
documentation:
tags:
- { name: One, description: '...' }
paths:
/onepath/:
...
Kernel.php
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$this->mergeNELMIOApiDocYamlDefinitions();
...
}
private function mergeNELMIOApiDocYamlDefinitions()
{
// dev, prod, ...
$kernelEnvironnement = $this->getEnvironment();
// No need to rebuild definitions in production
if ($kernelEnvironnement == 'prod')
return;
// Path to the configuration directory
$confDir = $this->getProjectDir() . '/config';
// Path to the documentations files to merge
$yamlDefinitionsDirectory = $confDir . '/NELMIOApiDocDefinitions';
// Get the files to merge (without dots directories)
$yamlDefinitionsFiles = array_diff(scandir($yamlDefinitionsDirectory), ['.', '..']);
// Read definitions files and merge key with theirs values
$mergedDefinitions = [];
foreach ($yamlDefinitionsFiles as $yamlDefinitionFile) {
$yamlFileContent = Yaml::parseFile($yamlDefinitionsDirectory . '/' . $yamlDefinitionFile);
$mergedDefinitions = array_merge_recursive($mergedDefinitions, $yamlFileContent);
}
// Build the YAML
$yaml = Yaml::dump($mergedDefinitions);
// Write the YAML into a single file to be loaded by Symfony
file_put_contents($confDir . '/packages/_NELMIOApiDocDefinitions.yaml', $yaml);
}
问题已解决
config/packages/nelmio.yaml
imports:
- {resource: '../nelmio/default.yaml'}
- {resource: '../nelmio/v1.yaml'}
config/nelmio/default.yaml
nelmio_api_doc:
areas:
default:
path_patterns: [ ^/default/ ]
documentation:
info:
title: Default
config/nelmio/v1.yaml
nelmio_api_doc:
areas:
default:
path_patterns: [ ^/v1/ ]
documentation:
info:
title: V1
NelmioApiDocBundle 只允许单个配置文件作为 .yml 作为。
nelmio_api_doc:
routes:
path_patterns: # an array of regexps
- ^/api
documentation:
paths:
/api/login_check:
...
/api/refresh_token:
...
但我有超过 200 个 URL 可供使用,并且全部用于不同的捆绑包。 它会正常工作但很难在同一个文件中处理所有内容。
所以如果有人有解决方案将 "paths" 分成不同的单独文件。
我在 configuration reference of NelmioApiDocBundle
中看不到配置选项 "routes" 和 "documentation"您应该改用注释,因为 described in the docs。
问题解决了! :-)
我的一个朋友有解决这个问题的好主意。实际上,这不是正确的解决方案,但我这是解决此问题的唯一方法。
我们创造"api_index.yaml"
export_path: '/config/packages/api_doc.yaml'
import_paths:
- "@DomCoreBundle/Resources/config/api_doc/api_base_doc.yaml"
- "@DomCmsBundle/Resources/config/api_doc/static_page_path_doc.yaml"
- "@DomEntityBundle/Resources/config/api_doc/category_path_doc.yaml"
- "@DomCmsBundle/Resources/config/api_doc/carousel_path_doc.yaml"
- "@DomQuickLinkBundle/Resources/config/api_doc/quick_link_path_doc.yaml"
- "@DomUserBundle/Resources/config/api_doc/user_path_doc.yaml"
- "@DomUserBundle/Resources/config/api_doc/dealer_path_doc.yaml"
...
然后我们创建一个 Symfony 命令(脚本),它读取每个 "import_paths" 文件并将内容附加到 "export_path" 文件中。
$this->io = new SymfonyStyle($input, $output);
$path = $this->kernel->locateResource('@FaCoreBundle/Resources/config/api_index.yaml');
$paths = Yaml::parse(file_get_contents($path));
if (array_key_exists('import_paths', $paths)) {
$contentLength = $this->loadFilesByPath($input, $output, $paths);
if ($input->getOption('watch')) {
$contentLengthNew = [];
while (true) {
foreach ($paths['import_paths'] as $path) {
$ymlPath = $this->kernel->locateResource($path);
$contentLengthNew[$ymlPath] = md5((file_get_contents($ymlPath)));
}
if (!empty(array_diff($contentLength, $contentLengthNew)) || count($contentLength) != count($contentLengthNew)) {
$diff = array_diff($contentLengthNew, $contentLength);
if (!empty($diff)) {
$this->io->writeln(sprintf('<comment>%s</comment> <info>[file+]</info> %s', date('H:i:s'), current(array_keys($diff))));
}
$contentLength = $this->loadFilesByPath($input, $output, $paths);
}
sleep($input->getOption('period'));
}
}
}
一个简单的方法是从非自动加载区域读取 yaml 文件,合并内容并写入一个将被加载的文件。
顺丰4.x
/config
/NELMIOApiDocDefinitions // out of autowire
/one.yaml
/two.yaml
/packages
/_NELMIOApiDocDefinitions.yaml // file_put_content()
one.yaml
nelmio_api_doc:
areas:
path_patterns:
- /one
documentation:
tags:
- { name: One, description: '...' }
paths:
/onepath/:
...
Kernel.php
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$this->mergeNELMIOApiDocYamlDefinitions();
...
}
private function mergeNELMIOApiDocYamlDefinitions()
{
// dev, prod, ...
$kernelEnvironnement = $this->getEnvironment();
// No need to rebuild definitions in production
if ($kernelEnvironnement == 'prod')
return;
// Path to the configuration directory
$confDir = $this->getProjectDir() . '/config';
// Path to the documentations files to merge
$yamlDefinitionsDirectory = $confDir . '/NELMIOApiDocDefinitions';
// Get the files to merge (without dots directories)
$yamlDefinitionsFiles = array_diff(scandir($yamlDefinitionsDirectory), ['.', '..']);
// Read definitions files and merge key with theirs values
$mergedDefinitions = [];
foreach ($yamlDefinitionsFiles as $yamlDefinitionFile) {
$yamlFileContent = Yaml::parseFile($yamlDefinitionsDirectory . '/' . $yamlDefinitionFile);
$mergedDefinitions = array_merge_recursive($mergedDefinitions, $yamlFileContent);
}
// Build the YAML
$yaml = Yaml::dump($mergedDefinitions);
// Write the YAML into a single file to be loaded by Symfony
file_put_contents($confDir . '/packages/_NELMIOApiDocDefinitions.yaml', $yaml);
}
问题已解决
config/packages/nelmio.yaml
imports:
- {resource: '../nelmio/default.yaml'}
- {resource: '../nelmio/v1.yaml'}
config/nelmio/default.yaml
nelmio_api_doc:
areas:
default:
path_patterns: [ ^/default/ ]
documentation:
info:
title: Default
config/nelmio/v1.yaml
nelmio_api_doc:
areas:
default:
path_patterns: [ ^/v1/ ]
documentation:
info:
title: V1