Source
xxxxxxxxxx
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
class API {
/**
* API wrapper that all of the calls will go through.
*
* @var CApiWrapper
*/
private static $wrapper;
/**
* Factory for creating API services.
*
* @var CRegistryFactory
*/
private static $apiServiceFactory;
/**
* Sets the API wrapper.
*
* @param CApiWrapper|null $wrapper
*/
public static function setWrapper(?CApiWrapper $wrapper = null) {
self::$wrapper = $wrapper;
}
/**
* Set the service factory.
*
* @param CRegistryFactory $factory
*/
public static function setApiServiceFactory(CRegistryFactory $factory) {
self::$apiServiceFactory = $factory;
}
/**
* Returns the API wrapper.
*
* @return CApiWrapper|null
*/
public static function getWrapper() {
return self::$wrapper;
}
/**
* Returns an object that can be used for making API calls. If a wrapper is used, returns a CApiWrapper,
* otherwise - returns a CApiService object.
*
* @param $name
*
* @return CApiWrapper|CApiService
*/
public static function getApi($name) {
if (self::$wrapper) {
self::$wrapper->api = $name;
return self::$wrapper;
}
else {
return self::getApiService($name);
}
}
/**
* Returns the CApiInstance object for the requested API.
*
* NOTE: This method must only be called from other CApiService objects.
*
* @param string $name
*
* @return CApiService
*/
public static function getApiService($name = null) {
return self::$apiServiceFactory->getObject($name ? $name : 'api');
}