1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: Muhannad Shelleh <muhannad.shelleh@live.com>
5+ * Date: 3/15/17
6+ * Time: 2:58 AM
7+ */
8+
9+ namespace ItvisionSy \Laravel \Modules ;
10+
11+ use DB ;
12+ use ItvisionSy \Laravel \Modules \Interfaces \KeyValueStoreInterface ;
13+
14+ class StoreHandler implements KeyValueStoreInterface
15+ {
16+
17+ protected static $ tableName = 'modules_storage ' ;
18+
19+ /**
20+ * @return Connection
21+ */
22+ public static function getConnection ()
23+ {
24+ $ connectionDriver = config ('modules.default_store_handler_connection ' , null ) ?: config ('database.default ' );
25+ /** @var Connection $connection */
26+ $ connection = DB ::connection ($ connectionDriver );
27+ return $ connection ;
28+ }
29+
30+ /**
31+ * @return mixed
32+ */
33+ public static function createTable ()
34+ {
35+ return static ::statement ("CREATE TABLE IF NOT EXISTS ` " . static ::$ tableName . "` (`key` VARCHAR(200) UNIQUE NOT NULL PRIMARY KEY, `value` VARCHAR(200) NULL); " );
36+ }
37+
38+ protected static function statement ($ query , array $ bindings = [])
39+ {
40+ return static ::getConnection ()->statement ($ query , $ bindings );
41+ }
42+
43+ protected static function select ($ query , array $ bindings = [])
44+ {
45+ return static ::getConnection ()->select ($ query , $ bindings );
46+ }
47+
48+ public function set ($ key , $ value = null )
49+ {
50+ return $ this ->statement ("INSERT OR REPLACE INTO ` " . static ::$ tableName . "` (`key`,`value`) VALUES (' " . addslashes ($ key ) . "',' " . addslashes ($ value ) . "') " );
51+ }
52+
53+ public function get ($ key , $ default = null )
54+ {
55+ $ result = $ this ->select ("SELECT `value` FROM ` " . static ::$ tableName . "` WHERE `key`=' " . addslashes ($ key ) . "' " );
56+ if (count ($ result )) {
57+ return $ result [0 ]->value ;
58+ }
59+ return $ default ;
60+ }
61+ }
0 commit comments