Skip to content

Commit 1564adb

Browse files
committed
Repository refactor
1 parent db543ff commit 1564adb

4 files changed

Lines changed: 203 additions & 287 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
use Illuminate\Config\Repository as Config;
4+
use Illuminate\Filesystem\Filesystem;
5+
6+
abstract class AbstractCodexRepository implements CodexRepositoryInterface
7+
{
8+
/**
9+
* @var Config
10+
*/
11+
protected $config;
12+
13+
/**
14+
* @var Filesystem
15+
*/
16+
protected $files;
17+
18+
public function __construct(Config $config, Filesyste $files)
19+
{
20+
$this->config = $config;
21+
$this->files = $files;
22+
}
23+
24+
public function getDefaultManual()
25+
{
26+
$manuals = $this->getManuals();
27+
28+
if (count($manuals) > 1) {
29+
if ( ! is_null($this->config->get('codex.default_manual'))) {
30+
return $this->config->get('codex.default_manual');
31+
} else {
32+
return strval($manuals[0]);
33+
}
34+
} elseif (count($manuals) === 1) {
35+
return strval($manuals[0]);
36+
} else {
37+
return null;
38+
}
39+
}
40+
41+
public function getDefaultVersion($manual)
42+
{
43+
$versions = $this->getVersions($manual);
44+
45+
switch ($this->config->get('codex.version_ordering')) {
46+
case 'numerical':
47+
sort($versions, SORT_NATURAL);
48+
break;
49+
case 'alphabetically':
50+
sort($versions, SORT_NUMERIC);
51+
break;
52+
}
53+
54+
return $versions[0];
55+
}
56+
57+
/**
58+
* Get all manuals from documentation directory.
59+
*
60+
* @return array
61+
*/
62+
public function getManuals()
63+
{
64+
return $this->getDirectories($this->storagePath);
65+
}
66+
67+
/**
68+
* Get all versions for the given manual.
69+
*
70+
* @param string $manual
71+
* @return array
72+
*/
73+
public function getVersions($manual)
74+
{
75+
$manualDir = $this->storagePath.'/'.$manual;
76+
77+
return $this->getDirectories($manualDir);
78+
}
79+
80+
/**
81+
* Return an array of folders within the supplied path.
82+
*
83+
* @param string $path
84+
* @return array
85+
*/
86+
public function getDirectories($path)
87+
{
88+
if ( ! $this->files->exists($path)) {
89+
App::abort(404);
90+
}
91+
92+
$directories = $this->files->directories($path);
93+
$folders = [];
94+
95+
if (count($directories) > 0) {
96+
97+
}
98+
99+
foreach ($directories as $dir) {
100+
$dir = str_replace('\\', '/', $dir);
101+
$folder = explode('/', $dir);
102+
$folders[] = end($folder);
103+
}
104+
105+
return $folders;
106+
}
107+
108+
/**
109+
* Return the first line of the supplied page. This will (or rather should)
110+
* always be an <h1> tag.
111+
*
112+
* @param string $page
113+
* @return string
114+
*/
115+
protected function getPageTitle($page)
116+
{
117+
$file = fopen($page, 'r');
118+
$title = fgets($file);
119+
120+
fclose($file);
121+
122+
return $title;
123+
}
124+
125+
/**
126+
* Gets the given documentation page modification time.
127+
*
128+
* @param string $manual
129+
* @param string $version
130+
* @param string $page
131+
* @return mixed
132+
*/
133+
public function getUpdatedTimestamp($manual, $version, $page)
134+
{
135+
$page = $this->storagePath.'/'.$manual.'/'.$version.'/'.$page.'.md';
136+
137+
if ($this->files->exists($page)) {
138+
$timestamp = DateTime::createFromFormat('U', filemtime($page));
139+
140+
return $timestamp->format($this->config->get('codex.modified_timestamp'));
141+
} else {
142+
return false;
143+
}
144+
}
145+
}

app/repositories/CodexRepositoryFlat.php

