Skip to content

Commit 61e2b68

Browse files
committed
Support laravel storage for custom dicts
1 parent 9f182b6 commit 61e2b68

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,28 @@ return [
5151

5252
'morphies' => [
5353
[
54-
// by this name specific morphy can be accessed through Morphy::morphy method
55-
// it may be any string
54+
// phpMorphy instance name
55+
// specific morphy can be accessed through Morphy::morphy($name) method
5656
'name' => SEOService2020\Morphy\Morphy::russianLang,
57+
58+
// phpMorphy language
59+
// see Morphy class for available values
5760
'language' => SEOService2020\Morphy\Morphy::russianLang,
58-
// if no options key or null value specified, default options will be used
61+
62+
// phpMorphy options
63+
// if not specified or null specified, default options will be used
5964
// to use common options from this config, specify []
6065
'options' => [],
61-
// when null specified, default morphy dicts path will be used
66+
67+
// dicts directory path
68+
// if not specified or null specified, default dicts path will be used
6269
'dicts_path' => null,
70+
71+
// values are 'storage', 'filesystem'
72+
// 'storage': dicts will be taken from laravel storage (storage/app folder)
73+
// 'filesystem': dicts will be taken by specified path as-is
74+
// must be specified when dicts_path is not null
75+
'dicts_storage' => 'storage',
6376
],
6477
],
6578
];

config/config.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@
1010

1111
'morphies' => [
1212
[
13-
// by this name specific morphy can be accessed through Morphy::morphy method
14-
// it may be any string
13+
// phpMorphy instance name
14+
// specific morphy can be accessed through Morphy::morphy($name) method
1515
'name' => SEOService2020\Morphy\Morphy::russianLang,
16+
17+
// phpMorphy language
18+
// see Morphy class for available values
1619
'language' => SEOService2020\Morphy\Morphy::russianLang,
17-
// if no options key or null value specified, default options will be used
20+
21+
// phpMorphy options
22+
// if not specified or null specified, default options will be used
1823
// to use common options from this config, specify []
1924
'options' => [],
20-
// when null specified, default morphy dicts path will be used
25+
26+
// dicts directory path
27+
// if not specified or null specified, default dicts path will be used
2128
'dicts_path' => null,
29+
30+
// values are 'storage', 'filesystem'
31+
// 'storage': dicts will be taken from laravel storage (storage/app folder)
32+
// 'filesystem': dicts will be taken by specified path as-is
33+
// must be specified when dicts_path is not null
34+
'dicts_storage' => 'storage',
2235
],
2336
],
2437
];

src/Factory.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,43 @@
22

33
namespace SEOService2020\Morphy;
44

5+
use InvalidArgumentException;
6+
7+
use Illuminate\Support\Facades\Storage;
8+
59

610
class Factory
711
{
812
public static function makeMorphy(array $config, ?array $commonOptions = null): Morphy
913
{
10-
return new Morphy(
11-
$config['language'],
12-
($config['options'] ?? null) === null ?
13-
null :
14-
array_replace_recursive($commonOptions ?? [], $config['options'] ?? []),
15-
$config['dicts_path'] ?? null
16-
);
14+
if (!array_key_exists('language', $config)) {
15+
throw new InvalidArgumentException(
16+
"language must be specified for morphy {$config['name']}"
17+
);
18+
}
19+
20+
if (!array_key_exists('dicts_storage', $config)) {
21+
throw new InvalidArgumentException(
22+
"dicts_storage must be specified for morphy {$config['name']}"
23+
);
24+
}
25+
26+
$dictsStorage = $config['dicts_storage'];
27+
if (!in_array($dictsStorage, ['storage', 'filesystem'])) {
28+
throw new InvalidArgumentException(
29+
"Wrong dicts_storage specified for morphy {$config['name']}: $dictsStorage"
30+
);
31+
}
32+
33+
$options = $config['options'] ?? null;
34+
$options = $options === null ?:
35+
array_replace_recursive($commonOptions ?? [], $options);
36+
37+
$dictsPath = $config['dicts_path'] ?? null;
38+
$dictsPath = ($dictsPath === null || $dictsStorage !== 'storage') ?:
39+
Storage::path($dictsPath);
40+
41+
return new Morphy($config['language'], $options, $dictsPath);
1742
}
1843

1944
public static function fromArray(array $configs, ?array $commonOptions = null): array

0 commit comments

Comments
 (0)