Version: current

      1 <?php
      2 /**
      3  * This file is part of phpMorphy library
      4  *
      5  * Copyright c 2007-2008 Kamaev Vladimir <heromantor@users.sourceforge.net>
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Lesser General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Lesser General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this library; if not, write to the
     19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     20  * Boston, MA 02111-1307, USA.
     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. -