From 2a1d4d9b905fb9b47428218d86d55ab5525d6a24 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Mon, 30 Oct 2017 16:20:15 -0300 Subject: [PATCH] Add basic support for Travis CI --- .gitignore | 3 +- .travis.yml | 122 ++++++++ README.md | 58 ++-- build.php | 253 ---------------- build/build.php | 269 ++++++++++++++++++ {inc => build}/config.inc.php | 25 +- {inc => build}/replacePlaceholders.func.php | 2 - .../templates}/abstract.tpl.php | 38 +-- .../templates}/concrete.tpl.php | 4 - build/templates/exception.tpl.php | 51 ++++ composer.json | 15 +- phpunit.xml.dist | 29 ++ src/.gitkeep | 0 tests/ZabbixApiTest.php | 39 +++ 14 files changed, 588 insertions(+), 320 deletions(-) create mode 100644 .travis.yml delete mode 100644 build.php create mode 100644 build/build.php rename {inc => build}/config.inc.php (76%) rename {inc => build}/replacePlaceholders.func.php (99%) rename {templates => build/templates}/abstract.tpl.php (94%) rename {templates => build/templates}/concrete.tpl.php (97%) create mode 100644 build/templates/exception.tpl.php create mode 100644 phpunit.xml.dist create mode 100644 src/.gitkeep create mode 100644 tests/ZabbixApiTest.php diff --git a/.gitignore b/.gitignore index 264b736..536687c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -test.php +/vendor/ +/src/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9ede92a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,122 @@ +language: php + +dist: bionic +sudo: false + +git: + depth: 1 + +php: + - 7.1 + - 7.2 + - 7.3 + - 7.4 + +env: + - ZABBIX_VERSION=2.4.0 + - ZABBIX_VERSION=2.4.8 + - ZABBIX_VERSION=3.0.0 + - ZABBIX_VERSION=3.4.15 + - ZABBIX_VERSION=4.0.0 + - ZABBIX_VERSION=4.4.4 + +jobs: + fast_finish: true + include: + - php: 5.3 + dist: precise + env: ZABBIX_VERSION=2.4.0 + - php: 5.3 + dist: precise + env: ZABBIX_VERSION=2.4.8 + - php: 5.4 + dist: trusty + env: ZABBIX_VERSION=2.4.0 + - php: 5.4 + dist: trusty + env: ZABBIX_VERSION=2.4.8 + - php: 5.4 + dist: precise + env: ZABBIX_VERSION=3.0.0 + - php: 5.4 + dist: precise + env: ZABBIX_VERSION=3.4.15 + - php: 5.4 + dist: precise + env: ZABBIX_VERSION=4.0.0 + - php: 5.4 + dist: precise + env: ZABBIX_VERSION=4.4.4 + - php: 5.5 + dist: trusty + env: ZABBIX_VERSION=2.4.0 + - php: 5.5 + dist: trusty + env: ZABBIX_VERSION=2.4.8 + - php: 5.5 + dist: precise + env: ZABBIX_VERSION=3.0.0 + - php: 5.5 + dist: precise + env: ZABBIX_VERSION=3.4.15 + - php: 5.5 + dist: precise + env: ZABBIX_VERSION=4.0.0 + - php: 5.5 + dist: precise + env: ZABBIX_VERSION=4.4.4 + - php: 5.6 + dist: trusty + env: ZABBIX_VERSION=2.4.0 + - php: 5.6 + dist: trusty + env: ZABBIX_VERSION=2.4.8 + - php: 5.6 + dist: precise + env: ZABBIX_VERSION=3.0.0 + - php: 5.6 + dist: precise + env: ZABBIX_VERSION=3.4.15 + - php: 5.6 + dist: precise + env: ZABBIX_VERSION=4.0.0 + - php: 5.6 + dist: precise + env: ZABBIX_VERSION=4.4.4 + - php: 7.0 + dist: trusty + env: ZABBIX_VERSION=2.4.0 + - php: 7.0 + dist: trusty + env: ZABBIX_VERSION=2.4.8 + - php: 7.0 + dist: precise + env: ZABBIX_VERSION=3.0.0 + - php: 7.0 + dist: precise + env: ZABBIX_VERSION=3.4.15 + - php: 7.0 + dist: precise + env: ZABBIX_VERSION=4.0.0 + - php: 7.0 + dist: precise + env: ZABBIX_VERSION=4.4.4 + +cache: + directories: + - $HOME/.composer + +before_install: + - | + # General configuration + stty cols 120 + mkdir -p /opt/zabbix + wget -qO- https://repo.zabbix.com/zabbix/$(echo $ZABBIX_VERSION | cut -d. -f-2)/ubuntu/pool/main/z/zabbix/zabbix_$ZABBIX_VERSION.orig.tar.gz | tar xvz -C /opt/zabbix + ln -s /opt/zabbix/zabbix-$ZABBIX_VERSION/frontends/php/ /opt/zabbix/frontend + +install: + - php ./build/build.php + - composer install --prefer-dist --optimize-autoloader --classmap-authoritative --no-interaction --no-scripts + +script: + - ./vendor/bin/phpunit diff --git a/README.md b/README.md index 714ff58..235bad0 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ ## PhpZabbixApi +[![Build Status](https://travis-ci.org/confirm/PhpZabbixApi.svg?branch=master)](https://travis-ci.org/confirm/PhpZabbixApi) + > __I'M LOOKING FOR CONTRIBUTORS, [CLICK HERE FOR MORE INFORMATIONS](https://github.com/confirm/PhpZabbixApi/issues/28)__ ### About PhpZabbixApi is an open-source PHP class library to communicate with the Zabbix™ JSON-RPC API. -Because PhpZabbixApi is generated directly from the origin Zabbix™ 2.0 PHP front-end source code / files, each real Zabbix™ JSON-RPC API method is implemented (hard-coded) directly as an own PHP method. This means PhpZabbixApi is IDE-friendly, because you've a PHP method for each API method, and there are no PHP magic functions or alike. +Because PhpZabbixApi is generated directly from the origin Zabbix™ PHP front-end source code / files, each real Zabbix™ JSON-RPC API method is implemented (hard-coded) directly as an own PHP method. This means PhpZabbixApi is IDE-friendly, because you've a PHP method for each API method, and there are no PHP magic functions or alike. ### License @@ -30,19 +32,19 @@ You might want to point `PATH_ZABBIX` to your Zabbix™ installation directory. If you setup everything correctly, you should be able to create the library by executing: ```bash -php build.php +php build/build.php ``` -There are also pre-built libraries available in the `build/` directory, if you don't want to build it yourself. +There are also pre-built libraries available in the `src/` directory, if you don't want to build it yourself. ### Download -[Download a release](https://github.com/domibarton/PhpZabbixApi/releases) and extract the pre-built PHP library from the `build/` directory. +[Download a release](https://github.com/domibarton/PhpZabbixApi/releases) and extract the pre-built PHP library from the `src/` directory. Make sure you've downloaded the following files and stored them in the same directory: -* `ZabbixApi.class.php` -* `ZabbixApiAbstract.class.php` +* `ZabbixApi.php` +* `AbstractZabbixApi.php` For example: @@ -50,8 +52,8 @@ For example: my_application ├── index.php └── lib - ├── ZabbixApiAbstract.class.php - └── ZabbixApi.class.php + ├── AbstractZabbixApi.php + └── ZabbixApi.php ``` ### Composer @@ -66,15 +68,15 @@ All [tagged](https://github.com/domibarton/PhpZabbixApi/tags) versions can be in ``` -composer require 'confirm-it-solutions/php-zabbix-api:2.2.2' -composer require 'confirm-it-solutions/php-zabbix-api:2.4.2' +composer require 'confirm-it-solutions/php-zabbix-api:^2.2' +composer require 'confirm-it-solutions/php-zabbix-api:^2.4' ``` If you're looking for more "bleeding-edge" versions (e.g. for testing), then you could also use [branches](https://github.com/confirm-it-solutions/PhpZabbixApi/branches): ``` -composer require 'confirm-it-solutions/php-zabbix-api:2.2.*@dev' -composer require 'confirm-it-solutions/php-zabbix-api:2.4.*@dev' +composer require 'confirm-it-solutions/php-zabbix-api:2.2@dev' +composer require 'confirm-it-solutions/php-zabbix-api:2.4@dev' ``` ## Using the thing @@ -101,34 +103,32 @@ dcheck.isWritable dcheckIsWritable() By default there are only 2 classes defined: ``` -ZabbixApiAbstract +AbstractZabbixApi └── ZabbixApi ``` If you want to customize or extend the library, you might want to do that in the `ZabbixApi` class. -Out of the box, `ZabbixApi` is an empty class inherited from `ZabbixApiAbstract`. +Out of the box, `ZabbixApi` is an empty class inherited from `AbstractZabbixApi`. -By customizing only `ZabbixApi`, you're able to update `ZabbixApiAbstract` (the build) at any time, without merging your customizations manually. +By customizing only `ZabbixApi`, you're able to update `AbstractZabbixApi` (the build) at any time, without merging your customizations manually. ### Basic usage -To use the PhpZabbixApi you just have to load `ZabbixApi.class.php`, create a new `ZabbixApi` instance, and you're ready to go: +To use the PhpZabbixApi you just have to load `ZabbixApi.php`, create a new `ZabbixApi` instance, and you're ready to go: ```php getMessage(); } @@ -153,8 +153,9 @@ Here's a simple request to fetch all defined graphs via [graph.get API method](h $graphs = $api->graphGet(); // print all graph IDs - foreach($graphs as $graph) + foreach ($graphs as $graph) { echo $graph->graphid."\n"; + } ``` ### Request with parameters @@ -170,8 +171,9 @@ Here's an example to fetch all CPU graphs via [graph.get API method](https://www )); // print graph ID with graph name - foreach($cpuGraphs as $graph) + foreach ($cpuGraphs as $graph) { printf("id:%d name:%s\n", $graph->graphid, $graph->name); + } ``` ### Define default parameters @@ -191,8 +193,9 @@ You can do that by defining the parameters in an array via `setDefaultParams()`: )); // print graph ID with graph name - foreach($cpuGraphs as $graph) + foreach ($cpuGraphs as $graph) { printf("id:%d name:%s\n", $graph->graphid, $graph->name); + } ``` ### Get associative / un-indexed array @@ -208,8 +211,9 @@ Here's an example to fetch all graphs in an associative array, with the graph's $graphs = $api->graphGet(array(), 'name'); // print graph ID with graph name - if(array_key_exists('CPU Load Zabbix Server', $graphs)) + if (array_key_exists('CPU Load Zabbix Server', $graphs)) { echo 'CPU Load graph exists'; - else + } else { echo 'Could not find CPU Load graph'; + } ``` diff --git a/build.php b/build.php deleted file mode 100644 index b2d462e..0000000 --- a/build.php +++ /dev/null @@ -1,253 +0,0 @@ -. - * - * @copyright GNU General Public License - * @author confirm IT solutions GmbH, Rathausstrase 14, CH-6340 Baar - */ - - -/* - * Load required files. - */ - - require 'inc/config.inc.php'; - require 'inc/replacePlaceholders.func.php'; - -/* - * Define some pathes and do some sanity checks for existence of the pathes. - */ - - if(!is_dir(PATH_ZABBIX)) - die('ERROR: Zabbix path "'.PATH_ZABBIX.'" is not a directory! Please check the PATH_ZABBIX configuration constant.'); - - // load Zabbix internal constants, to access ZABBIX_API_VERSION - require PATH_ZABBIX.'/include/defines.inc.php'; - - /** - * @brief Path to the API.php class file of the Zabbix PHP front-end. - * - * This class file will be used, to determine all available API classes. - */ - - define('PATH_ZABBIX_API_CLASS_FILE', PATH_ZABBIX.'/include/classes/api/API.php'); - - if(!file_exists(PATH_ZABBIX_API_CLASS_FILE)) - die('ERROR: API class file "'.PATH_ZABBIX_API_CLASS_FILE.'" not found! Please check the PATH_ZABBIX_API_CLASS_FILE configuration constant'); - - /** - * @brief Path to the api/classes/ directory of the Zabbix PHP front-end. - * - * This directory and the contained class files will be used, to determine all - * available methods for each API class. - */ - - if(version_compare(ZABBIX_API_VERSION, '2.4') >= 0) - define('PATH_ZABBIX_API_CLASSES_DIRECTORY', PATH_ZABBIX.'/include/classes/api/services'); - else - define('PATH_ZABBIX_API_CLASSES_DIRECTORY', PATH_ZABBIX.'/api/classes'); - - if(!is_dir(PATH_ZABBIX_API_CLASSES_DIRECTORY)) - die('ERROR: API class directory "'.PATH_ZABBIX_API_CLASSES_DIRECTORY.'" not found!'); - -/* - * Initialize. - */ - - // send HTTP header - header('Content-type: text/plain; charset=utf-8'); - - // set template placeholders - $templatePlaceholders = array( - 'CLASSNAME_ABSTRACT' => CLASSNAME_ABSTRACT, - 'CLASSNAME_CONCRETE' => CLASSNAME_CONCRETE, - 'FILENAME_ABSTRACT' => FILENAME_ABSTRACT, - 'FILENAME_CONCRETE' => FILENAME_CONCRETE - ); - -/* - * Create class-map class. - * - * Create a new class and extend it from the origin Zabbix classes, so that we - * can fetch the class map directly from the Zabbix classes without defining - * it here. - * - * There are some differences between the Zabbix versions: - * - * < 2.4: The class map is stored as a static property directly in the - * origin API class. - * - * >= 2.4: The class map is stored as an instance property in the - * origin CApiServiceFactory class. - */ - - // load API - require PATH_ZABBIX_API_CLASS_FILE; - - // create new class to fetch class map for API classes - if(version_compare(ZABBIX_API_VERSION, '2.4') >= 0) - { - require PATH_ZABBIX.'/include/classes/core/CRegistryFactory.php'; - require PATH_ZABBIX.'/include/classes/api/CApiServiceFactory.php'; - require PATH_ZABBIX.'/include/classes/api/CApiService.php'; - - class ZabbixApiClassMap extends CApiServiceFactory - { - public function getClassMap() - { - $classMap = $this->objects; - return $classMap; - } - } - } - else - { - require PATH_ZABBIX.'/include/classes/api/CZBXAPI.php'; - class ZabbixApiClassMap extends API - { - public function getClassMap() - { - return self::$classMap; - } - } - } - -/* - * Register SPL autoloader. - * - * The API class files always inherit from other classes. Most of the classes - * inherit from the CZBXAPI class, but there are a bunch of classes which - * are extended by other API classes. - * - * So that we don't have to "follow" the right order on loading API class files, - * we're register an API autoloader right here. - * - * Later the get_class_methods() function will automatically invoke this - * autoloader. - */ - - function __autoload($className) - { - require PATH_ZABBIX_API_CLASSES_DIRECTORY.'/'.$className.'.php'; - } - -/* - * Build API array. - * - * Just loop through all available API classes and find all defined methods in - * these classes. - */ - - // initialze API array - $apiArray = array(); - - // Create new instance for API class map. - $apiClassMap = new ZabbixApiClassMap(); - - // loop through class map - foreach($apiClassMap->getClassMap() as $resource => $class) - { - // add resource to API array - $apiArray[$resource] = array(); - - // create new reflection class - $reflection = new ReflectionClass($class); - - // loop through defined methods - foreach($reflection->getMethods(ReflectionMethod::IS_PUBLIC & ~ReflectionMethod::IS_STATIC) as $method) - { - // add action to API array - if( $method->class != 'CZBXAPI' - && !($resource == 'user' && $method->name == 'login') - && !($resource == 'user' && $method->name == 'logout') - && !$method->isConstructor() - && !$method->isDestructor() - && !$method->isAbstract() - ) - $apiArray[$resource][] = $method->name; - } - - } - -/* - * Build abstract template. - */ - - // get template - if(!$template = file_get_contents(PATH_TEMPLATES.'/abstract.tpl.php')) - die(); - - // fetch API method block - preg_match('/(.*)(.*)(.*)/s', $template, $matches); - - // sanity check - if(count($matches) != 4) - die('Template "'.PATH_TEMPLATES.'"/abstract.tpl.php parsing failed!'); - - // initialize variable for API methods - $apiMethods = ''; - - // build API methods - foreach($apiArray as $resource => $actions) - { - foreach($actions as $action) - { - $methodPlaceholders = array( - 'API_METHOD' => $resource.'.'.$action, - 'PHP_METHOD' => $resource.ucfirst($action) - ); - $apiMethods .= replacePlaceholders($matches[2], $methodPlaceholders); - } - } - - // build file content - $fileContent = replacePlaceholders($matches[1].$apiMethods.$matches[3], $templatePlaceholders); - - // write abstract class - if(!file_put_contents(PATH_BUILD.'/'.FILENAME_ABSTRACT, $fileContent)) - die(); - - echo 'BUILT: abstract class file "'.PATH_BUILD.'/'.FILENAME_ABSTRACT.'"'."\n"; - -/* - * Build concrete template. - */ - - if(!file_exists(PATH_BUILD.'/'.FILENAME_CONCRETE)) - { - // get template - if(!$template = file_get_contents(PATH_TEMPLATES.'/concrete.tpl.php')) - die(); - - // build file content - $fileContent = replacePlaceholders($template, $templatePlaceholders); - - // write abstract class - if(!file_put_contents(PATH_BUILD.'/'.FILENAME_CONCRETE, $fileContent)) - die(); - - echo 'BUILT: conrete class file "'.PATH_BUILD.'/'.FILENAME_CONCRETE.'"'."\n"; - } - else - { - echo 'SKIPPED: concrete class file "'.PATH_BUILD.'/'.FILENAME_CONCRETE.'"'."\n"; - } - -?> diff --git a/build/build.php b/build/build.php new file mode 100644 index 0000000..58863e0 --- /dev/null +++ b/build/build.php @@ -0,0 +1,269 @@ +. + * + * @copyright GNU General Public License + * @author confirm IT solutions GmbH, Rathausstrase 14, CH-6340 Baar + */ + +if (!in_array(PHP_SAPI, array('cli', 'phpdbg', 'embed'), true)) { + throw new RuntimeException('Error: '.__FILE__.' must be invoked via the CLI version of PHP, not the '.PHP_SAPI.' SAPI'.PHP_EOL); +} + +set_time_limit(0); + +/* + * Load required files. + */ +require __DIR__.'/config.inc.php'; +require __DIR__.'/replacePlaceholders.func.php'; + +/* + * Define some pathes and do some sanity checks for existence of the pathes. + */ + +if (!is_dir(PATH_ZABBIX)) { + throw new RuntimeException('ERROR: Zabbix path "'.PATH_ZABBIX.'" is not a directory! Please check the PATH_ZABBIX configuration constant.'); +} + +// load Zabbix internal constants, to access ZABBIX_API_VERSION +require PATH_ZABBIX.'/include/defines.inc.php'; + +/** + * @brief Path to the API.php class file of the Zabbix PHP front-end. + * + * This class file will be used, to determine all available API classes. + */ +define('PATH_ZABBIX_API_CLASS_FILE', PATH_ZABBIX.'/include/classes/api/API.php'); + +if (!file_exists(PATH_ZABBIX_API_CLASS_FILE)) { + throw new RuntimeException('ERROR: API class file "'.PATH_ZABBIX_API_CLASS_FILE.'" not found! Please check the PATH_ZABBIX_API_CLASS_FILE configuration constant'); +} + +/** + * @brief Path to the api/classes/ directory of the Zabbix PHP front-end. + * + * This directory and the contained class files will be used, to determine all + * available methods for each API class. + */ +if (version_compare(ZABBIX_API_VERSION, '2.4') >= 0) { + define('PATH_ZABBIX_API_CLASSES_DIRECTORY', PATH_ZABBIX.'/include/classes/api/services'); +} else { + define('PATH_ZABBIX_API_CLASSES_DIRECTORY', PATH_ZABBIX.'/api/classes'); +} + +if (!is_dir(PATH_ZABBIX_API_CLASSES_DIRECTORY)) { + throw new RuntimeException('ERROR: API class directory "'.PATH_ZABBIX_API_CLASSES_DIRECTORY.'" not found!'); +} + +/* + * Initialize. + */ + +// set template placeholders +$templatePlaceholders = array( + 'CLASSNAME_ABSTRACT' => CLASSNAME_ABSTRACT, + 'CLASSNAME_CONCRETE' => CLASSNAME_CONCRETE, + 'CLASSNAME_EXCEPTION' => CLASSNAME_EXCEPTION, + 'FILENAME_ABSTRACT' => FILENAME_ABSTRACT, + 'FILENAME_CONCRETE' => FILENAME_CONCRETE, + 'FILENAME_EXCEPTION' => FILENAME_EXCEPTION, +); + +/* + * Create class-map class. + * + * Create a new class and extend it from the origin Zabbix classes, so that we + * can fetch the class map directly from the Zabbix classes without defining + * it here. + * + * There are some differences between the Zabbix versions: + * + * < 2.4: The class map is stored as a static property directly in the + * origin API class. + * + * >= 2.4: The class map is stored as an instance property in the + * origin CApiServiceFactory class. + */ + +// load API +require PATH_ZABBIX_API_CLASS_FILE; + +// create new class to fetch class map for API classes +if (version_compare(ZABBIX_API_VERSION, '2.4') >= 0) { + require PATH_ZABBIX.'/include/classes/core/CRegistryFactory.php'; + require PATH_ZABBIX.'/include/classes/api/CApiServiceFactory.php'; + require PATH_ZABBIX.'/include/classes/api/CApiService.php'; + + class ZabbixApiClassMap extends CApiServiceFactory + { + public function getClassMap() + { + $classMap = $this->objects; + return $classMap; + } + } +} else { + require PATH_ZABBIX.'/include/classes/api/CZBXAPI.php'; + + class ZabbixApiClassMap extends API + { + public function getClassMap() + { + return self::$classMap; + } + } +} + +/* + * Register SPL autoloader. + * + * The API class files always inherit from other classes. Most of the classes + * inherit from the CZBXAPI class, but there are a bunch of classes which + * are extended by other API classes. + * + * So that we don't have to "follow" the right order on loading API class files, + * we're register an API autoloader right here. + * + * Later the get_class_methods() function will automatically invoke this + * autoloader. + */ +function __autoload($className) +{ + require PATH_ZABBIX_API_CLASSES_DIRECTORY.'/'.$className.'.php'; +} + +/* + * Build API array. + * + * Just loop through all available API classes and find all defined methods in + * these classes. + */ + +// initialze API array +$apiArray = array(); + +// Create new instance for API class map. +$apiClassMap = new ZabbixApiClassMap(); + +// loop through class map +foreach ($apiClassMap->getClassMap() as $resource => $class) { + // add resource to API array + $apiArray[$resource] = array(); + + // create new reflection class + $reflection = new ReflectionClass($class); + + // loop through defined methods + foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC & ~ReflectionMethod::IS_STATIC) as $method) { + // add action to API array + if ($method->class != 'CZBXAPI' && !($resource == 'user' && $method->name == 'login') && !($resource == 'user' && $method->name == 'logout') && !$method->isConstructor() && !$method->isDestructor() && !$method->isAbstract()) { + $apiArray[$resource][] = $method->name; + } + } +} + +/* + * Build abstract template. + */ + +// get template +if (!$template = file_get_contents(PATH_TEMPLATES.'/abstract.tpl.php')) { + throw new RuntimeException('Error.'); +} + +// fetch API method block +preg_match('/(.*)(.*)(.*)/s', $template, $matches); + +// sanity check +if (count($matches) != 4) { + throw new RuntimeException('Template "'.PATH_TEMPLATES.'"/abstract.tpl.php parsing failed!'); +} + +// initialize variable for API methods +$apiMethods = ''; + +// build API methods +foreach ($apiArray as $resource => $actions) { + foreach ($actions as $action) { + $methodPlaceholders = array( + 'API_METHOD' => $resource.'.'.$action, + 'PHP_METHOD' => $resource.ucfirst($action) + ); + $apiMethods .= replacePlaceholders($matches[2], $methodPlaceholders); + } +} + +// build file content +$fileContent = replacePlaceholders($matches[1].$apiMethods.$matches[3], $templatePlaceholders); + +// write abstract class +if (!file_put_contents(PATH_BUILD.'/'.FILENAME_ABSTRACT, $fileContent)) { + throw new RuntimeException('Error.'); +} + +echo 'BUILT: abstract class file "'.PATH_BUILD.'/'.FILENAME_ABSTRACT.'"'."\n"; + +/* + * Build concrete template. + */ + +if (!file_exists(PATH_BUILD.'/'.FILENAME_CONCRETE)) { + // get template + if (!$template = file_get_contents(PATH_TEMPLATES.'/concrete.tpl.php')) { + throw new RuntimeException('Error.'); + } + + // build file content + $fileContent = replacePlaceholders($template, $templatePlaceholders); + + // write abstract class + if (!file_put_contents(PATH_BUILD.'/'.FILENAME_CONCRETE, $fileContent)) { + throw new RuntimeException('Error.'); + } + + echo 'BUILT: conrete class file "'.PATH_BUILD.'/'.FILENAME_CONCRETE.'"'."\n"; +} else { + echo 'SKIPPED: concrete class file "'.PATH_BUILD.'/'.FILENAME_CONCRETE.'"'."\n"; +} + +/* + * Build exception template. + */ + +if (!file_exists(PATH_BUILD.'/'.FILENAME_EXCEPTION)) { + // get template + if (!$template = file_get_contents(PATH_TEMPLATES.'/exception.tpl.php')) { + throw new RuntimeException('Error.'); + } + + // build file content + $fileContent = replacePlaceholders($template, $templatePlaceholders); + + // write abstract class + if (!file_put_contents(PATH_BUILD.'/'.FILENAME_EXCEPTION, $fileContent)) { + throw new RuntimeException('Error.'); + } + + echo 'BUILT: conrete class file "'.PATH_BUILD.'/'.FILENAME_EXCEPTION.'"'."\n"; +} else { + echo 'SKIPPED: concrete class file "'.PATH_BUILD.'/'.FILENAME_EXCEPTION.'"'."\n"; +} diff --git a/inc/config.inc.php b/build/config.inc.php similarity index 76% rename from inc/config.inc.php rename to build/config.inc.php index 64da545..26e60d5 100644 --- a/inc/config.inc.php +++ b/build/config.inc.php @@ -27,13 +27,13 @@ * @brief Class name for the abstract API class. */ -define('CLASSNAME_ABSTRACT', 'ZabbixApiAbstract'); +define('CLASSNAME_ABSTRACT', 'AbstractZabbixApi'); /** * @brief Filename for the abstract API class. */ -define('FILENAME_ABSTRACT', CLASSNAME_ABSTRACT.'.class.php'); +define('FILENAME_ABSTRACT', CLASSNAME_ABSTRACT.'.php'); /** * @brief Class name for the concrete API class. @@ -45,7 +45,19 @@ * @brief Filename for the abstract API class. */ -define('FILENAME_CONCRETE', CLASSNAME_CONCRETE.'.class.php'); +define('FILENAME_CONCRETE', CLASSNAME_CONCRETE.'.php'); + +/** + * @brief Class name for the abstract API class. + */ + +define('CLASSNAME_EXCEPTION', 'Exception'); + +/** + * @brief Filename for the abstract API class. + */ + +define('FILENAME_EXCEPTION', CLASSNAME_EXCEPTION.'.php'); /** * @brief Filesystem path to templates directory. @@ -53,7 +65,7 @@ * This directory contains all templates to build the class files. */ -define('PATH_TEMPLATES', 'templates'); +define('PATH_TEMPLATES', __DIR__.'/templates'); /** * @brief Filesystem path to build directory. @@ -61,7 +73,7 @@ * This directory contains the built class files. */ -define('PATH_BUILD', 'build'); +define('PATH_BUILD', __DIR__.'/../src'); /** * @brief Filesystem path to the Zabbix PHP front-end root. @@ -73,5 +85,4 @@ * to update this constant! */ -define('PATH_ZABBIX', '/usr/share/zabbix'); -?> +define('PATH_ZABBIX', '/opt/zabbix/frontend'); diff --git a/inc/replacePlaceholders.func.php b/build/replacePlaceholders.func.php similarity index 99% rename from inc/replacePlaceholders.func.php rename to build/replacePlaceholders.func.php index b8ec2c8..e105476 100644 --- a/inc/replacePlaceholders.func.php +++ b/build/replacePlaceholders.func.php @@ -42,5 +42,3 @@ function replacePlaceholders($string, $placeholders) return $string; } - -?> diff --git a/templates/abstract.tpl.php b/build/templates/abstract.tpl.php similarity index 94% rename from templates/abstract.tpl.php rename to build/templates/abstract.tpl.php index 007ed6c..a5e457f 100644 --- a/templates/abstract.tpl.php +++ b/build/templates/abstract.tpl.php @@ -44,14 +44,6 @@ namespace ZabbixApi; -/** - * @brief Exception class for ZabbixApi namespace. - */ - -class Exception extends \Exception -{ -} - /** * @brief Abstract class for the Zabbix API. */ @@ -282,7 +274,7 @@ public function printCommunication($print = TRUE) } /** - * @brief Sends are request to the zabbix API and returns the response + * @brief Sends are request to the Zabbix API and returns the response * as object. * * @param $method Name of the API method. @@ -421,7 +413,7 @@ private function convertToAssociatveArray($objectArray, $useObjectProperty) * This method will automatically convert all provided types into a correct * array. Which means: * - * - arrays will not be converted (indexed & associatve) + * - arrays will not be converted (indexed & associative) * - scalar values will be converted into an one-element array (indexed) * - other values will result in an empty array * @@ -431,7 +423,7 @@ private function convertToAssociatveArray($objectArray, $useObjectProperty) * indexed array, the default params will not be merged. This is because * there are some API methods, which are expecting a simple JSON array (aka * PHP indexed array) instead of an object (aka PHP associative array). - * Example for this behaviour are delete operations, which are directly + * Example for this behavior are delete operations, which are directly * expecting an array of IDs '[ 1,2,3 ]' instead of '{ ids: [ 1,2,3 ] }'. * * @param $params Params array. @@ -460,7 +452,7 @@ private function getRequestParamsArray($params) /** * @brief Login into the API. * - * This will also retreive the auth Token, which will be used for any + * This will also retrieves the auth Token, which will be used for any * further requests. Please be aware that by default the received auth * token will be cached on the filesystem. * @@ -474,10 +466,10 @@ private function getRequestParamsArray($params) * For more informations about these parameters, check the Zabbix API * documentation at https://www.zabbix.com/documentation/. * - * The $arrayKeyProperty can be used to get an associatve instead of an + * The $arrayKeyProperty can be used to get an associative instead of an * indexed array as response. A valid value for the $arrayKeyProperty is - * is any property of the returned JSON objects (e.g. name, host, - * hostid, graphid, screenitemid). + * is any property of the returned JSON objects (e.g. "name", "host", + * "hostid", "graphid", "screenitemid"). * * @param $params Parameters to pass through. * @param $arrayKeyProperty Object property for key of array. @@ -544,10 +536,10 @@ final public function userLogin($params=array(), $arrayKeyProperty='', $tokenCac * For more informations about these parameters, check the Zabbix API * documentation at https://www.zabbix.com/documentation/. * - * The $arrayKeyProperty can be used to get an associatve instead of an + * The $arrayKeyProperty can be used to get an associative instead of an * indexed array as response. A valid value for the $arrayKeyProperty is - * is any property of the returned JSON objects (e.g. name, host, - * hostid, graphid, screenitemid). + * is any property of the returned JSON objects (e.g. "name", "host", + * "hostid", "graphid", "screenitemid"). * * @param $params Parameters to pass through. * @param $arrayKeyProperty Object property for key of array. @@ -567,17 +559,17 @@ final public function userLogout($params=array(), $arrayKeyProperty='') /** - * @brief Reqeusts the Zabbix API and returns the response of the API + * @brief Requests the Zabbix API and returns the response of the API * method . * * The $params Array can be used, to pass parameters to the Zabbix API. * For more informations about these parameters, check the Zabbix API * documentation at https://www.zabbix.com/documentation/. * - * The $arrayKeyProperty can be used to get an associatve instead of an + * The $arrayKeyProperty can be used to get an associative instead of an * indexed array as response. A valid value for the $arrayKeyProperty is - * is any property of the returned JSON objects (e.g. name, host, - * hostid, graphid, screenitemid). + * is any property of the returned JSON objects (e.g. "name", "host", + * "hostid", "graphid", "screenitemid"). * * @param $params Zabbix API parameters. * @param $arrayKeyProperty Object property for key of array. @@ -601,5 +593,3 @@ public function ($params=array(), $arrayKeyProperty='') } - -?> diff --git a/templates/concrete.tpl.php b/build/templates/concrete.tpl.php similarity index 97% rename from templates/concrete.tpl.php rename to build/templates/concrete.tpl.php index f991f71..77f4fae 100644 --- a/templates/concrete.tpl.php +++ b/build/templates/concrete.tpl.php @@ -31,8 +31,6 @@ namespace ZabbixApi; -require ''; - /** * @brief Concrete class for the Zabbix API. */ @@ -41,5 +39,3 @@ class extends { } - -?> diff --git a/build/templates/exception.tpl.php b/build/templates/exception.tpl.php new file mode 100644 index 0000000..7970d76 --- /dev/null +++ b/build/templates/exception.tpl.php @@ -0,0 +1,51 @@ + + * + * @brief Class file for the implementation of the class . + * + * __ ___ ____ _ _ ___ _ _ ____ + * \ \ / / \ | _ \| \ | |_ _| \ | |/ ___| + * \ \ /\ / / _ \ | |_) | \| || || \| | | _ + * \ V V / ___ \| _ <| |\ || || |\ | |_| | + * \_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____| + * + * This class was automatically generated by the PhpZabbixApi builder script, + * and will be replaced as soon as the script is invoked again, or you're about + * to update the PhpZabbixApi version. Therefor do not modify this file and + * leave it as it is. After updating / regenerating the library, this file will + * be overwritten, and all modifications will be lost. + * + * This file is part of PhpZabbixApi. + * + * 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. + * + * @copyright The MIT License (MIT) + * @author confirm IT solutions GmbH, Rathausstrase 14, CH-6340 Baar + */ + +namespace ZabbixApi; + +/** + * @brief Exception class for ZabbixApi namespace. + */ + +class Exception extends \Exception +{ +} diff --git a/composer.json b/composer.json index c09b480..745ad42 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,20 @@ } ], "require": { - "php": ">=5.3.0" + "php": "^5.3|^7.0" + }, + "require-dev": { + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0" }, "autoload": { - "classmap": ["build/"] + "psr-4": { "ZabbixApi\\": ["src/"] } + }, + "autoload-dev": { + "psr-4": { "ZabbixApi\\Tests\\": ["tests/"] } + }, + "extra": { + "branch-alias": { + "dev-master": "2.4-dev" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..75039f5 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,29 @@ + + + + + + + + + + + + tests + + + + + + src + + build + + + + diff --git a/src/.gitkeep b/src/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/ZabbixApiTest.php b/tests/ZabbixApiTest.php new file mode 100644 index 0000000..592e63d --- /dev/null +++ b/tests/ZabbixApiTest.php @@ -0,0 +1,39 @@ + + */ +final class ZabbixApiTest extends TestCase +{ + public function testZabbixApiClass() + { + $this->assertTrue(class_exists('ZabbixApi\ZabbixApi')); + + $rc = new \ReflectionClass('ZabbixApi\ZabbixApi'); + + $this->assertGreaterThanOrEqual(405, $rc->getMethods(\ReflectionMethod::IS_PUBLIC)); + } + + public function testZabbixApiConnectionNotTriggered() + { + $zabbix = new ZabbixApi('http://localhost/json_rpc.php'); + $zabbix = new ZabbixApi('http://localhost/json_rpc.php', 'zabbix'); + $zabbix = new ZabbixApi('http://localhost/json_rpc.php', '', 'very_secret'); + + $this->assertSame('http://localhost/json_rpc.php', $zabbix->getApiUrl()); + } + + /** + * @expectedException ZabbixApi\Exception + * @expectedExceptionMessage Could not connect to "http://not.found.tld/json_rpc.php" + */ + public function testZabbixApiConnectionError() + { + new ZabbixApi('http://not.found.tld/json_rpc.php', 'zabbix', 'very_secret_pass'); + } +}