1. turanct
  2. wijs

Commits

turanct  committed 68d79e7

Add Settings ServiceProvider

  • Participants
  • Parent commits ef0a4c0
  • Branches master

Comments (0)

Files changed (1)

File models/Models/SettingsServiceProvider.php

View file
+<?php
+namespace Models;
+
+use Silex\Application;
+use Silex\ServiceProviderInterface;
+
+/**
+ * Settings Service Provider
+ */
+class SettingsServiceProvider implements ServiceProviderInterface {
+	/**
+	 * Register method
+	 */
+	public function register(Application $app) {
+		// Register the settings service
+		$app['settings'] = $app->protect(function () use ($app) {
+			// Create query
+			$sql = "SELECT * FROM 'settings'";
+
+			// Fetch data
+			$settingsRaw = $app['db']->fetchAll($sql);
+
+			// Prepare settings object
+			$settings = new \stdClass();
+
+			// Walk through data
+			foreach ($settingsRaw as $setting) {
+				$settings->{$setting['key']} = $setting['data'];
+			}
+
+			// Return
+			return $settings;
+		});
+
+
+		// Register the update service
+		$app['update'] = $app->protect(function ($settings) use ($app) {
+			// Prepare the query
+			$sql = "UPDATE settings SET data = ? WHERE key = ?";
+
+			// Walk through settings
+			foreach ($settings as $key => $value) {
+				// Update the db
+				$app['db']->executeUpdate($sql, array($value, $key));
+			}
+
+			// Return
+			return $settings;
+		});
+	}
+
+
+	/**
+	 * Boot method (not used)
+	 */
+	public function boot(Application $app) {
+
+	}
+}