Ejemplo n.º 1
0
/**
 * Return the lastest DI created
 *
 * @return Phalcon\DiInterface
 */
PHP_METHOD(Phalcon_DI, getDefault){

	zval default_di = {}, dependency_injector = {};

	phalcon_return_static_property_ce(&default_di, phalcon_di_ce, SL("_default"));
	if (Z_TYPE(default_di) != IS_OBJECT) {
		object_init_ex(&dependency_injector, phalcon_di_factorydefault_ce);
		PHALCON_CALL_METHODW(NULL, &dependency_injector, "__construct");
		RETURN_CTORW(&dependency_injector);
	}

	RETURN_CTORW(&default_di);
}
Ejemplo n.º 2
0
/**
 * Load config file
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Ini, read){

	zval *file_path, *absolute_path = NULL, *scanner_mode = NULL, config_dir_path = {}, base_path = {}, ini_config = {}, config = {}, *directives;
	zend_string *str_key;
	ulong idx;

	phalcon_fetch_params(0, 1, 2, &file_path, &absolute_path, &scanner_mode);
	PHALCON_ENSURE_IS_STRING(file_path);

	if (!absolute_path) {
		absolute_path = &PHALCON_GLOBAL(z_false);
	}

	if (zend_is_true(absolute_path)) {
		PHALCON_CPY_WRT_CTOR(&config_dir_path, file_path);
	} else {
		phalcon_return_static_property_ce(&base_path, phalcon_config_adapter_ce, SL("_basePath"));

		PHALCON_CONCAT_VV(&config_dir_path, &base_path, file_path);
	}

	/** 
	 * Use the standard parse_ini_file
	 */
	if (scanner_mode && Z_TYPE_P(scanner_mode) == IS_LONG) {
		PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true), scanner_mode);
	} else {
		PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true));
	}

	/** 
	 * Check if the file had errors
	 */
	if (Z_TYPE(ini_config) != IS_ARRAY) {
		zend_throw_exception_ex(phalcon_config_exception_ce, 0, "Configuration file '%s' cannot be read", Z_STRVAL(config_dir_path));
		return;
	}

	array_init(&config);

	ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(ini_config), idx, str_key, directives) {
		zval section = {}, *value;
		if (str_key) {
			ZVAL_STR(&section, str_key);
		} else {
			ZVAL_LONG(&section, idx);
		}

		if (unlikely(Z_TYPE_P(directives) != IS_ARRAY) || zend_hash_num_elements(Z_ARRVAL_P(directives)) == 0) {
			phalcon_array_update_zval(&config, &section, directives, PH_COPY);
		} else {
			ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(directives), idx, str_key, value) {
				zval key = {}, directive_parts = {};
				if (str_key) {
					ZVAL_STR(&key, str_key);
				} else {
					ZVAL_LONG(&key, idx);
				}

				if (str_key && memchr(Z_STRVAL(key), '.', Z_STRLEN(key))) {
					phalcon_fast_explode_str(&directive_parts, SL("."), &key);
					phalcon_config_adapter_ini_update_zval_directive(&config, &section, &directive_parts, value);
				} else {
					phalcon_array_update_multi_2(&config, &section, &key, value, PH_COPY);
				}
			} ZEND_HASH_FOREACH_END();
		}
