Skip to content

Commit 785454a

Browse files
committed
Use multiple morphy object according to config, make service provider defferable
1 parent 73a57b9 commit 785454a

File tree

5 files changed

+116
-31
lines changed

5 files changed

+116
-31
lines changed

config/config.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<?php
22

33
return [
4-
// phpMorphy language
5-
'language' => SEOService2020\Morphy\Morphy::russianLang,
6-
// phpMorphy options
7-
'options' => [
4+
'common_options' => [
85
'storage' => phpMorphy::STORAGE_FILE,
96
'predict_by_suffix' => true,
107
'predict_by_db' => true,
118
'graminfo_as_text' => true,
129
],
13-
// when null, default dicts wil be loaded
14-
'dicts_path' => null,
10+
11+
'morphies' => [
12+
// order determines search predecence when facade called directly
13+
[
14+
// by this name specific morphy can be accessed thorough Morphy::morphy method
15+
'name' => 'ru',
16+
'language' => SEOService2020\Morphy\Morphy::russianLang,
17+
// if [] specified, default options will be used
18+
// to use common options, specify null
19+
'options' => [],
20+
// when null specified, default morphy dicts path will be used
21+
'dicts_path' => null,
22+
],
23+
],
1524
];

src/Factory.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace SEOService2020\Morphy;
4+
5+
6+
class Factory
7+
{
8+
public static function makeMorphy(array $config, ?array $defaultOptions = null): Morphy
9+
{
10+
return new Morphy(
11+
$config['language'],
12+
($config['options'] ?? null) === null ?
13+
null :
14+
array_replace_recursive($defaultOptions ?? [], $config['options'] ?? []),
15+
$config['dicts_path'] ?? null
16+
);
17+
}
18+
19+
public static function fromArray(array $configs, ?array $defaultOptions = null): array
20+
{
21+
$morphies = [];
22+
foreach ($configs as $config) {
23+
$morphies[$config['name']] = self::makeMorphy($config, $defaultOptions);
24+
}
25+
return $morphies;
26+
}
27+
}

src/Morphy.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,4 @@ public function __construct(
5555

5656
parent::__construct($this->dictsPath, $this->language, $this->options);
5757
}
58-
59-
public function morphy(): self
60-
{
61-
return $this;
62-
}
63-
64-
public function withLanguage(string $language): self
65-
{
66-
return new self($language, $this->options, $this->dictsPath);
67-
}
68-
69-
public function withOptions(array $options): self
70-
{
71-
return new self($this->language, $options, $this->dictsPath);
72-
}
73-
74-
public function withDicts(?string $dictsPath = null): self
75-
{
76-
return new self($this->language, $this->options, $dictsPath);
77-
}
7858
}

src/MorphyManager.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace SEOService2020\Morphy;
4+
5+
use BadMethodCallException;
6+
use InvalidArgumentException;
7+
use phpMorphy;
8+
9+
10+
class MorphyManager
11+
{
12+
protected $morphies;
13+
14+
public function __construct(array $morphies)
15+
{
16+
$this->morphies = $morphies;
17+
}
18+
19+
public function morphies($language = null): array
20+
{
21+
if ($language === null) {
22+
return $this->morphies;
23+
}
24+
25+
return array_filter(
26+
$this->morphies,
27+
function ($morphy) use ($language) {
28+
return $morphy->getLocale() == $language;
29+
}
30+
);
31+
}
32+
33+
public function morphy(string $name): ?Morphy
34+
{
35+
return isset($this->morphies[$name]) ? $this->morphies[$name] : null;
36+
}
37+
38+
public function __call($name, $arguments)
39+
{
40+
if (count($arguments) < 1) {
41+
throw new BadMethodCallException(
42+
"Too few arguments provided: at least morphy name expected"
43+
);
44+
}
45+
46+
$morphyName = array_shift($arguments);
47+
if (!array_key_exists($morphyName, $this->morphies)) {
48+
throw new InvalidArgumentException("Unknown morphy: $morphyName");
49+
}
50+
51+
return call_user_func_array([$this->morphies[$morphyName], $name], $arguments);
52+
}
53+
54+
public static function __callStatic($name, $arguments)
55+
{
56+
return phpMorphy::$name(...$arguments);
57+
}
58+
}

src/MorphyServiceProvider.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace SEOService2020\Morphy;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Contracts\Support\DeferrableProvider;
67

78

8-
class MorphyServiceProvider extends ServiceProvider
9+
class MorphyServiceProvider extends ServiceProvider implements DeferrableProvider
910
{
1011
/**
1112
* Bootstrap the application services.
@@ -31,11 +32,21 @@ public function register()
3132
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'morphy');
3233

3334
$this->app->bind('morphy', function () {
34-
return new Morphy(
35-
config('morphy.language'),
36-
config('morphy.options'),
37-
config('morphy.dicts_path')
35+
return new MorphyManager(
36+
Factory::fromArray(
37+
config('morphy.morphies'),
38+
config('morphy.default_options')
39+
)
3840
);
3941
});
4042
}
43+
44+
/**
45+
* Get the services provided by the provider.
46+
*
47+
* @return array
48+
*/
49+
public function provides() {
50+
return ['morphy'];
51+
}
4152
}

0 commit comments

Comments
 (0)