-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
66cf6a7
Give ability to extractors to extract from an array of files besides …
marcosdsanchez 692c363
Bump symfony/translation dependency of Twig Bridge to 2.7
marcosdsanchez f27491c
Bump symfony/translation dependency of FrameworkBundle to 2.7
marcosdsanchez 4c5adbc
Update Twig Bridge and FrameworkBundle CHANGELOG.md files
marcosdsanchez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Symfony/Bridge/Twig/Tests/Fixtures/extractor/with_translations.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>{{ 'Hi!'|trans }}</h1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/Translation/Extractor/AbstractFileExtractor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Extractor; | ||
|
||
/** | ||
* Base class used by classes that extract translation messages from files. | ||
* | ||
* @author Marcos D. Sánchez <marcosdsanchez@gmail.com> | ||
*/ | ||
abstract class AbstractFileExtractor | ||
{ | ||
/** | ||
* @param string|array $resource files, a file or a directory | ||
* | ||
* @return array | ||
*/ | ||
protected function extractFiles($resource) | ||
{ | ||
if (is_array($resource) || $resource instanceof \Traversable) { | ||
$files = array(); | ||
foreach ($resource as $file) { | ||
if ($this->canBeExtracted($file)) { | ||
$files[] = $this->toSplFileInfo($file); | ||
} | ||
} | ||
} elseif (is_file($resource)) { | ||
$files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array(); | ||
} else { | ||
$files = $this->extractFromDirectory($resource); | ||
} | ||
|
||
return $files; | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* | ||
* @return \SplFileInfo | ||
*/ | ||
private function toSplFileInfo($file) | ||
{ | ||
return ($file instanceof \SplFileInfo) ? $file : new \SplFileInfo($file); | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* | ||
* @throws \InvalidArgumentException | ||
* | ||
* @return bool | ||
*/ | ||
protected function isFile($file) | ||
{ | ||
if (!is_file($file)) { | ||
throw new \InvalidArgumentException(sprintf('The "%s" file does not exist.', $file)); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* | ||
* @return bool | ||
*/ | ||
abstract protected function canBeExtracted($file); | ||
|
||
/** | ||
* @param string|array $resource files, a file or a directory | ||
* | ||
* @return array files to be extracted | ||
*/ | ||
abstract protected function extractFromDirectory($resource); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
implementExtractorInterface
instead? Or can we think about a use case where this is not necessary?There was a problem hiding this comment.
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.