Skip to content

File loaders can now set the type to load explicitly #7353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
File loadres can now set the type to load explicitly
  • Loading branch information
javiereguiluz committed Jan 11, 2017
commit abea9963962dda19722a976b4e995ffefdde9ac0
16 changes: 12 additions & 4 deletions components/config/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ the resource::
$loaderResolver = new LoaderResolver(array(new YamlUserLoader($locator)));
$delegatingLoader = new DelegatingLoader($loaderResolver);

$delegatingLoader->load(__DIR__.'/users.yml');
/*
The YamlUserLoader will be used to load this resource,
// YamlUserLoader is used to load this resource because it supports
// files with the '.yml' extension
since it supports files with a "yml" extension
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed, right?

*/
$delegatingLoader->load(__DIR__.'/users.yml');

.. tip::

If your application uses unconventional file extensions (for example, your
YAML files have a ``.res`` extension) you can pass the file type as the
Copy link
Contributor

@ogizanagi ogizanagi Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it depends on the loader implementation. The one described in this part of the documentation (YamlUserLoader code above) totally ignores the type argument, as was done before by the DIC loaders 😅 (I think it should not appear at all in the Config component doc)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there plans to add this feature to these loaders too ... or it doesn't make sense? Thanks!

Copy link
Contributor

@ogizanagi ogizanagi Jan 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other loaders implementing LoaderInterface from the Config component are the routing ones.
They handle the $type argument quite differently:

# Symfony\Component\Routing\Loader\YamlFileLoader
/**
 * {@inheritdoc}
 */
public function supports($resource, $type = null)
{
    return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
}

The type cannot be forced, but should be empty or match the extension. Maybe it can be updated to match the new behavior introduced in the di loaders.

But I still think this note should not appear in the Config component documentation, as it should described the LoaderInterface and what it is used for. It should not document implementations from other components.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks for the explanation.

second optional parameter of the ``load()`` method::

// ...
$delegatingLoader->load(__DIR__.'/users.res', 'yml');
9 changes: 9 additions & 0 deletions components/dependency_injection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ Loading a YAML config file::
If you want to load YAML config files then you will also need to install
:doc:`the Yaml component </components/yaml>`.

.. tip::

If your application uses unconventional file extensions (for example, your
XML files have a ``.config`` extension) you can pass the file type as the
second optional parameter of the ``load()`` method::

// ...
$loader->load('services.config', 'xml');

If you *do* want to use PHP to create the services then you can move this
into a separate config file and load it in a similar way::

Expand Down