8000 [Translation][Extractor] Allow extracting an array of files besides extracting a directory by marcosdsanchez · Pull Request #14002 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation][Extractor] Allow extracting an array of files besides extracting a directory #14002

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

Conversation

marcosdsanchez
Copy link
Contributor
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR N/A

Give ability to extractors to extract from an array of files a file, or a Traversable besides a directory.

The following commit adds the new feature to the following extractors:
PhpExtractor and TwigExtractor.
It also corrects the interface documentation to show the new options allowed as a parameter.

I found this change useful as I've got a custom translation:update command that instead of extracting from $rootPath. '/Resources/views/' it extracts from the files that git is showing as modified. The command usually runs much faster than the default as it only parses the needed files.

$finder = new Finder();
$files = $finder->files()->name('*.twig')->sortByName()->in($directory);
if (is_array($resource)) {
$files = array_filter($resource, function ($filename) {
Copy link
Contributor

Choose a reason for hiding this comment

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

$files = array();
foreach ($resource as $file) {
    if (!is_file($file)) {
        throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
    }

    if ('twig' === pathinfo($filename, PATHINFO_EXTENSION)) {
        throw new \InvalidArgumentException('the given file type is not supported');
    }

    $files[] = new \SplFileInfo($file);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some concerns about your comment:

  • Isn't the is_file check too expensive if the array contains a lot of files?
  • The second if statement is making this behave differently than before. Files with another extensions were ignored, no exception was thrown. I'd prefer to keep the old behaviour.
  • Are you creating SplFileInfo instances only to keep the code from below the same?

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Anyway we need to be sure that file exist, the Finder is expensive too
  2. ok
  3. Yes, it keep the code from below the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comments addressed. Also added more tests and different options for $resource like support for Traversable, a file, etc. Updated interface documentation too.

@marcosdsanchez marcosdsanchez force-pushed the translation-extractors branch 3 times, most recently from 59790c3 to 4b5abd8 Compare March 21, 2015 21:58
@aitboudad
Copy link
Contributor

@marcosdsanchez what do you think about adding a new class that give ability to extract from one file (FileTwigExtractor for example) ? /cc @fabpot

@marcosdsanchez
Copy link
Contributor Author

@aitboudad , what about something like 1b419cb ?

@aitboudad
Copy link
Contributor

@marcosdsanchez It's exactly what I want :D

/**
* @var string
*/
private static $extension = 'twig';
Copy link
Contributor

Choose a reason for hiding this comment

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

we can avoid static here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should it be a constant instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

@marcosdsanchez marcosdsanchez force-pushed the translation-extractors branch from 0695b03 to 7185752 Compare March 23, 2015 17:55
@aitboudad
Copy link
Contributor

👍

@@ -20,7 +21,7 @@
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class PhpExtractor implements ExtractorInterface
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to let AbstractFileExtractor implement ExtractorInterface instead? Or can we think about a use case where this is not necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about this too but decided to go this way as the abstract class doesn't implement any of the methods from the interface.

@marcosdsanchez
Copy link
Contributor Author

@aitboudad Because of the new class the code is not backwards compatible anymore. See https://travis-ci.org/symfony/symfony/builds/55525434 . What should I do? Get back to the version I had before or point the PR to another branch?

@aitboudad
Copy link
Contributor

just update composer.json to 2.7

@xabbuh
Copy link
Member
xabbuh commented Mar 23, 2015

@marcosdsanchez You will have to bump the version in the FrameworkBundle too.

@marcosdsanchez
Copy link
Contributor Author

@aitboudad @xabbuh updated Twig Bridge and Framework Bundle. Travis is still failing with high dependencies in php 5.6 https://travis-ci.org/symfony/symfony/builds/55551337 . What am I missing?

@xabbuh
Copy link
Member
xabbuh commented Mar 24, 2015

@marcosdsanchez This seems to be related to how that specific build works (you change is not available in the components repos as it was not merged). So that's nothing to worry about.

@marcosdsanchez
Copy link
Contributor Author

Thanks @xabbuh . I think this is ready then.

@xabbuh
Copy link
Member
xabbuh commented Mar 26, 2015

Can you please add an entry to the changelog?

@marcosdsanchez
Copy link
Contributor Author

@xabbuh sure. For which version 2.3.27 (unreleased) ?

@fabpot
Copy link
Member
fabpot commented Mar 26, 2015

@marcosdsanchez You need to add an entry in the CHANGELOG of the component, not the CHANGLOG file from the root directory (those are automatically generated.)

@xabbuh
Copy link
Member
xabbuh commented Mar 26, 2015

@marcosdsanchez As @fabpot said, and the version should be 2.7.0.

@marcosdsanchez
Copy link
Contributor Author

Thanks @fabpot and @xabbuh . Done in 5258f01.

/**
* @var string
*/
private $extension = 'twig';
Copy link
Member

Choose a reason for hiding this comment

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

I would remove this as there is no way to change it and no need to change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was already discussed here. I'd prefer to have a class constant, but ok, I'll remove...

@fabpot
Copy link
Member
fabpot commented Mar 26, 2015

👍 when my 2 comments are fixed

…a directory.

The following commit adds the new feature to the following extractors: PhpExtractor and TwigExtractor.
It also corrects the interface documentation to show that an array of files is also allowed as a parameter.
A new base class called AbstractFileExtractor was created. It includes common code used by FileExtractors.
@marcosdsanchez marcosdsanchez force-pushed the translation-extractors branch from 5258f01 to 4c5adbc Compare March 26, 2015 17:39
@marcosdsanchez
Copy link
Contributor Author

@fabpot comments addressed.

@fabpot
Copy link
Member
fabpot commented Mar 27, 2015

Thank you @marcosdsanchez.

@fabpot fabpot closed this Mar 27, 2015
fabpot added a commit that referenced this pull request Mar 27, 2015
…files besides extracting a directory (marcosdsanchez)

This PR was squashed before being merged into the 2.7 branch (closes #14002).

Discussion
----------

[Translation][Extractor] Allow extracting an array of files besides extracting a directory

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

Give ability to extractors to extract from an array of files a file, or a Traversable besides a directory.

The following commit adds the new feature to the following extractors:
PhpExtractor and TwigExtractor.
It also corrects the interface documentation to show the new options allowed as a parameter.

I found this change useful as I've got a custom translation:update command that instead of extracting from $rootPath. '/Resources/views/' it extracts from the files that git is showing as modified. The command usually runs much faster than the default as it only parses the needed files.

Commits
-------

9d6596c [Translation][Extractor] Allow extracting an array of files besides extracting a directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0