Skip to content

Commit d467086

Browse files
committed
Implemented other assets.
1 parent 91a50a0 commit d467086

30 files changed

+843
-177
lines changed

lib/ddl/assets/0100_create_tables.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ create table PLS_ASSET
22
(
33
ass_type varchar not null, -- The asset type (css, images, js).
44
ass_base_dir varchar not null, -- The directory relative to project root to base dir of the asset.
5+
ass_to_dir varchar, -- The directory under the asset dir (assert root + asset type).
56
ass_path varchar not null -- The relative path of the asset.
67
);
78

8-
create unique index idx_pls_asset_01 on PLS_ASSET(ass_type, ass_base_dir, ass_path);
9+
create unique index idx_pls_asset_01 on PLS_ASSET(ass_type, ass_base_dir, ass_to_dir, ass_path);
910

1011
create table PLS_CURRENT
1112
(
1213
cur_type varchar not null, -- The asset type (css, images, js).
1314
cur_base_dir varchar not null, -- The directory relative to project root to base dir of the asset.
15+
cur_to_dir varchar, -- The directory under the asset dir (assert root + asset type).
1416
cur_path varchar not null -- The relative path of the asset.
1517
);
1618

17-
create unique index idx_pls_current_01 on PLS_CURRENT(cur_type, cur_base_dir, cur_path);
19+
create unique index idx_pls_current_01 on PLS_CURRENT(cur_type, cur_base_dir, cur_to_dir, cur_path);

lib/psql/assets/pls_current_assets_delete_asset.psql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
*
44
* @param varchar :p_cur_type The asset type (css, images, js).
55
* @param varchar :p_cur_base_dir The directory relative to project root to base dir of the asset.
6+
* @param varchar :p_cur_to_dir The directory under the asset dir (assert root + asset type).
67
* @param varchar :p_cur_path The relative path of the asset.
78

89
* @type none
910
*/
1011
delete from PLS_CURRENT
1112
where cur_type = :p_cur_type
1213
and cur_base_dir = :p_cur_base_dir
14+
and cur_to_dir = :p_cur_to_dir
1315
and cur_path = :p_cur_path

lib/psql/assets/pls_current_assets_get_all.psql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
select cur_type
77
, cur_base_dir
8+
, cur_to_dir
89
, cur_path
910
from PLS_CURRENT
1011
order by cur_type
1112
, cur_base_dir
13+
, cur_to_dir
1214
, cur_path

lib/psql/assets/pls_diff_current.psql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
*/
66
select ass_type
77
, ass_base_dir
8+
, ass_to_dir
89
, ass_path
910
, sum(case src when 1 then 1 else 0 end) cnt1
1011
, sum(case src when 2 then 1 else 0 end) cnt2
1112
from
1213
(
1314
select ass_type
1415
, ass_base_dir
16+
, ass_to_dir
1517
, ass_path
1618
, 1 src
1719
from PLS_ASSET
@@ -20,15 +22,18 @@ from
2022

2123
select cur_type
2224
, cur_base_dir
25+
, cur_to_dir
2326
, cur_path
2427
, 2 src
2528
from PLS_CURRENT
2629
) t
2730
group by ass_type
2831
, ass_base_dir
32+
, ass_to_dir
2933
, ass_path
3034
having cnt1 = 1
3135
and cnt2 = 1
3236
order by ass_type
3337
, ass_base_dir
38+
, ass_to_dir
3439
, ass_path

