-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrabAssets.php
145 lines (131 loc) · 5.06 KB
/
GrabAssets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* List of assets and selected assets for PicoCMS.
*
* Marginally based off the code by Nicolas Liautaud
* (https://github.com/nliautaud/pico-pages-list)
*
* - Adds twig global `{{ array_of_assets }}` and `{{ selected_assets }}`
*
* Selected assets are only set if file.md contains the following YAML:
*
* selected_assets: assets/path_to_assets
*
* So what is it good for? Directories of images. Using this one can get
* image paths and create, using TWIG, a page full of images.
*
* @author Lloyd Sargent
* @link
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
*
* Hours having written php code, maybe 4 hours. Heh.
*/
class GrabAssets extends AbstractPicoPlugin
{
const API_VERSION = 2;
private $currentPagePath;
/**
* Triggered when Pico discovered the current, previous and next pages
*
* This function is designed on save the base_url.
*
* @see Pico::getCurrentPage()
* @see Pico::getPreviousPage()
* @see Pico::getNextPage()
*
* @param array|null &$currentPage data of the page being served
* @param array|null &$previousPage data of the previous page
* @param array|null &$nextPage data of the next page
*
* @return void
*/
protected function onCurrentPageDiscovered(array &$currentPage = null, array &$previousPage = null, array &$nextPage = null) {
if ($currentPage == null) {
return;
}
$base_url = $this->getConfig('base_url');
$this->currentPagePath = str_replace(array('?', $base_url), '', urldecode($currentPage['url']));
}
/**
* Triggered before Pico renders the page
*
* This depends on two settings that exist in your config.yml file.
* assets_dir: assets # path to assets
* supported_assets: [gif, jpg, jpeg, png, svg]
*
* assets_dir is for sites with very complex paths. This must exist for the
* plugin to work. Otherwise the twig variables are empty.
*
* supported_assets defines what type of assets you want in your list.
*
* Note that this code is VERY wordy and could be shortened. As they say,
* "Next version."
*
* Register twig variable 'array_of_assets'
* Register twig variable 'selected_assets'
*
* array_of_assets will contain all assets
* selected_assets will contain files in the diretory in
* the meta.selected_assets (in the header of the .md file)
*
* @see DummyPlugin::onPageRendered()
*
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
*
* @return void
*/
public function onPageRendering(string &$templateName, array &$twigVariables)
{
$twig = $this->getPico()->getTwig();
$twigConfig = $twigVariables['config'];
$twigMeta = $twigVariables['meta'];
//----- see if our assets configuration variable exists
//----- if it doesn't exist, return early
if (!array_key_exists('assets_dir', $twigConfig)) {
$twigVariables['nested_assets'] = [];
$twigVariables['selected_assets'] = [];
return;
}
//----- assume a bogus asset directory. Very unlikely.
$selectedAssetDirectory = '/deadbeef/beefdead/deadbeef';
if (array_key_exists('selected_assets', $twigMeta)) {
$selectedAssetDirectory = $twigMeta['selected_assets'];
} else {
return;
}
//----- create our assets directory
$base_dir = $twigVariables['base_dir'];
$base_dir .= '/';
$assets_dir = $twigVariables['base_dir'];
$assets_dir .= '/';
$assets_dir .= $selectedAssetDirectory;
//----- get our supported images
$supported_assets = $twigConfig['supported_assets'];
//----- create our iterator
$Directory = new RecursiveDirectoryIterator($assets_dir);
$Iterator = new RecursiveIteratorIterator($Directory);
//----- iterate through each directory
$selected_files = array();
$all_assets = array();
foreach ($Iterator as $info) {
$src_file_name = $info->getPathName();
$src_file_name = str_replace($base_dir, '', $src_file_name);
$ext = strtolower(pathinfo($src_file_name, PATHINFO_EXTENSION));
//----- only keep a list of supported file types
if (in_array($ext, $supported_assets)) {
$all_assets[] = $src_file_name;
//----- chop off the leading part so we are left with `assets/blah/blah.png`
if (substr($src_file_name, 0, strlen($selectedAssetDirectory)) === $selectedAssetDirectory) {
$selected_files[] = $src_file_name;
}
}
}
//----- set twig variables that we just created
sort($all_assets);
$twigVariables['array_of_assets'] = $all_assets;
sort($selected_files);
$twigVariables['selected_assets'] = $selected_files;
}
}