Cphalcon: [BUG]: PHP Warning: Invalid callback with Phalcon\Config\Adapter\Php

Created on 2 Nov 2019  路  7Comments  路  Source: phalcon/cphalcon

I try to simply use Phalcon\Config\Adapter\Php and I receive a PHP Warning: Invalid callback , no array or string given.

I got the issue with the first $config = new Phalcon\Config\Adapter\Php(DIR_CONFIGS.'/config.php');

In my index.php

$di = new Phalcon\Di\FactoryDefault();

$di->setShared('config', function() {

  $config = new Phalcon\Config\Adapter\Php(DIR_CONFIGS.'/config.php');
  $external_path = DIR_CONFIGS.'/externals/config.php';
  if(file_exists($external_path)) {
    $external_array = include($external_path);

    if(!is_array($external_array)) {
      return $config;
    }

    $config_external = new Phalcon\Config($external_array);
    $config->merge($config_external);
  }
  return $config;
});

In my controller

<?php
class EventController extends \Phalcon\Mvc\Controller {

  public function insert() {
     echo var_export($this->di->get('config'), true);
  }
}

Stack Trace

[02-Nov-2019 09:31:54 Europe/Paris] PHP Stack trace:
[02-Nov-2019 09:31:54 Europe/Paris] PHP   1. {main}() /var/www/www/index.php:0
[02-Nov-2019 09:31:54 Europe/Paris] PHP   2. Phalcon\Mvc\Micro->handle() /var/www/www/index.php:122
[02-Nov-2019 09:31:54 Europe/Paris] PHP   3. Phalcon\Mvc\Micro\LazyLoader->callMethod() /var/www/www/index.php:122
[02-Nov-2019 09:31:54 Europe/Paris] PHP   4. EventController->insert() /var/www/www/index.php:122
[02-Nov-2019 09:31:54 Europe/Paris] PHP   5. Phalcon\Di\FactoryDefault->get() /var/www/app/controllers/EventController.php:39
[02-Nov-2019 09:31:54 Europe/Paris] PHP   6. Phalcon\Di\Service->resolve() /var/www/app/controllers/EventController.php:39
[02-Nov-2019 09:31:54 Europe/Paris] PHP   7. Phalcon\Di\FactoryDefault->{closure:/var/www/www/index.php:20-35}() /var/www/app/controllers/EventController.php:39
[02-Nov-2019 09:31:54 Europe/Paris] PHP   8. Phalcon\Config\Adapter\Php->__construct() /var/www/www/index.php:22
[02-Nov-2019 09:31:54 Europe/Paris] PHP   9. Phalcon\Config\Adapter\Php->__construct() /var/www/www/index.php:22
[02-Nov-2019 09:31:54 Europe/Paris] PHP  10. Phalcon\Config\Adapter\Php->init() /var/www/www/index.php:22
[02-Nov-2019 09:31:54 Europe/Paris] PHP  11. Phalcon\Config\Adapter\Php->setData() /var/www/www/index.php:22

config.php
DIR_CONFIGS.'/config.php' is a good full qualified path.
I also tried to include it and var_export it and I got an array.

<?php
return [
  "test" => [
    "test2" => 1,
  ]
];

Details
Debian Buster in a Docker container + nginx + php-fpm
Phalcon version : commit 95484a06ff9f1f4624b83761aba4d9222026788f (HEAD -> master, tag: v4.0.0-rc.2, origin/master, origin/HEAD)
Php version : 7.3.11
Powered by Zephir => Version 0.12.10-fedc314
Phalcon compiled from sources

Thanks for the help :)

not a bug

Most helpful comment

Ok, finded the issue thanks to strace.

I missed php-mbstring...

Sorry for that and thanks for help ekmst and sergeyklay :+1:

All 7 comments

Please try using require

$external_array = require $external_path;

Hello,

I tried but I got the same issue.
The warning is not link to that part but with the first instanciation of Phalcon\Config\Adapter\Php.

For example, I got the exact same issue with

$di = new Phalcon\Di\FactoryDefault();

$di->setShared('config', function() {
  $config = new Phalcon\Config\Adapter\Php(DIR_CONFIGS.'/config.php');
  return $config;
});

Thanks

@mathbo
You can take advantage of this. While i check

$di = new Phalcon\Di\FactoryDefault();

$di->setShared('config', function() {
  $configs = require DIR_CONFIGS . '/config.php';
  $config = new Phalcon\Config($configs);
  return $config;
});

Sorry, I was not able to reproduce this in my environment.

Please double check DIR_CONFIGS.'/config.php' file exists. How about something like this:

$di = new Phalcon\Di\FactoryDefault();

$di->setShared('config', function() {
    $config = new Phalcon\Config([]);

    // Verify file exists
    if (file_exists(DIR_CONFIGS . '/config.php')) {
        $arrayConfigs = require DIR_CONFIGS . '/config.php';

        // Verify it is array
        if (is_array($arrayConfigs)) {
            $config->merge($arrayConfigs);
        }
    }

    return $config;
});

@mathbo
You can take advantage of this. While i check

$di = new Phalcon\Di\FactoryDefault();

$di->setShared('config', function() {
  $configs = require DIR_CONFIGS . '/config.php';
  $config = new Phalcon\Config($configs);
  return $config;
});

It make php-fpm crash.
I tried also with that and it crash too. I will try to strace what happen.

$di->setShared('config', function() {
  $config = new Phalcon\Config(['test' => 'test1']);
  return $config;
});

Ok, finded the issue thanks to strace.

I missed php-mbstring...

Sorry for that and thanks for help ekmst and sergeyklay :+1:

Was this page helpful?
0 / 5 - 0 ratings