
#ifdef HAVE_CONFIG_H
#include "../../ext_config.h"
#endif

#include <php.h>
#include "../../php_ext.h"
#include "../../ext.h"

#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>

#include "kernel/main.h"
#include "kernel/object.h"
#include "kernel/memory.h"
#include "kernel/fcall.h"
#include "kernel/array.h"
#include "kernel/operators.h"


/**
 * This file is part of the Phalcon Framework.
 *
 * (c) Phalcon Team <team@phalcon.io>
 *
 * For the full copyright and license information, please view the LICENSE.txt
 * file that was distributed with this source code.
 */
/**
 * Creates a new Cache class
 *
 * @phpstan-import-type storage_adapter_options from StorageTypes
 */
ZEPHIR_INIT_CLASS(Phalcon_Cache_CacheFactory)
{
	ZEPHIR_REGISTER_CLASS_EX(Phalcon\\Cache, CacheFactory, phalcon, cache_cachefactory, phalcon_factory_abstractconfigfactory_ce, phalcon_cache_cachefactory_method_entry, 0);

	{
		zval _zc0;
		ZVAL_UNDEF(&_zc0);
		zephir_declare_typed_property(phalcon_cache_cachefactory_ce, SL("adapterFactory"), &_zc0, ZEND_ACC_PROTECTED, 0, SL("Phalcon\\Cache\\AdapterFactory"));
	}

	return SUCCESS;
}

/**
 * Constructor
 */
PHP_METHOD(Phalcon_Cache_CacheFactory, __construct)
{
	zval *factory, factory_sub;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&factory_sub);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("adapterFactory", 14, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_OBJECT_OF_CLASS(factory, phalcon_cache_adapterfactory_ce)
	ZEND_PARSE_PARAMETERS_END();
	zephir_fetch_params_without_memory_grow(1, 0, &factory);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 489, factory);
}

/**
 * Factory to create an instance from a Config object
 *
 * @param array<string, mixed>|ConfigInterface $config = [
 *     'adapter' => 'apcu',
 *     'options' => [
 *         'servers' => [
 *             [
 *                 'host' => 'localhost',
 *                 'port' => 11211,
 *                 'weight' => 1,
 *             ]
 *         ],
 *         'host' => '127.0.0.1',
 *         'port' => 6379,
 *         'index' => 0,
 *         'persistent' => false,
 *         'auth' => '',
 *         'socket' => '',
 *         'defaultSerializer' => 'Php',
 *         'lifetime' => 3600,
 *         'serializer' => null,
 *         'prefix' => 'phalcon',
 *         'storageDir' => ''
 *     ]
 * ]
 *
 * @return CacheInterface
 * @throws Exception
 */
PHP_METHOD(Phalcon_Cache_CacheFactory, load)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zend_long ZEPHIR_LAST_CALL_STATUS;
	zval *config = NULL, config_sub, name, options, _0, _1;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&config_sub);
	ZVAL_UNDEF(&name);
	ZVAL_UNDEF(&options);
	ZVAL_UNDEF(&_0);
	ZVAL_UNDEF(&_1);
	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_ZVAL(config)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_fetch_params(1, 1, 0, &config);
	ZEPHIR_SEPARATE_PARAM(config);
	ZEPHIR_CALL_METHOD(&_0, this_ptr, "checkconfig", NULL, 0, config);
	zephir_check_call_status();
	ZEPHIR_CPY_WRT(config, &_0);
	ZEPHIR_INIT_VAR(&_1);
	ZVAL_STRING(&_1, "adapter");
	ZEPHIR_CALL_METHOD(&_0, this_ptr, "checkconfigelement", NULL, 0, config, &_1);
	zephir_check_call_status();
	ZEPHIR_CPY_WRT(config, &_0);
	zephir_memory_observe(&name);
	zephir_array_fetch_string(&name, config, SL("adapter"), PH_NOISY, "phalcon/Cache/CacheFactory.zep", 72);
	zephir_memory_observe(&options);
	if (!(zephir_array_isset_string_fetch(&options, config, SL("options"), 0))) {
		ZEPHIR_INIT_NVAR(&options);
		array_init(&options);
	}
	if (Z_TYPE_P(&options) != IS_ARRAY) {
		ZEPHIR_INIT_NVAR(&options);
		array_init(&options);
	}
	ZEPHIR_RETURN_CALL_METHOD(this_ptr, "newinstance", NULL, 0, &name, &options);
	zephir_check_call_status();
	RETURN_MM();
}

/**
 * Constructs a new Cache instance.
 *
 * @param array  $options = [
 *      'servers'           => [
 *          [
 *              'host' => 'localhost',
 *              'port' => 11211,
 *              'weight' => 1,
 *          ]
 *      ],
 *      'host'              => '127.0.0.1',
 *      'port'              => 6379,
 *      'index'             => 0,
 *      'persistent'        => false,
 *      'auth'              => '',
 *      'socket'            => '',
 *      'defaultSerializer' => 'Php',
 *      'lifetime'          => 3600,
 *      'serializer'        => null,
 *      'prefix'            => 'phalcon',
 *      'storageDir'        => '',
 * ]
 *
 * @phpstan-param storage_adapter_options $options
 *
 * @return CacheInterface
 * @throws Exception
 */
PHP_METHOD(Phalcon_Cache_CacheFactory, newInstance)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zend_long ZEPHIR_LAST_CALL_STATUS;
	zval options;
	zval name_zv, *options_param = NULL, adapter, _0;
	zend_string *name = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&name_zv);
	ZVAL_UNDEF(&adapter);
	ZVAL_UNDEF(&_0);
	ZVAL_UNDEF(&options);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("adapterFactory", 14, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 2)
		Z_PARAM_STR(name)
		Z_PARAM_OPTIONAL
		ZEPHIR_Z_PARAM_ARRAY(options, options_param)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	if (ZEND_NUM_ARGS() > 1) {
		options_param = ZEND_CALL_ARG(execute_data, 2);
	}
	zephir_memory_observe(&name_zv);
	ZVAL_STR_COPY(&name_zv, name);
	if (!options_param) {
		ZEPHIR_INIT_VAR(&options);
		array_init(&options);
	} else {
		zephir_get_arrval(&options, options_param);
	}
	zephir_read_property_cached(&_0, this_ptr, _zephir_prop_0, 489, PH_NOISY_CC | PH_READONLY);
	ZEPHIR_CALL_METHOD(&adapter, &_0, "newinstance", NULL, 0, &name_zv, &options);
	zephir_check_call_status();
	object_init_ex(return_value, phalcon_cache_cache_ce);
	ZEPHIR_CALL_METHOD(NULL, return_value, "__construct", NULL, 451, &adapter);
	zephir_check_call_status();
	RETURN_MM();
}

/**
 * Returns the exception class for the factory
 *
 * @return class-string<Throwable>
 */
PHP_METHOD(Phalcon_Cache_CacheFactory, getExceptionClass)
{

	RETURN_STRING("Phalcon\\Cache\\Exception\\Exception");
}