Lines changed: 1 addition & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Config\Repository as Config;
44
use Illuminate\Filesystem\Filesystem;
55

6-
class CodexRepositoryFlat implements CodexRepositoryInterface
6+
class CodexRepositoryFlat extends AbstractCodexRepository
77
{
88
use CacheTrait;
99

@@ -81,94 +81,6 @@ public function get($manual, $version, $page)
8181
}
8282
}
8383

84-
/**
85-
* Gets the given documentation page modification time.
86-
*
87-
* @param string $manual
88-
* @param string $version
89-
* @param string $page
90-
* @return mixed
91-
*/
92-
public function getUpdatedTimestamp($manual, $version, $page)
93-
{
94-
$page = $this->storagePath.'/'.$manual.'/'.$version.'/'.$page.'.md';
95-
96-
if ($this->files->exists($page)) {
97-
$timestamp = DateTime::createFromFormat('U', filemtime($page));
98-
99-
return $timestamp->format($this->config->get('codex.modified_timestamp'));
100-
} else {
101-
return false;
102-
}
103-
}
104-
105-
/**
106-
* Get all manuals from documentation directory.
107-
*
108-
* @return array
109-
*/
110-
public function getManuals()
111-
{
112-
return $this->getDirectories($this->storagePath);
113-
}
114-
115-
/**
116-
* Get all versions for the given manual.
117-
*
118-
* @param string $manual
119-
* @return array
120-
*/
121-
public function getVersions($manual)
122-
{
123-
$manualDir = $this->storagePath.'/'.$manual;
124-
125-
return $this->getDirectories($manualDir);
126-
}
127-
128-
/**
129-
* Get the default manual.
130-
*
131-
* @return mixed
132-
*/
133-
public function getDefaultManual()
134-
{
135-
$manuals = $this->getManuals();
136-
137-
if (count($manuals) > 1) {
138-
if ( ! is_null($this->config->get('codex.default_manual'))) {
139-
return $this->config->get('codex.default_manual');
140-
} else {
141-
return strval($manuals[0]);
142-
}
143-
} elseif (count($manuals) === 1) {
144-
return strval($manuals[0]);
145-
} else {
146-
return null;
147-
}
148-
}
149-
150-
/**
151-
* Get the default version for the given manual.
152-
*
153-
* @param string $manual
154-
* @return string
155-
*/
156-
public function getDefaultVersion($manual)
157-
{
158-
$versions = $this->getVersions($manual);
159-
160-
switch ($this->config->get('codex.version_ordering')) {
161-
case 'numerical':
162-
sort($versions, SORT_NATURAL);
163-
break;
164-
case 'alphabetically':
165-
sort($versions, SORT_NUMERIC);
166-
break;
167-
}
168-
169-
return $versions[0];
170-
}
171-
17284
/**
17385
* Search manual for given string.
17486
*
@@ -200,48 +112,4 @@ public function search($manual, $version, $needle = '')
200112
return $results;
201113
}
202114

203-
/**
204-
* Return the first line of the supplied page. This will (or rather should)
205-
* always be an <h1> tag.
206-
*
207-
* @param string $page
208-
* @return string
209-
*/
210-
private function getPageTitle($page)
211-
{
212-
$file = fopen($page, 'r');
213-
$title = fgets($file);
214-
215-
fclose($file);
216-
217-
return $title;
218-
}
219-
220-
/**
221-
* Return an array of folders within the supplied path.
222-
*
223-
* @param string $path
224-
* @return array
225-
*/
226-
private function getDirectories($path)
227-
{
228-
if ( ! $this->files->exists($path)) {
229-
App::abort(404);
230-
}
231-
232-
$directories = $this->files->directories($path);
233-
$folders = [];
234-
235-
if (count($directories) > 0) {
236-
237-
}
238-
239-
foreach ($directories as $dir) {
240-
$dir = str_replace('\\', '/', $dir);
241-
$folder = explode('/', $dir);
242-
$folders[] = end($folder);
243-
}
244-
245-
return $folders;
246-
}
247115
}

0 commit comments

Comments
 (0)