lib/psql/assets/pls_diff_new.psql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
*/
66
select ass_type
77
, ass_base_dir
8+
, ass_to_dir
89
, ass_path
910
, sum(case src when 1 then 1 else 0 end) cnt1
1011
, sum(case src when 2 then 1 else 0 end) cnt2
1112
from
1213
(
1314
select ass_type
1415
, ass_base_dir
16+
, ass_to_dir
1517
, ass_path
1618
, 1 src
1719
from PLS_ASSET
@@ -20,15 +22,18 @@ from
2022

2123
select cur_type
2224
, cur_base_dir
25+
, cur_to_dir
2326
, cur_path
2427
, 2 src
2528
from PLS_CURRENT
2629
) t
2730
group by ass_type
2831
, ass_base_dir
32+
, ass_to_dir
2933
, ass_path
3034
having cnt1 = 1
3135
and cnt2 = 0
3236
order by ass_type
3337
, ass_base_dir
38+
, ass_to_dir
3439
, ass_path

lib/psql/assets/pls_diff_obsolete.psql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
*/
66
select ass_type
77
, ass_base_dir
8+
, ass_to_dir
89
, ass_path
910
, sum(case src when 1 then 1 else 0 end) cnt1
1011
, sum(case src when 2 then 1 else 0 end) cnt2
1112
from
1213
(
1314
select ass_type
1415
, ass_base_dir
16+
, ass_to_dir
1517
, ass_path
1618
, 1 src
1719
from PLS_ASSET
@@ -20,15 +22,18 @@ from
2022

2123
select cur_type
2224
, cur_base_dir
25+
, cur_to_dir
2326
, cur_path
2427
, 2 src
2528
from PLS_CURRENT
2629
) t
2730
group by ass_type
2831
, ass_base_dir
32+
, ass_to_dir
2933
, ass_path
3034
having cnt1 = 0
3135
and cnt2 = 1
3236
order by ass_type
3337
, ass_base_dir
38+
, ass_to_dir
3439
, ass_path

src/Command/AssetsCommand.php

