PHP code example of srag / dic

1. Go to this page and download the library: Download srag/dic library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

srag / dic example snippets


//...
use srag\DIC\DICTrait;
//...
class x {
	//...
	use DICTrait;
	//...
	const PLUGIN_CLASS_NAME = ilXPlugin::class;
	//...
}

/**
 * Get DIC interface
 * 
 * @return DICInterface DIC interface
 */
self::dic(): DICInterface;

/**
 * @return ilCtrl
 */
self::dic()->ctrl(): ilCtrl;

/**
 * Get plugin interface
 * 
 * @return PluginInterface Plugin interface
 *
 * @throws DICException
 */
self::plugin(): PluginInterface;

/**
 * Get plugin directory
 * 
 * @return string Plugin directory
 */
self::plugin()->directory(): string;

/**
 * Output HTML or GUI
 * 
 * @param string|object $html          HTML code or some GUI instance
 * @param bool          $show          Show main template?
 * @param bool          $main_template Display main skin?
 *
 * @throws DICException
 */
self::output()->output($value, bool $show = false, bool $main_template = true) : void

/**
 * Output JSON
 * 
 * @param string|int|double|bool|array|stdClass|null|JsonSerializable $value JSON value
 *
 * @throws DICException
 */
self::output()->outputJSON($value) : void;

/**
 * Get HTML of GUI
 * 
 * @param string|object $html HTML code or some GUI instance
 *
 * @return string HTML
 *
 * @throws DICException
 */
self::output()->getHTML($value): string;

/**
 * Get a template
 * 
 * @param string $template                 Template path
 * @param bool   $remove_unknown_variables Should remove unknown variables?
 * @param bool   $remove_empty_blocks      Should remove empty blocks?
 * @param bool   $plugin                   Plugin template or ILIAS core template?
 *
 * @return ilTemplate ilTemplate instance
 *
 * @throws DICException
 */
self::plugin()->template(string $template, bool $remove_unknown_variables = true, bool $remove_empty_blocks = true, bool $plugin = true): ilTemplate;

/**
 * Translate text
 * 
 * @param string $key          Language key
 * @param string $module       Language module
 * @param array  $placeholders Placeholders in your language texst to replace with vsprintf
 * @param bool   $plugin       Plugin language or ILIAS core language?
 * @param string $lang         Possibly specific language, otherwise current language, if empty
 * @param string $default      Default text, if language key not exists
 *
 * @return string Translated text
 *
 * @throws DICException
 */
self::plugin()->translate(string $key, string $module = "", array $placeholders = [], bool $plugin = true, string $lang = "", string $default = "MISSING %s"): string;

/**
 * Get ILIAS plugin object instance
 *
 * Please avoid to use ILIAS plugin object instance and instead use methods in this class!
 *
 * @return ilPlugin ILIAS plugin object instance
 */
self::plugin()->getPluginObject(): ilPlugin;

/**
 * Get version interface
 * 
 * @return VersionInterface Version interface
 */
self::version(): VersionInterface;

use srag\DIC\x\DICStatic;use srag\Plugins\x\x\x;DICStatic::dic()->database()->createAutoIncrement(x::TABLE_NAME, "id");

self::dic()->database()->resetAutoIncrement(x::TABLE_NAME, "id");

self::dic()->database()->dropAutoIncrementTable(x::TABLE_NAME);

$x = $this->factory()->newInstance();
...
$x->setId(self::dic()->database()->store(x::TABLE_NAME, [
			"field_1" => [ ilDBConstants::T_TEXT, $x->getField1() ],
			"field_2" => [ ilDBConstants::T_INTEGER, $x->getField2() ]
		], "id", $x->getId()));

$array = self::dic()->database()->fetchAllCallback(self::dic()->database()->query('SELECT * FROM ' . self::dic()->database()
				->quoteIdentifier(x::TABLE_NAME)), [ $this->factory(), "fromDB" ]);
		
...

public function fromDB(stdClass $data): x {
	$x = $this->newInstance();

	$x->setId($data->id);
	$x->setField1($data->field_1);
	$x->setField2($data->field_2);

	return $x;
}


self::dic()->database()->createOrUpdateTable($table_name, $columns, $primary_columns)

 self::dic()->database()->multipleInsert($table_name, ["column1", "column2", "column3"], [
            [
                ["value11", ilDBConstants::T_TEXT],
                ["value12", ilDBConstants::T_INTEGER],
                ["value13", ilDBConstants::T_TEXT]
            ],
            [
                ["value21", ilDBConstants::T_TEXT],
                ["value22", ilDBConstants::T_INTEGER],
                ["value23", ilDBConstants::T_TEXT]
            ],
            [
                ["value31", ilDBConstants::T_TEXT],
                ["value32", ilDBConstants::T_INTEGER],
                ["value33", ilDBConstants::T_TEXT]
            ]
        ])

...
use srag\DIC\x\Version\PluginVersionParameter;
...
$version_parameter = PluginVersionParameter::getInstance()->withPlugin(self::plugin());
self::dic()->ui()->mainTemplate()->addCss($version_parameter->appendToUrl("..."));
self::dic()->ui()->mainTemplate()->addJavaScript($version_parameter->appendToUrl("..."));
...