Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

added support for glob loaders in Config#21635

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromfabpot:glob-loader
Feb 18, 2017

Conversation

@fabpot
Copy link
Member

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsn/a
LicenseMIT
Doc PRnot yet

In#21270, we added the possibility to use glob patterns to import (not load) config files, but it was restricted to the container. The same feature could be useful (and I actually have a use case) for the routing.

So, this PR moves the logic to the Config component. It also adds a newGlobFileLoader class that allows to load glob patterns (not just import them as in#21270).

Last, but not least, the new glob file loader is registered in both the routing and the container default loaders.

Here is a simple, but powerful example, using the Symfony micro kernel (actually, this is a snippet from the Kernel used in Symfony Flex :)):

<?phpnamespaceSymfony\Flex;useSymfony\Component\HttpKernel\KernelasBaseKernel;class Kernelextends BaseKernel{use MicroKernelTrait;constCONFIG_EXTS ='.{php,xml,yaml,yml}';// ...protectedfunctionconfigureContainer(ContainerBuilder$container,LoaderInterface$loader)    {$confDir =dirname($this->getRootDir()).'/etc';$loader->import($confDir.'/packages/*'.self::CONFIG_EXTS,'glob');$loader->import($confDir.'/packages/'.$this->getEnvironment().'/**/*'.self::CONFIG_EXTS,'glob');$loader->import($confDir.'/container'.self::CONFIG_EXTS,'glob');    }protectedfunctionconfigureRoutes(RouteCollectionBuilder$routes)    {$confDir =dirname($this->getRootDir()).'/etc';$routes->import($confDir.'/routing/*'.self::CONFIG_EXTS,'/','glob');$routes->import($confDir.'/routing/'.$this->getEnvironment().'/**/*'.self::CONFIG_EXTS,'/','glob');$routes->import($confDir.'/routing'.self::CONFIG_EXTS,'/','glob');    }}

chalasr and ogizanagi reacted with thumbs up emoji
++$ct;
$ret[] =$this->doImport($resource,$type,$ignoreErrors,$sourceResource);
}

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

This is ugly asload andimport are not supposed to return anything, but the routing loaders do expect some return value (something that should probably be fixed at some point).

Copy link
Member

Choose a reason for hiding this comment

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

even if already ugly enough as is, do we really want this to require php 7+ for such syntactic sugar???

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

fixed

$classes =array();
$extRegexp =defined('HHVM_VERSION') ?'/\\.(?:php|hh)$/' :'/\\.php$/';
$prefixLen =null;
foreach ($this->glob($resource,true,$prefix)as$path =>$info) {

Choose a reason for hiding this comment

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

if you call glob outside of the loop, you can compute prefixLen outside of the foreach

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

that does not work

++$ct;
$ret[] =$this->doImport($resource,$type,$ignoreErrors,$sourceResource);
}

Choose a reason for hiding this comment

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

drop $ct then:
isset($ret[1]) ? $ret : ($ret ? $ret[0] : null) or
$ret ? (isset($ret[1]) ? $ret : $ret[0]) : null ?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

which would be less readable

protectedfunctionglob($resource,$recursive, &$prefix =null,$ignoreErrors =false)
{
if (strlen($resource) ===$i =strcspn($resource,'*?{[')) {
if (!$recursive) {

Choose a reason for hiding this comment

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

just in case, $prefix should be set to null here

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

done

*/
publicfunctionsupports($resource,$type =null)
{
return'glob' ===$type;

Choose a reason for hiding this comment

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

add a check on $resource, likefalse === strpbrk($resource, '*?{[')?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Not needed as we force$type to beglob

Choose a reason for hiding this comment

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

@nicolas-grekas could the check be done at some other place, see#22160
Actually just a check if a glob pattern is used but not the correct type (type: glob) is configured to return a meaningful errormessage?

*
* @throws FileLoaderLoadException
*/
publicfunctionimport($resource,$prefix ='/',$type =null)

Choose a reason for hiding this comment

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

in case of a glob, this is missing tracking of the directory prefixing the pattern - see "$this->container->fileExists($resourcePrefix, '/^$/');" in DI.
Should we do it inline here? Would be like a leak but do we have a better choice?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Not about the "leak", as routing resources are tracked in a different file, so we cannot reused the same logic.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

By the way, we have the same issue with the container.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I think there is nothing to fix here. Callingimport does not imply that you were using a glob. However, the previous implementation made this assumption and always added the direction as a resource, even for non-globs, I don't think this is something we want.

@fabpotfabpotforce-pushed theglob-loader branch 2 times, most recently froma183027 toce72d21CompareFebruary 16, 2017 21:53
@nicolas-grekasnicolas-grekas added this to the3.3 milestoneFeb 18, 2017
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍 after rebase

@fabpotfabpot merged commit025585d intosymfony:masterFeb 18, 2017
fabpot added a commit that referenced this pull requestFeb 18, 2017
This PR was merged into the 3.3-dev branch.Discussion----------added support for glob loaders in Config| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | n/a| License       | MIT| Doc PR        | not yetIn#21270, we added the possibility to use glob patterns to import (not load) config files, but it was restricted to the container. The same feature could be useful (and I actually have a use case) for the routing.So, this PR moves the logic to the Config component. It also adds a new `GlobFileLoader` class that allows to load glob patterns (not just import them as in#21270).Last, but not least, the new glob file loader is registered in both the routing and the container default loaders.Here is a simple, but powerful example, using the Symfony micro kernel (actually, this is a snippet from the Kernel used in Symfony Flex :)):```php<?phpnamespace Symfony\Flex;use Symfony\Component\HttpKernel\Kernel as BaseKernel;class Kernel extends BaseKernel{    use MicroKernelTrait;    const CONFIG_EXTS = '.{php,xml,yaml,yml}';    // ...    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)    {        $confDir = dirname($this->getRootDir()).'/etc';        $loader->import($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');        $loader->import($confDir.'/packages/'.$this->getEnvironment().'/**/*'.self::CONFIG_EXTS, 'glob');        $loader->import($confDir.'/container'.self::CONFIG_EXTS, 'glob');    }    protected function configureRoutes(RouteCollectionBuilder $routes)    {        $confDir = dirname($this->getRootDir()).'/etc';        $routes->import($confDir.'/routing/*'.self::CONFIG_EXTS, '/', 'glob');        $routes->import($confDir.'/routing/'.$this->getEnvironment().'/**/*'.self::CONFIG_EXTS, '/', 'glob');        $routes->import($confDir.'/routing'.self::CONFIG_EXTS, '/', 'glob');    }}```Commits-------025585d added support for glob loaders in Config
@fabpotfabpot deleted the glob-loader branchApril 7, 2017 17:17
@fabpotfabpot mentioned this pull requestMay 1, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@chalasrchalasrchalasr left review comments

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

+1 more reviewer

@luneticsluneticslunetics left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

5 participants

@fabpot@lunetics@nicolas-grekas@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp