1. Arjan Scherpenisse
  2. php-anymeta

Commits

Arjan Scherpenisse  committed 9ea138e

Added the AnymetaRegistry class, with the same api as the python-anymeta one.

  • Participants
  • Parent commits 9b7d7fb
  • Branches default

Comments (0)

Files changed (3)

File example-registry.php

View file
+<?php
+
+// minimal example of how to use the php-anymeta library.
+
+require_once 'src/AnymetaAPI.php';
+
+$client = AnyMetaAPI::from_registry('sysadmin@ixion.local');
+
+$thing = array('kind' => 'ARTICLE',
+               'text' => array('title' => 'Dit is een nieuw artikel.',
+                               'subtitle' => 'Wat een leuk artikel weer....',
+                               'intro' => 'Vandaag gaan we es wat vertellen over nieuwe dingen. Blabfldjsfdks',
+                               'language' => 'nl')
+    );
+
+$result = $client->doMethod("anymeta.thing.insert", array('data' => $thing));
+
+var_dump($result);
+
+?>

File src/AnymetaAPI.php

View file
     }
 
 
+    /*
+     * Create an AnyMeta instance by looking into the local key registry.
+     */
+    public static function from_registry ($key)
+    {
+        require_once 'AnymetaRegistry.php';
+        $registry = AnymetaRegistry::getInstance();
+        $entry = $registry->get($key);
+        extract($entry['oauth']);
+        return new AnyMetaAPI($entry['entrypoint'], $c_key, $c_sec, $t_key, $t_sec);
+    }
+
+
     /**
      * Call a method on Anymeta.
      */

File src/AnymetaRegistry.php

View file
+<?php
+/**
+ * Very basic Anymeta client.
+ *
+ * Loosely based upon the python-anymeta library at
+ * http://trac.mediamatic.nl/python-anymeta.
+ *
+ * @author Arjan Scherpenisse <arjan@mediamatic.nl>
+ *
+ * -----------------------------------------------------------------------------
+ * The MIT License
+ * 
+ * Copyright (c) 2010 Arjan Scherpenisse
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+
+class AnymetaRegistryException extends Exception
+{
+}
+
+
+class AnymetaRegistry
+{
+    private $registry;
+    private $cfgfile = NULL;
+
+    private static $instance;
+
+
+    public static function getInstance ()
+    {
+        if (!isset(self::$instance))
+        {
+            self::$instance = new AnymetaRegistry();
+        }
+        return self::$instance;
+    }
+
+    
+    public function __construct ($cfgfile = NULL)
+    {
+        $this->registry = array();
+        $this->load($cfgfile);
+    }
+
+    
+    public function load ($cfgfile)
+    {
+        if ($cfgfile)
+        {
+            $this->cfgfile = $cfgfile;
+        }
+        else
+        {
+            // guess
+            $userfile = getenv("HOME")."/.anymeta";
+            if (file_exists($userfile))
+            {
+                $this->cfgfile = $userfile;
+            }
+            else if (file_exists("/etc/anymeta.conf"))
+            {
+                $this->cfgfile = "/etc/anymeta.conf";
+            }
+            else
+            {
+                $fp = fopen($userfile, "w");
+                fclose($fp);
+                $this->cfgfile = $userfile;
+            }
+        }
+        $this->registry = parse_ini_file($this->cfgfile, true);
+    }
+
+
+    public function get ($key)
+    {
+        if (!isset($this->registry[$key]))
+        {
+            throw new AnymetaRegistryException('Unknown config section: ' . $key);
+        }
+        $r = $this->registry[$key];
+        return array('entrypoint' => $r['entrypoint'],
+                     'oauth' => array('c_key' => $r['c_key'],
+                                      'c_sec' => $r['c_sec'],
+                                      't_key' => $r['t_key'],
+                                      't_sec' => $r['t_sec']),
+                     'comment' => $r['comment']);
+    }
+
+    
+    public function set ($key, $entrypoint, $oauth, $comment="")
+    {
+        $entry = array('entrypoint' => $entrypoint,
+                       'comment' => $comment);
+        $entry = array_merge($entry, $oauth);
+        $this->registry[$key] = $entry;
+    }
+
+    
+    public function delete ($key)
+    {
+        unset($this->registry[$key]);
+    }
+
+    
+    public function save ()
+    {
+        $fp = fopen($this->cfgfile, "w");
+        foreach ($this->registry as $sect => $values)
+        {
+            fprintf($fp, "[%s]\n", $sect);
+            foreach ($values as $k => $v)
+            {
+                fprintf($fp, "%s = %s\n", $k, $v);
+            }
+            fwrite($fp, "\n");
+        }
+        fclose($fp);
+    }
+
+    
+    public function exists ($key)
+    {
+        return !empty($this->registry[$key]);
+    }
+
+    
+    public function register_interactive ($key, $entrypoint, $comment="")
+    {
+        throw new Exception('Not implemented');
+    }
+
+}