Ejemplo n.º 3
0
/**
 * Phalcon\Image\GD constructor
 *
 * @param string $file
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, __construct){

	zval *file, *width = NULL, *height = NULL, exception_message = {}, checked = {}, realpath = {}, img_width = {}, img_height = {}, type = {}, mime = {}, format = {}, image = {}, imageinfo = {}, saveflag = {}, blendmode = {};

	phalcon_fetch_params(0, 1, 2, &file, &width, &height);

	if (Z_TYPE_P(file) != IS_STRING) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "file parameter should be a string");
		return;
	}

	phalcon_return_static_property_ce(&checked, phalcon_image_adapter_gd_ce, SL("_checked"));

	if (!zend_is_true(&checked)) {
		PHALCON_CALL_CE_STATICW(NULL, phalcon_image_adapter_gd_ce, "check");
	}

	phalcon_update_property_zval(getThis(), SL("_file"), file);

	if (phalcon_file_exists(file) != FAILURE) {
		phalcon_file_realpath(&realpath, file);
		if (unlikely(Z_TYPE(realpath) != IS_STRING)) {
			convert_to_string(&realpath);
		}

		phalcon_update_property_zval(getThis(), SL("_realpath"), &realpath);

		PHALCON_CALL_FUNCTIONW(&imageinfo, "getimagesize", &realpath);

		if (phalcon_array_isset_fetch_long(&img_width, &imageinfo, 0)) {
			phalcon_update_property_zval(getThis(), SL("_width"), &img_width);
		}

		if (phalcon_array_isset_fetch_long(&img_height, &imageinfo, 1)) {
			phalcon_update_property_zval(getThis(), SL("_height"), &img_height);
		}

		if (phalcon_array_isset_fetch_long(&type, &imageinfo, 2)) {
			convert_to_long(&type);
			phalcon_update_property_zval(getThis(), SL("_type"), &type);
		} else {
			ZVAL_LONG(&type, -1);
		}

		if (phalcon_array_isset_fetch_str(&mime, &imageinfo, SL("mime"))) {
			convert_to_string(&mime);
			phalcon_update_property_zval(getThis(), SL("_mime"), &mime);
		}

		assert(Z_TYPE(type) == IS_LONG);

		switch (Z_LVAL(type)) {
			case 1: // GIF
				ZVAL_STRING(&format, "gif");
				PHALCON_CALL_FUNCTIONW(&image, "imagecreatefromgif", &realpath);
				break;

			case 2: // JPEG
				ZVAL_STRING(&format, "jpg");
				PHALCON_CALL_FUNCTIONW(&image, "imagecreatefromjpeg", &realpath);
				break;

			case 3: // PNG
				ZVAL_STRING(&format, "png");
				PHALCON_CALL_FUNCTIONW(&image, "imagecreatefrompng", &realpath);
				break;

			default:
				if (PHALCON_IS_NOT_EMPTY(&mime)) {
					zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Installed GD does not support '%s' images", Z_STRVAL(mime));
				} else {
					zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Installed GD does not support such images");
				}
				return;
		}

		if (Z_TYPE(image) != IS_RESOURCE) {
			assert(Z_TYPE(realpath) == IS_STRING);
			zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Failed to create image from file '%s'", Z_STRVAL(realpath));
			return;
		}

		phalcon_update_property_zval(getThis(), SL("_format"), &format);

		ZVAL_TRUE(&saveflag);

		PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &image, &saveflag);
	} else if (width && height) {
		PHALCON_CALL_FUNCTIONW(&image, "imagecreatetruecolor", width, height);

		if (Z_TYPE(image) != IS_RESOURCE) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "imagecreatetruecolor() failed");
			return;
		}

		ZVAL_TRUE(&blendmode);
		ZVAL_TRUE(&saveflag);

		PHALCON_CALL_FUNCTIONW(NULL, "imagealphablending", &image, &blendmode);
		PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &image, &saveflag);

		phalcon_update_property_zval(getThis(), SL("_realpath"), file);
		phalcon_update_property_zval(getThis(), SL("_width"), width);
		phalcon_update_property_zval(getThis(), SL("_height"), height);

		ZVAL_LONG(&type, 3);

		phalcon_update_property_zval(getThis(), SL("_type"), &type);

		ZVAL_STRING(&format, "png");

		phalcon_update_property_zval(getThis(), SL("_format"), &format);

		ZVAL_STRING(&mime, "image/png");

		phalcon_update_property_zval(getThis(), SL("_mime"), &mime);
	} else {
		PHALCON_CONCAT_SVS(&exception_message, "Failed to create image from file '", file, "'");
		PHALCON_THROW_EXCEPTION_ZVALW(phalcon_image_exception_ce, &exception_message);
		return;
	}

	phalcon_update_property_zval(getThis(), SL("_image"), &image);
}