Skip to content

Commit

Permalink
[Translation][Extractor] Allow extracting an array of files besides e…
Browse files Browse the repository at this point in the history
…xtracting a directory
  • Loading branch information
marcosdsanchez authored and fabpot committed Mar 27, 2015
1 parent fd95ee2 commit 2f19ca5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
83 changes: 83 additions & 0 deletions Extractor/AbstractFileExtractor.php
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);
}
8 changes: 4 additions & 4 deletions Extractor/ExtractorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
use Symfony\Component\Translation\MessageCatalogue;

/**
* Extracts translation messages from a template directory to the catalogue.
* Extracts translation messages from a directory or files to the catalogue.
* New found messages are injected to the catalogue using the prefix.
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
interface ExtractorInterface
{
/**
* Extracts translation messages from a template directory to the catalogue.
* Extracts translation messages from files, a file or a directory to the catalogue.
*
* @param string $directory The path to look into
* @param string|array $resource files, a file or a directory
* @param MessageCatalogue $catalogue The catalogue
*/
public function extract($directory, MessageCatalogue $catalogue);
public function extract($resource, MessageCatalogue $catalogue);

/**
* Sets the prefix that should be used for new found messages.
Expand Down

0 comments on commit 2f19ca5

Please sign in to comment.