1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  GitHub
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 use Joomla\Registry\Registry;
 13 
 14 /**
 15  * Joomla Platform class for interacting with a GitHub server instance.
 16  *
 17  * @property-read  JGithubPackageActivity       $activity       GitHub API object for activity.
 18  * @property-read  JGithubPackageAuthorization  $authorization  GitHub API object for authorizations.
 19  * @property-read  JGithubPackageData           $data           GitHub API object for data.
 20  * @property-read  JGithubPackageGists          $gists          GitHub API object for gists.
 21  * @property-read  JGithubPackageGitignore      $gitignore      GitHub API object for gitignore.
 22  * @property-read  JGithubPackageIssues         $issues         GitHub API object for issues.
 23  * @property-read  JGithubPackageMarkdown       $markdown       GitHub API object for markdown.
 24  * @property-read  JGithubPackageOrgs           $orgs           GitHub API object for orgs.
 25  * @property-read  JGithubPackagePulls          $pulls          GitHub API object for pulls.
 26  * @property-read  JGithubPackageRepositories   $repositories   GitHub API object for repositories.
 27  * @property-read  JGithubPackageSearch         $search         GitHub API object for search.
 28  * @property-read  JGithubPackageUsers          $users          GitHub API object for users.
 29  *
 30  * @property-read  JGithubRefs        $refs        Deprecated GitHub API object for referencess.
 31  * @property-read  JGithubForks       $forks       Deprecated GitHub API object for forks.
 32  * @property-read  JGithubCommits     $commits     Deprecated GitHub API object for commits.
 33  * @property-read  JGithubMilestones  $milestones  Deprecated GitHub API object for commits.
 34  * @property-read  JGithubStatuses    $statuses    Deprecated GitHub API object for commits.
 35  * @property-read  JGithubAccount     $account     Deprecated GitHub API object for account references.
 36  * @property-read  JGithubHooks       $hooks       Deprecated GitHub API object for hooks.
 37  * @property-read  JGithubMeta        $meta        Deprecated GitHub API object for meta.
 38  *
 39  * @since       11.3
 40  * @deprecated  4.0  Use the `joomla/github` package via Composer instead
 41  */
 42 class JGithub
 43 {
 44     /**
 45      * @var    Registry  Options for the GitHub object.
 46      * @since  11.3
 47      */
 48     protected $options;
 49 
 50     /**
 51      * @var    JGithubHttp  The HTTP client object to use in sending HTTP requests.
 52      * @since  11.3
 53      */
 54     protected $client;
 55 
 56     /**
 57      * @var    array  List of known packages.
 58      * @since  3.3 (CMS)
 59      */
 60     protected $packages = array(
 61         'activity',
 62         'authorization',
 63         'data',
 64         'gists',
 65         'gitignore',
 66         'issues',
 67         'markdown',
 68         'orgs',
 69         'pulls',
 70         'repositories',
 71         'users',
 72     );
 73 
 74     /**
 75      * @var    array  List of known legacy packages.
 76      * @since  3.3 (CMS)
 77      */
 78     protected $legacyPackages = array('refs', 'forks', 'commits', 'milestones', 'statuses', 'account', 'hooks', 'meta');
 79 
 80     /**
 81      * Constructor.
 82      *
 83      * @param   Registry     $options  GitHub options object.
 84      * @param   JGithubHttp  $client   The HTTP client object.
 85      *
 86      * @since   11.3
 87      */
 88     public function __construct(Registry $options = null, JGithubHttp $client = null)
 89     {
 90         $this->options = isset($options) ? $options : new Registry;
 91         $this->client  = isset($client) ? $client : new JGithubHttp($this->options);
 92 
 93         // Setup the default API url if not already set.
 94         $this->options->def('api.url', 'https://api.github.com');
 95     }
 96 
 97     /**
 98      * Magic method to lazily create API objects
 99      *
100      * @param   string  $name  Name of property to retrieve
101      *
102      * @throws RuntimeException
103      *
104      * @since   11.3
105      * @return  JGithubObject  GitHub API object (gists, issues, pulls, etc).
106      */
107     public function __get($name)
108     {
109         if (false == in_array($name, $this->packages))
110         {
111             // Check for a legacy class
112             if (in_array($name, $this->legacyPackages))
113             {
114                 if (false == isset($this->$name))
115                 {
116                     $className = 'JGithub' . ucfirst($name);
117 
118                     $this->$name = new $className($this->options, $this->client);
119                 }
120 
121                 return $this->$name;
122             }
123 
124             throw new RuntimeException(sprintf('%1$s - Unknown package %2$s', __METHOD__, $name));
125         }
126 
127         if (false == isset($this->$name))
128         {
129             $className = 'JGithubPackage' . ucfirst($name);
130 
131             $this->$name = new $className($this->options, $this->client);
132         }
133 
134         return $this->$name;
135     }
136 
137     /**
138      * Get an option from the JGitHub instance.
139      *
140      * @param   string  $key  The name of the option to get.
141      *
142      * @return  mixed  The option value.
143      *
144      * @since   11.3
145      */
146     public function getOption($key)
147     {
148         return $this->options->get($key);
149     }
150 
151     /**
152      * Set an option for the JGitHub instance.
153      *
154      * @param   string  $key    The name of the option to set.
155      * @param   mixed   $value  The option value to set.
156      *
157      * @return  JGitHub  This object for method chaining.
158      *
159      * @since   11.3
160      */
161     public function setOption($key, $value)
162     {
163         $this->options->set($key, $value);
164 
165         return $this;
166     }
167 }
168