I try to parse a ini file to load PHP classes from it but whenever parse_ini_file parses the file, the variable that holds the content is always only "1".
Here is my code:
private $plugins = array();
public function __construct() {
Logger::log("Loading plugins");
Logger::debug("Loading " . APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini");
if (file_exists(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini")) {
$data = parse_ini_file(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini", true);
foreach ($data as $k => $d) {
if (class_exists($k)) {
$plugin = $this->createPlugin($k);
if ($plugin instanceof Plugin) {
$this->init($plugin, $d);
} else {
Logger::error("Plugin doesn't implements interface Plugin");
}
} else {
Logger::error("Can't load plugin $k");
}
}
} else {
Logger::error(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini doesn't exists");
}
}
public function createPlugin($name) {
$plugin = unserialize(
sprintf(
'O:%d:"%s":0:{}', strlen($name), $name
)
);
return $plugin;
}
public function init(Plugin $plugin, $data) {
try {
Logger::log("Adding plugin " . get_class($plugin));
Logger::debug("Calling init");
$plugin->init($data);
$this->plugins[get_class($plugin)] = $plugin;
} catch (PluginException $ex) {
Logger::error("Error while init plugin");
}
}
The ini file:
[\eBot\Plugins\Official\MissionChecker]
url=http://someurl
I tried to add a log message after
foreach ($data as $k => $d) {
But this line was never called, therefore $data must be empty, buw how is that possible?
var_dump and print_r($data) show me "1".
I solved the problem by putting the url in the ini file in quotes.
[\eBot\Plugins\Official\MissionChecker]
url="http://someurl"
Related
I was updating my client's website and I encountered this error.
I looked in the file where the error was and it's this code:
function asset_path($filename) {
$dist_path = get_template_directory_uri() . '/dist/';
$directory = dirname($filename) . '/';
$file = basename($filename);
static $manifest;
if (empty($manifest)) {
$manifest_path = get_template_directory() . '/dist/' . 'assets.json';
$manifest = new JsonManifest($manifest_path);
}
if (array_key_exists($file, $manifest->get())) {
return $dist_path . $directory . $manifest->get()[$file];
} else {
return $dist_path . $directory . $file;
}
}
especially, this code in question
if (array_key_exists($file, $manifest->get())) {
return $dist_path . $directory . $manifest->get()[$file];
} else {
return $dist_path . $directory . $file;
}
What's wrong with this code above and how do I fix it?
I am trying to read some class files stored in a folder and get their methods.
However, the get_class_methods is returning null. But if I put my server_defines file and index.php into the v1 folder, everything works as it should.
I'm unsure what is going on, since it appears to me (a novice) that everything is included.
Here is the folder structure
Api
server_defines.php
Test
index.php
v1
MyMasterClass.php
Operations
OneClass.php
TwoClass.php
ThreeClass.php
server_defines.php
error_reporting(E_ALL ^ E_STRICT ^ E_DEPRECATED);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
if (!defined("SERVER_DOCUMENT_ROOT")) {
define("SERVER_DOCUMENT_ROOT", $_SERVER['DOCUMENT_ROOT'] . "/");
}
define( "SERVER_API_PATH", SERVER_DOCUMENT_ROOT . "v1/" );
define( "SERVER_API_OPERATIONS_PATH", SERVER_API_PATH . "Operations/" );
Test/index.php
require_once( $_SERVER[ 'DOCUMENT_ROOT' ] . "/server_defines.php" );
spl_autoload_register( function ( $class ) {
$sFile = SERVER_API_PATH . str_replace( '\\', '/', $class ) . '.php';
if (file_exists( $sFile )) {
echo "requiring file: " . $sFile . "<br/>";
require $sFile;
}
} );
var_dump(MyMasterClass::getClassesAndMethods());
* Which Outputs*
requiring file: /Users/myname/Documents/GitHub/myproject/Api/v1/MyMasterClass.php
folder_contents: array(3) {
[0]=>
string(80) "/Users/myname/Documents/GitHub/myproject/Api/v1/Operations/OneClass.php"
[1]=>
string(75) "/Users/myname/Documents/GitHub/myproject/Api/v1/Operations/TwoClass.php"
[2]=>
string(75) "/Users/myname/Documents/GitHub/myproject/Api/v1/Operations/ThreeClass.php"
}
basename: OneClass
get_class_methods: NULL
basename: TwoClass
get_class_methods: NULL
basename: ThreeClass
get_class_methods: NULL
MyMasterClass.php
class MyMasterClass {
public function __toString () {
return $this->result;
}
public function __construct ( ) {
}
public static function getClassesAndMethods () {
$api = new API();
return $api->buildClassesAndMethods();
}
private function buildClassesAndMethods () {
$folder_contents = glob( SERVER_API_OPERATIONS_PATH . '*.{php}', GLOB_BRACE );
echo "<br/>folder_contents: ";
var_dump( $folder_contents );
echo "<br/>";
$collection = array();
foreach ($folder_contents as $name) {
$name = basename( $name, ".php" );
echo "<br/>basename: " . $name . "<br/>";
echo "<br/>get_class_methods: ";
var_dump( get_class_methods( $name ) );
echo "<br/>";
}
return $collection;
}
}
When you move your classes to subfolder, you autoloader isn't able to load those classes since it covers only files located in SERVER_API_PATH. Try to update your autoload method or load classes manually using require_once.
Try
...
foreach ($folder_contents as $name) {
require_once($name);
$name = basename( $name, ".php" );
echo "<br/>basename: " . $name . "<br/>";
echo "<br/>get_class_methods: ";
var_dump( get_class_methods( $name ) );
echo "<br/>";
}
...
I have a website which already works on 2 languages ,russian and english(everything runs well in both languages), now i have added armenian language.
The Problem --- when i switch on the website into armenain language , i see ,for example,in breadcrumbs
text_home button_continue button_login ....
i have checked \catalog\language\armen\armenian.php file and noticed that values of this varables exist.
By the way ,when i add from armenian.php into ,for example, language/armen/common/header .php this code
$_['text_home'] = 'arm_home';
it works , but thit means that i should add by hand in every single page this general variable...
i would like to have more optimal solution ...
from admin panel i set armenain as default language
Maybe ,i should edit system\library\language.php ???
Here is the structure
<?php
class Language {
private $default = 'en-gb';
private $directory;
private $data = array();
public function __construct($directory = '') {
$this->directory = $directory;
}
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : $key);
}
public function set($key, $value) {
$this->data[$key] = $value;
}
// Please dont use the below function i'm thinking getting rid of it.
public function all() {
return $this->data;
}
// Please dont use the below function i'm thinking getting rid of it.
public function merge(&$data) {
array_merge($this->data, $data);
}
public function load($filename, &$data = array()) {
$_ = array();
$file = DIR_LANGUAGE . 'english/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . 'english/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->default . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->default . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->directory . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$this->data = array_merge($this->data, $_);
return $this->data;
}
}
Thank you in advance
I used an old package of armenain language ,which wasn't compatible with oc 2.3,
solution https://crowdin.com/project/opencart-translation-v2/hy-AM#
I am using codeigniter. I need to get all variables from a language file to an array.Is it possible?
Is there any method available like as follows?
$a = $this->load->language('editor');
print_r($a);
I was tried $this->lang->language; But,This will return labels from another language files loaded.
$CI = & get_instance();
$arr = $CI->lang->language;
Or Use following library
Class My_language {
var $language = array();
/**
* List of loaded language files
*
* #var array
*/
var $is_loaded = array();
function __construct() {
log_message('debug', "Language Class Initialized");
}
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
$langfile = str_replace('.php', '', $langfile);
if ($add_suffix == TRUE) {
$langfile = str_replace('_lang.', '', $langfile) . '_lang';
}
$langfile .= '.php';
if (in_array($langfile, $this->is_loaded, TRUE)) {
return;
}
$config = & get_config();
if ($idiom == '') {
$deft_lang = (!isset($config['language'])) ? 'english' : $config['language'];
$idiom = ($deft_lang == '') ? 'english' : $deft_lang;
}
// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path . 'language/' . $idiom . '/' . $langfile)) {
include($alt_path . 'language/' . $idiom . '/' . $langfile);
} else {
$found = FALSE;
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) {
if (file_exists($package_path . 'language/' . $idiom . '/' . $langfile)) {
include($package_path . 'language/' . $idiom . '/' . $langfile);
$found = TRUE;
break;
}
}
if ($found !== TRUE) {
show_error('Unable to load the requested language file: language/' . $idiom . '/' . $langfile);
}
}
if (!isset($lang)) {
log_message('error', 'Language file contains no data: language/' . $idiom . '/' . $langfile);
return;
}
if ($return == TRUE) {
return $lang;
}
$this->is_loaded[] = $langfile;
$this->language = array();
$this->language = $lang;
return $this->language;
unset($lang);
log_message('debug', 'Language file loaded: language/' . $idiom . '/' . $langfile);
return TRUE;
}
}
Call like this
$this->load->library('my_language');
$arr = $this->my_language->load('demo');
print_r($arr);
I know this is quite an old question, but I just want to give my solution for this problem since no answers has done the trick for this problem. (tested on codeigniter 3)
$this->load->helper('language');
$foo = $this->lang->load('lang_file', 'english', true);
print_r($foo);
notice that the third parameter for load method determines whether to return the loaded array of translations. source: codeigniter 3 docs.
hope this helps
Yeah ofcourse its possible. You can do like this :
//load helper for language
$this->load->helper('language');
//test is the language file in english folder
$this->lang->load('test','english');
//fetch all the data in $var variable
$var=$this->lang->language;
//print $var
print_r($var);
$var will return the array. :)
If you want to return language file data in Array than you need to pass the third parameter in load function.
$this->lang->load('header','hindi',true) // filename,language,true
<?php
/** Check if environment is development and display errors **/
function setReporting() {
if (DEVELOPMENT_ENVIRONMENT == true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
ini_set('error_log', ROOT.DS.'tmp'.DS.'logs'.DS.'error.log');
}
}
/** Check for Magic Quotes and remove them **/
function stripSlashesDeep($value) {
$value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);
return $value;
}
function removeMagicQuotes() {
if ( get_magic_quotes_gpc() ) {
$_GET = stripSlashesDeep($_GET );
$_POST = stripSlashesDeep($_POST );
$_COOKIE = stripSlashesDeep($_COOKIE);
}
}
/** Check register globals and remove them **/
/*function unregisterGlobals() {
if (ini_get('register_globals')) {
$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
unset($GLOBALS[$key]);
}
}
}
}
}*/
/** Routing **/
function routeURL($url) {
global $routing;
foreach ( $routing as $pattern => $result ) {
if ( preg_match( $pattern, $url ) ) {
return preg_replace( $pattern, $result, $url );
}
}
return ($url);
}
/** Main Call Function **/
function callHook() {
global $url;
global $default;
global $sent;
$queryString = array();
if (!isset($url)) {
$controller = $default['controller'];
$action = $default['action'];
} else {
$url = routeURL($url);
$urlArray = array();
$urlArray = explode("/",$url);
$controller = $urlArray[0];
array_shift($urlArray);
if (isset($urlArray[0])) {
$action = $urlArray[0];
array_shift($urlArray);
} else {
$action = 'view'; // Default Action
}
$queryString = $urlArray;
if(isset($queryString[0]))
$sent=$queryString[0];
//echo $sent;
}
$controllerName = $controller;
$controller = ucwords($controller);
$model = rtrim($controller, 's');
$controller .= 'Controller';
//echo($model);
//echo($controllerName);
//echo($action);
//echo phpinfo();
**$dispatch = new $controller($model,$controllerName,$action);**
if ((int)method_exists($controller, $action)) {
//call_user_func_array(array($dispatch,"beforeAction"),$queryString);
call_user_func_array(array($dispatch,$action),$queryString);
//call_user_func_array(array($dispatch,"afterAction"),$queryString);
} else {
/* Error Generation Code Here */
}
}
/** Autoload any classes that are required **/
function __autoload($className) {
if (file_exists(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php')) {
include_once(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php')) {
include_once(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php')) {
include_once(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php');
} else {
/* Error Generation Code Here */
}
}
setReporting();
removeMagicQuotes();
//unregisterGlobals();
callHook();
*****//
When I uploaded this file on server it is showing the error
Fatal error: Cannot instantiate non-existent class:updatescontroller on line 97
pointing to the line
$dispatch =new $controller($model,$controllerName,$action);
Please help me determine what is going wrong.
Also the same server is also not allowing me to run the unregisterGlobals() function and showing “too many errors for undefined index”.
The complete project is running very well on my localhost server.
From the error message I guess you are missing an included file that declares the updatescontroller class. Maybe you need to upload the entire project?
It also seems that your local PHP configuration doesn't match the remote configuration. Try matching your local php.ini with the remote server to make local testing more realistic.
If at all possible try to match the PHP version on your local server with the remote server. There can be subtle differences between versions.