summaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-05-02 17:25:05 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-05-02 17:25:05 +0200
commit9c9625b0f6520f80996e17304805d8bae421f44c (patch)
tree4d2e99fdab4edf3a45d3570f534f11678f7f19c4 /config
parentc9aead3a88405e7051529c8c4e26c69b7f6369cd (diff)
add parser for app.json file, parsing jobs and hooks still left
Diffstat (limited to 'config')
-rw-r--r--config/appconfig.php240
-rw-r--r--config/dependencyexception.php28
2 files changed, 268 insertions, 0 deletions
diff --git a/config/appconfig.php b/config/appconfig.php
new file mode 100644
index 000000000..0d4ea6537
--- /dev/null
+++ b/config/appconfig.php
@@ -0,0 +1,240 @@
+<?php
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright Alessandro Cosentino 2012
+ * @copyright Bernhard Posselt 2012, 2014
+ */
+
+namespace OCA\News\Config;
+
+use OCP\AppFramework\IApi;
+use OCP\BackgroundJob\IJob;
+use OCP\BackgroundJob\IJobList;
+use OCP\INavigationManager;
+use OCP\IL10N;
+use OCP\IURLGenerator;
+
+
+// Used to parse app.json file, should be in core at some point
+class AppConfig {
+
+ public $config;
+
+ private $navigationManager;
+ private $urlGenerator;
+ private $phpVersion;
+ private $ownCloudVersion;
+ private $installedApps;
+ private $installedExtensions;
+ private $databaseType;
+
+ /**
+ * TODO: External deps that are needed:
+ * - add jobs
+ * - connect to hooks
+ */
+ public function __construct(INavigationManager $navigationManager,
+ IL10N $l10n,
+ IURLGenerator $urlGenerator,
+ $phpVersion,
+ $ownCloudVersion,
+ $installedApps,
+ $installedExtensions,
+ $databaseType) {
+ $this->navigationManager = $navigationManager;
+ $this->l10n = $l10n;
+ $this->urlGenerator = $urlGenerator;
+ $this->ownCloudVersion = $ownCloudVersion;
+ $this->phpVersion = $phpVersion;
+ $this->installedApps = $installedApps;
+ $this->installedExtensions = $installedExtensions;
+ $this->databaseType = $databaseType;
+ }
+
+
+ /**
+ * @param string|array $data path to the config file or an array with the config
+ * @throws \OCA\News\Config\DependencyException if a required lib or version
+ * is not satisfied by the current installation
+ */
+ public function load($data) {
+ if(is_array($data)) {
+ $this->config = $data;
+ } else {
+ $json = file_get_contents($data);
+ $this->config = json_decode($json, true);
+ }
+
+ $this->testDependencies();
+ $this->parseNavigation();
+ $this->parseJobs();
+ $this->parseHooks();
+ }
+
+
+ /**
+ * Parses the navigation and creates a navigation entry if needed
+ */
+ private function parseNavigation() {
+ // if key is missing, dont create a navigation
+ if(array_key_exists('navigation', $this->config)) {
+ $nav = $this->config['navigation'];
+
+ // add defaults
+ $defaults = array(
+ 'id' => $this->config['id'],
+ 'route' => $this->config['id'] . '.page.index',
+ 'order' => 10,
+ 'icon' => 'app.svg',
+ 'name' => $this->config['name']
+ );
+
+ foreach($defaults as $key => $value) {
+ if(!array_key_exists($key, $nav)) {
+ $nav[$key] = $value;
+ }
+ }
+
+
+ $navConfig = array(
+ 'id' => $nav['id'],
+ 'order' => $nav['order']
+ );
+
+ $navConfig['name'] = $this->l10n->t($nav['name']);
+ $navConfig['href'] = $this->urlGenerator->linkToRoute($nav['route']);
+ $navConfig['icon'] = $this->urlGenerator->imagePath($nav['id'],
+ $nav['icon']);
+
+ $this->navigationManager->add($navConfig);
+ }
+
+ }
+
+
+ private function parseJobs() {
+
+ }
+
+
+ private function parseHooks() {
+
+ }
+
+
+ /**
+ * Validates all dependencies that the app has
+ * @throws \OCA\News\DependencyException if one version is not satisfied
+ */
+ private function testDependencies() {
+ if(array_key_exists('dependencies', $this->config)) {
+
+ $deps = $this->config['dependencies'];
+
+ $msg = '';
+
+ if(array_key_exists('php', $deps)) {
+ $msg .= $this->requireVersion($this->phpVersion, $deps['php'],
+ 'PHP');
+ }
+
+ if(array_key_exists('owncloud', $deps)) {
+ $msg .= $this->requireVersion($this->ownCloudVersion,
+ $deps['owncloud'], 'ownCloud');
+ }
+
+ if(array_key_exists('apps', $deps)) {
+ foreach ($deps['apps'] as $app => $versions) {
+ if(array_key_exists($app, $this->installedApps)) {
+ $msg .= $this->requireVersion($this->installedApps[$app],
+ $versions, 'App ' . $app);
+ } else {
+ $msg .= 'ownCloud app ' . $app . ' required but not installed';
+ }
+ }
+ }
+
+ if(array_key_exists('libs', $deps)) {
+ foreach ($deps['libs'] as $lib => $versions) {
+ if(array_key_exists($lib, $this->installedExtensions)) {
+ $msg .= $this->requireVersion($this->installedExtensions[$lib],
+ $versions, 'PHP extension ' . $lib);
+ } else {
+ $msg .= 'PHP extension ' . $lib . ' required but not installed';
+ }
+ }
+ }
+
+
+ if($msg !== '') {
+ throw new DependencyException($msg);
+ }
+
+ }
+ }
+
+
+ /**
+ * Compares a version with a version requirement string
+ * @param string $actual the actual version that is there
+ * @param string $required a version requirement in the form of
+ * <=5.3,>4.5 versions are seperated with a comma
+ * @param string $versionType a description of the string that is prepended
+ * to the error message
+ * @return an error message if the version is not met, empty string if ok
+ */
+ private function requireVersion($actual, $required, $versionType) {
+ $requiredVersions = $this->splitVersions($required);
+
+ foreach($requiredVersions as $version) {
+ // accept * as wildcard for any version
+ if($version['version'] === '*') {
+ continue;
+ }
+ if(!version_compare($actual, $version['version'], $version['operator'])) {
+ return $versionType . ' Version not satisfied: ' . $version['operator'] .
+ $version['version'] . ' required but found ' . $actual . '\n';
+ }
+ }
+
+ return '';
+ }
+
+
+ /**
+ * Versions can be seperated by a comma so split them
+ * @param string $versions a version requirement in the form of
+ * <=5.3,>4.5 versions are seperated with a comma
+ * @return array of arrays with key=version value=operator
+ */
+ private function splitVersions($versions) {
+ $result = array();
+ $versions = explode(',', $versions);
+
+ foreach($versions as $version) {
+ preg_match('/^(?<operator><|<=|>=|>|<>)?(?<version>.*)$/', $version, $matches);
+ if($matches['operator'] !== '') {
+ $required = array(
+ 'version' => $matches['version'],
+ 'operator' => $matches['operator'],
+ );
+ } else {
+ $required = array(
+ 'version' => $matches['version'],
+ 'operator' => '==',
+ );
+ }
+ $result[] = $required;
+ }
+
+ return $result;
+ }
+
+
+} \ No newline at end of file
diff --git a/config/dependencyexception.php b/config/dependencyexception.php
new file mode 100644
index 000000000..690d187c3
--- /dev/null
+++ b/config/dependencyexception.php
@@ -0,0 +1,28 @@
+<?php
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright Alessandro Cosentino 2012
+ * @copyright Bernhard Posselt 2012, 2014
+ */
+
+namespace OCA\News\Config;
+
+class DependencyException extends \Exception {
+
+
+ /**
+ * Constructor
+ * @param string $msg the error message
+ */
+ public function __construct($msg){
+ parent::__construct($msg);
+ }
+
+
+} \ No newline at end of file
4'>754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895