Skip to content

Commit 701275f

Browse files
Muhannad ShellehMuhannad Shelleh
authored andcommitted
2 parents 342840c + abaf052 commit 701275f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

publishes/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
5+
];

src/StoreHandlers/StoreHandler.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)