Beispiel #1
0
 /**
  * Method to add resources to be load.
  * 
  * @param FileResource $resource
  */
 public function addResource(FileResource $resource)
 {
     $name = basename($resource->getResource(), ".yml");
     if (!array_key_exists($name, $this->resources)) {
         $this->resources[$name] = $resource;
     }
 }
 public function testIsFresh()
 {
     $this->assertTrue($this->resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
     $this->assertFalse($this->resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
     $resource = new FileResource('/____foo/foobar' . rand(1, 999999));
     $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
 }
    private function getDomDocument(FileResource $resource)
    {
        $this->currentLibXmlErrorHandler = libxml_use_internal_errors(true);

        $document = new DOMDocument;
        // avoid creating textNodes for each carriage return
        $document->preserveWhiteSpace = false;
        // but preserve output indentation when dumping
        $document->formatOutput = true;

        $document->load($resource->getResource());

        return $document;
    }
Beispiel #4
0
    /**
     *
     * Updates the content of a csv file with value for the matched trans id
     */
    public function update(FileResource $resource, $id, $value)
    {
        if ('' === $id) {
            throw new InvalidTranslationKeyException(
                sprintf('An empty key can not be used in "%s"', $resource->getResource())
            );
        }

        $lines = $this->all($resource);

        if(false === $fd = fopen($resource->getResource(), 'r+b')) {
            throw new \InvalidArgumentException(sprintf('Error opening file "%s" for writing.', $resource->getResource()));
        }
        // empty the file
        ftruncate($fd, 0);

        $updated = false;
        foreach ($lines as $data) {
            if(0 === strpos($data[0], '#')) {
                continue;
            }
            if ($id === $data[0]) {
                // this line is the one we want to update
                $data[1] = $value;
                $updated = true;
            }
            fputcsv($fd, $data, ';', '"');
        }

        if (false === $updated) {
            $updated = false !== fputcsv($fd, array($id, $value), ';', '"');
        }
        fclose($fd);

        return $updated;
    }
    /**
     *
     * Updates the content of a yaml file with value for the matched trans id
     * The separation of trans id with dots describes the nested level of the node in yaml
     *
     * @see Symfony\Component\Translator\Loader\ArrayLoader which has the inverse behavior.
     *
     * knplabs_translator.title will match:
     * ``` yaml
     *
     * knplabs_translator:
     *     title: <new value>
     *
     * ```
     */
    public function update(FileResource $resource, $id, $value)
    {
        if('' === $id) {
            throw new InvalidTranslationKeyException(
                sprintf('An empty key can not be used in "%s"', $resource->getResource())
            );
        }
        $translations = Yaml::parse($resource->getResource());
        if (!is_array($translations)) {
            return false;
        }
        // working on references of the array elements
        $finalNode =& $translations;

        $explodedId = explode('.', $id);
        $count = count($explodedId);
        $i = 1;
        $lastKey = '';
        foreach ($explodedId as $key) {
            if( ! is_array($finalNode)) {
                throw new InvalidTranslationKeyException(
                    sprintf('The part "%s" of key "%s" is a scalar yaml node in "%s"', $lastKey, $id, $resource->getResource())
                );
            }
            if (false === array_key_exists($key, $finalNode)) {
                // node doesn't exist, create it
                // if last node, create scalar node, else array
                $node = ($i === $count) ? '' : array();
                $finalNode[$key] = $node;
            }
            // working on references of the array elements
            $finalNode =& $finalNode[$key];
            $lastKey = $key;
            $i++;
        }

        if(is_array($finalNode)) {
            throw new InvalidTranslationKeyException(
                sprintf('The key "%s" is not a scalar yaml node in "%s"', $id, $resource->getResource())
            );
        }

        //this puts the value in the translations array, by reference
        $finalNode = $value;
        // dump yaml and switch to inline at 1000th level
        $yaml = Yaml::dump($translations, 1000);

        $result = file_put_contents($resource->getResource(), $yaml);

        return false !== $result;
    }
Beispiel #6
0
 public function testGetResourceWithScheme()
 {
     $resource = new FileResource('file://' . $this->file);
     $this->assertSame('file://' . $this->file, $resource->getResource(), '->getResource() returns the path to the schemed resource');
 }