1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Markus Tacker <[email protected]> |
5
|
|
|
* @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BCRM\BackendBundle\Content; |
9
|
|
|
|
10
|
|
|
use BCRM\BackendBundle\Exception\FileNotFoundException; |
11
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
12
|
|
|
|
13
|
|
|
class FileContentReader implements ContentReader |
14
|
|
|
{ |
15
|
|
|
const PROPERTIES_MATCH = '/@([a-z0-9]+)=([^\n]+)\n/'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \SplFileInfo |
19
|
|
|
*/ |
20
|
|
|
protected $contentDir; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $contentPath; |
26
|
|
|
|
27
|
|
|
public function __construct($contentDir, $contentPath) |
28
|
|
|
{ |
29
|
|
|
$this->contentDir = new \SplFileInfo($contentDir); |
30
|
|
|
$this->contentPath = $contentPath; |
31
|
|
|
$this->properties = new ArrayCollection(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDocs} |
36
|
|
|
*/ |
37
|
|
|
public function getInfo($path) |
38
|
|
|
{ |
39
|
|
|
$file = $this->getFilePath($path); |
40
|
|
|
$info = new Info(); |
41
|
|
|
$info->setLastModified(new \DateTime('@' . filemtime($file))); |
42
|
|
|
$info->setEtag(md5_file($file)); |
43
|
|
|
return $info; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $path |
48
|
|
|
* |
49
|
|
|
* @return \SplFileInfo |
50
|
|
|
*/ |
51
|
|
|
protected function getFilePath($path) |
52
|
|
|
{ |
53
|
|
|
$contentdir = $this->contentDir->getPathname() . DIRECTORY_SEPARATOR; |
54
|
|
|
$file = $contentdir . $path; |
55
|
|
|
if (!is_file($file)) throw new FileNotFoundException($path); |
56
|
|
|
return new \SplFileInfo($file); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDocs} |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function getContent($path) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$file = $this->getFilePath($path); |
65
|
|
|
$content = file_get_contents($file->getPathname()); |
66
|
|
|
$properties = $this->readProperties($content); |
67
|
|
|
$content = $this->removeProperties($content); |
68
|
|
|
$c = new Content(); |
69
|
|
|
$c->setContent($content); |
70
|
|
|
$c->setProperties(new ArrayCollection(array_merge($c->getProperties()->toArray(), $properties))); |
71
|
|
|
return $c; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function readProperties($markdown) |
75
|
|
|
{ |
76
|
|
|
$properties = array(); |
77
|
|
|
if (!preg_match_all(static::PROPERTIES_MATCH, $markdown, $matches, PREG_SET_ORDER)) return $properties; |
78
|
|
|
foreach ($matches as $match) { |
79
|
|
|
$properties[$match[1]] = $match[2]; |
80
|
|
|
} |
81
|
|
|
return $properties; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function removeProperties($markdown) |
85
|
|
|
{ |
86
|
|
|
return preg_replace(static::PROPERTIES_MATCH, '', $markdown); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: