1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22
23 define('PHPMORPHY_SOURCE_FSA', 'fsa');
24 define('PHPMORPHY_SOURCE_DBA', 'dba');
25 define('PHPMORPHY_SOURCE_SQL', 'sql');
26
27 interface phpMorphy_Source_Interface {
28 function getValue($key);
29 }
30
31 class phpMorphy_Source_Fsa implements phpMorphy_Source_Interface {
32 protected
33 $fsa,
34 $root;
35
36 function __construct(phpMorphy_Fsa_Interface $fsa) {
37 $this->fsa = $fsa;
38 $this->root = $fsa->getRootTrans();
39 }
40
41 function getFsa() {
42 return $this->fsa;
43 }
44
45 function getValue($key) {
46 if(false === ($result = $this->fsa->walk($this->root, $key, true)) || !$result['annot']) {
47 return false;
48 }
49
50 return $result['annot'];
51 }
52 }
53
54 class phpMorphy_Source_Dba implements phpMorphy_Source_Interface {
55 const DEFAULT_HANDLER = 'db3';
56
57 protected $handle;
58
59 function __construct($fileName, $options = null) {
60 $this->handle = $this->openFile($fileName, $this->repairOptions($options));
61 }
62
63 function close() {
64 if(isset($this->handle)) {
65 dba_close($this->handle);
66 $this->handle = null;
67 }
68 }
69
70 static function getDefaultHandler() {
71 return self::DEFAULT_HANDLER;
72 }
73
74 protected function openFile($fileName, $options) {
75 if(false === ($new_filename = realpath($fileName))) {
76 throw new phpMorphy_Exception("Can`t get realpath for '$fileName' file");
77 }
78
79 $lock_mode = $options['lock_mode'];
80 $handler = $options['handler'];
81 $func = $options['persistent'] ? 'dba_popen' : 'dba_open';
82
83 if(false === ($result = $func($new_filename, "r$lock_mode", $handler))) {
84 throw new phpMorphy_Exception("Can`t open '$fileFile' file");
85 }
86
87 return $result;
88 }
89
90 protected function repairOptions($options) {
91 $defaults = array(
92 'lock_mode' => 'd',
93 'handler' => self::getDefaultHandler(),
94 'persistent' => false
95 );
96
97 return (array)$options + $defaults;
98 }
99
100 function getValue($key) {
101 return dba_fetch($key, $this->handle);
102 }
103 }
104
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-