Carew\Event\Listener\Metadata\Extraction::onDocument PHP Method

onDocument() public method

public onDocument ( CarewEvent $event )
$event Carew\Event\CarewEvent
    public function onDocument(CarewEvent $event)
    {
        $document = $event->getSubject();
        $file = $document->getFile();
        $document->setPath($this->path->generatePath($file->getRelativePathName()));
        preg_match('#^---\\n(.+)---\\n(.+)$#sU', $document->getBody(), $matches);
        if ($matches) {
            list(, $metadatas, $body) = $matches;
            $metadatas = Yaml::parse($metadatas);
            $document->setLayout('default');
            foreach ($metadatas as $key => $value) {
                $method = 'set' . ucfirst($key);
                if (method_exists($document, $method)) {
                    $document->{$method}($value);
                    unset($metadatas[$key]);
                }
            }
            if (isset($metadatas['permalink'])) {
                $document->setPath($this->path->generatePath($metadatas['permalink']));
            }
            $document->addMetadatas($metadatas);
            $document->setBody($body);
        }
    }

Usage Example

Example #1
0
 /**
  * @dataProvider getOnDocumentWithPermalink
  */
 public function testOnDocumentWithPermalink($expected, $file)
 {
     $document = $this->createDocument($file);
     $event = new CarewEvent($document);
     $extraction = new Extraction();
     $extraction->onDocument($event);
     $this->assertSame($expected, $document->getPath());
 }