Lines changed: 94 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
use Plaisio\Console\Helper\Assets\AssetsPlaisioXmlHelper;
77
use Plaisio\Console\Helper\Assets\AssetsStore;
8+
use Plaisio\Console\Helper\ConfigException;
89
use Plaisio\Console\Helper\PlaisioXmlUtility;
9-
use SetBased\Exception\RuntimeException;
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Webmozart\PathUtil\Path;
@@ -17,13 +17,6 @@
1717
class AssetsCommand extends PlaisioCommand
1818
{
1919
//--------------------------------------------------------------------------------------------------------------------
20-
/**
21-
* All possible assets types.
22-
*
23-
* @var string[]
24-
*/
25-
private $assetTypes = ['css', 'images', 'js'];
26-
2720
/**
2821
* File count.
2922
*
@@ -63,19 +56,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
6356
{
6457
$this->io->title('Plaisio: Assets');
6558

66-
$this->readResourceDir();
67-
$this->createStore();
68-
$this->findAssets();
69-
$this->readCurrentAssets();
70-
$this->assetsRemoveObsolete();
71-
$this->assetsUpdateCurrent();
72-
$this->assetsAddNew();
73-
$this->writeCurrentAssets();
59+
try
60+
{
61+
$this->readResourceDir();
62+
$this->createStore();
63+
$this->findAssets();
64+
$this->readCurrentAssets();
65+
$this->assetsRemoveObsolete();
66+
$this->assetsUpdateCurrent();
67+
$this->assetsAddNew();
68+
$this->writeCurrentAssets();
69+
70+
$this->io->text(sprintf('Removed %d, updated %d, and added %d assets',
71+
$this->count['old'],
72+
$this->count['current'],
73+
$this->count['new']));
74+
}
75+
catch (ConfigException $exception)
76+
{
77+
$this->io->error($exception->getMessage());
78+
$this->io->logVerbose($exception->getTraceAsString());
7479

75-
$this->io->text(sprintf('Removed %d, updated %d, and added %d assets',
76-
$this->count['old'],
77-
$this->count['current'],
78-
$this->count['new']));
80+
return -1;
81+
}
7982

8083
return 0;
8184
}
@@ -89,8 +92,8 @@ private function assetsAddNew(): void
8992
$assets = $this->store->plsDiffNew();
9093
foreach ($assets as $asset)
9194
{
92-
$pathSource = Path::join($asset['ass_base_dir'], $asset['ass_path']);
93-
$pathDest = Path::join($this->rootAssetDir, $asset['ass_type'], $asset['ass_path']);
95+
$pathSource = $this->composeSourcePath($asset);
96+
$pathDest = $this->composeDestPath($asset);
9497

9598
$this->io->logVerbose('Adding asset <fso>%s</fso>', $pathDest);
9699

@@ -99,6 +102,7 @@ private function assetsAddNew(): void
99102
$this->store->insertRow('PLS_CURRENT', ['cur_id' => null,
100103
'cur_type' => $asset['ass_type'],
101104
'cur_base_dir' => $asset['ass_base_dir'],
105+
'cur_to_dir' => $asset['ass_to_dir'],
102106
'cur_path' => $asset['ass_path']]);
103107
$this->count['new']++;
104108
}
@@ -113,7 +117,7 @@ private function assetsRemoveObsolete(): void
113117
$assets = $this->store->plsDiffObsolete();
114118
foreach ($assets as $asset)
115119
{
116-
$path = Path::join($this->rootAssetDir, $asset['ass_type'], $asset['ass_path']);
120+
$path = $this->composeDestPath($asset);
117121
if (file_exists($path))
118122
{
119123
$this->io->logVerbose('Removing obsolete asset <fso>%s</fso>', $path);
@@ -123,7 +127,10 @@ private function assetsRemoveObsolete(): void
123127
$this->count['old']++;
124128
}
125129

126-
$this->store->plsCurrentAssetsDeleteAsset($asset['ass_type'], $asset['ass_base_dir'], $asset['ass_path']);
130+
$this->store->plsCurrentAssetsDeleteAsset($asset['ass_type'],
131+
$asset['ass_base_dir'],
132+
$asset['ass_to_dir'],
133+
$asset['ass_path']);
127134
}
128135
}
129136

@@ -136,8 +143,8 @@ private function assetsUpdateCurrent(): void
136143
$assets = $this->store->plsDiffCurrent();
137144
foreach ($assets as $asset)
138145
{
139-
$pathSource = Path::join($asset['ass_base_dir'], $asset['ass_path']);
140-
$pathDest = Path::join($this->rootAssetDir, $asset['ass_type'], $asset['ass_path']);
146+
$pathSource = $this->composeSourcePath($asset);
147+
$pathDest = $this->composeDestPath($asset);
141148

142149
$source = file_get_contents($pathSource);
143150
if (file_exists($pathDest))
@@ -160,6 +167,32 @@ private function assetsUpdateCurrent(): void
160167
}
161168
}
162169

170+
//--------------------------------------------------------------------------------------------------------------------
171+
/**
172+
* Composes the destination path of an asset.
173+
*
174+
* @param array $asset The details of the asset.
175+
*
176+
* @return string
177+
*/
178+
private function composeDestPath(array $asset): string
179+
{
180+
return Path::join($this->rootAssetDir, $asset['ass_type'], $asset['ass_to_dir'] ?? '', $asset['ass_path']);
181+
}
182+
183+
//--------------------------------------------------------------------------------------------------------------------
184+
/**
185+
* Composes the source path of an asset.
186+
*
187+
* @param array $asset The details of the asset.
188+
*
189+
* @return string
190+
*/
191+
private function composeSourcePath(array $asset): string
192+
{
193+
return Path::join($asset['ass_base_dir'], $asset['ass_path']);
194+
}
195+
163196
//--------------------------------------------------------------------------------------------------------------------
164197
/**
165198
* Copies a file.
@@ -190,30 +223,53 @@ private function createStore(): void
190223
//--------------------------------------------------------------------------------------------------------------------
191224
/**
192225
* Finds all assets all packages (using plaisio-assets.xml files).
226+
*
227+
* @throws ConfigException
193228
*/
194229
private function findAssets(): void
195230
{
196231
$plaisioXmlList = PlaisioXmlUtility::findPlaisioXmlPackages('assets');
197232
foreach ($plaisioXmlList as $plaisioConfigPath)
198233
{
199-
$helper = new AssetsPlaisioXmlHelper($plaisioConfigPath);
200-
foreach ($this->assetTypes as $assetType)
234+
$helper = new AssetsPlaisioXmlHelper($plaisioConfigPath);
235+
$collections = $helper->queryAssetFileList();
236+
if (!empty($collections))
201237
{
202-
$files = $helper->queryFileList($assetType);
203-
if (!empty($files))
238+
foreach ($collections as $collection)
204239
{
205-
foreach ($files['files'] as $file)
240+
foreach ($collection['files'] as $file)
206241
{
207-
$this->io->logVerbose('Found asset <fso>%s</fso>', Path::join($files['base-dir'], $file));
242+
$this->io->logVerbose('Found asset <fso>%s</fso>', Path::join($collection['base-dir'], $file));
208243

209244
$this->store->insertRow('PLS_ASSET', ['ass_id' => null,
210-
'ass_type' => $files['type'],
211-
'ass_base_dir' => $files['base-dir'],
245+
'ass_type' => $collection['type'],
246+
'ass_base_dir' => $collection['base-dir'],
247+
'ass_to_dir' => $collection['to-dir'],
212248
'ass_path' => $file]);
213249
}
214250
}
215251
}
216252
}
253+
254+
$plaisioConfigPath = PlaisioXmlUtility::plaisioXmlPath('assets');
255+
$helper = new AssetsPlaisioXmlHelper($plaisioConfigPath);
256+
$collections = $helper->queryOtherAssetFileList();
257+
if (!empty($collections))
258+
{
259+
foreach ($collections as $collection)
260+
{
261+
foreach ($collection['files'] as $file)
262+
{
263+
$this->io->logVerbose('Found asset <fso>%s</fso>', Path::join($collection['base-dir'], $file));
264+
265+
$this->store->insertRow('PLS_ASSET', ['ass_id' => null,
266+
'ass_type' => $collection['type'],
267+
'ass_base_dir' => $collection['base-dir'],
268+
'ass_to_dir' => $collection['to-dir'],
269+
'ass_path' => $file]);
270+
}
271+
}
272+
}
217273
}
218274

219275
//--------------------------------------------------------------------------------------------------------------------
@@ -232,7 +288,8 @@ private function readCurrentAssets(): void
232288
$this->store->insertRow('PLS_CURRENT', ['cur_id' => null,
233289
'cur_type' => $data[0],
234290
'cur_base_dir' => $data[1],
235-
'cur_path' => $data[2]]);
291+
'cur_to_dir' => $data[2],
292+
'cur_path' => $data[3]]);
236293
}
237294
fclose($handle);
238295
}
@@ -250,7 +307,7 @@ private function readResourceDir(): void
250307

251308
if (!file_exists($this->rootAssetDir))
252309
{
253-
throw new RuntimeException("Asset root directory '%s' does not exists", $this->rootAssetDir);
310+
throw new ConfigException("Asset root directory '%s' does not exists", $this->rootAssetDir);
254311
}
255312
}
256313

@@ -268,7 +325,7 @@ private function writeCurrentAssets(): void
268325
$handle = fopen($path1, 'w');
269326
foreach ($assets as $asset)
270327
{
271-
fputcsv($handle, [$asset['cur_type'], $asset['cur_base_dir'], $asset['cur_path']]);
328+
fputcsv($handle, [$asset['cur_type'], $asset['cur_base_dir'], $asset['cur_to_dir'], $asset['cur_path']]);
272329
}
273330

274331
$new = file_get_contents($path1);

0 commit comments

Comments
 (0)