Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use object of type stdClass as array in ComposerJson.php #1

Open
MaffooBristol opened this issue Jun 25, 2014 · 19 comments
Open

Comments

@MaffooBristol
Copy link

Hey, I'm getting the error listed below whilst trying to use xautoload.

Brief overview: I'm currently making a wrapper module for the PHPWord library, which uses the namespace \PHPOffice\PHPWord. I'm adding the 3rd party API using the libraries module, and I've tried using xautoload as a closure within hook_libraries_info() and as a separate hook. Code snippet is as follows:

function phpword_libraries_info() {
  return array(
    'phpword' => array(
      'name' => 'PHPWord',
      'vendor url' => 'https://github.com/PHPOffice/PHPWord',
      'download url' => 'https://github.com/PHPOffice/PHPWord/releases',
      'version arguments' => array(
        'file' => 'VERSION',
        'pattern' => "/(\d+\.\d+\.\d+)/",
        'lines' => 1,
      ),
      'path' => 'src/PhpWord',
      'files' => array(
        'php' => array(
          'Autoloader.php',
        ),
      ),
    ),
  );
}
function phpword_xautoload($adapter) {
  $adapter->absolute()->composerJson('sites/all/libraries/phpword/composer.json');
}

The error is here:

  static function createFromData($data, $path_prefix) {
    return empty($data['target-dir']) // <--- this line
      ? new self($data, $path_prefix)
      : new ComposerJsonTargetDir($data, $path_prefix)
      ;
  }

When I tried to investigate the cause of the error, I saw that the $data variable is indeed a class not an array, and that there is no 'target-dir' property available, which is peculiar as I was to believe that I'd followed the same instructions as in the documentation.

Any ideas? Thanks :)


( ! ) Fatal error: Cannot use object of type stdClass as array in sites/all/modules/contrib/xautoload/lib/Discovery/ComposerJson.php on line 47

N Time Memory Function Location
1 0.0001 688856 {main}( ) ../index.php:0
2 0.0009 791312 drupal_bootstrap( ) ../index.php:24
3 0.0720 2548088 _drupal_bootstrap_full( ) ../bootstrap.inc:2212
4 0.1317 7598264 menu_set_custom_theme( ) ../common.inc:5147
5 0.1317 7598344 menu_get_custom_theme( ) ../menu.inc:1770
6 0.1317 7598936 module_invoke_all( ) ../menu.inc:1748
7 0.1323 7601968 call_user_func_array ( ) ../module.inc:857
8 0.1324 7602336 xautoload_custom_theme( ) ../module.inc:857
9 0.1332 7605512 Drupal\xautoload\ClassFinder\ProxyClassFinder->onFinderInit( ) ../xautoload.module:46
10 0.1332 7605512 Drupal\xautoload\FinderOperation\HookXautoloadOperation->operateOnFinder( ) ../ProxyClassFinder.php:73
11 0.1655 7718632 phpword_xautoload( ) ../HookXautoloadOperation.php:21
12 0.1656 7719072 Drupal\xautoload\Adapter\ClassFinderAdapter->composerJson( ) ../phpword.module:84
13 0.1667 7725552 Drupal\xautoload\Discovery\ComposerJson::createFromFile( ) ../ClassFinderAdapter.php:102
14 0.1671 7747976 Drupal\xautoload\Discovery\ComposerJson::createFromData( ) ../ComposerJson.php:35
@MaffooBristol
Copy link
Author

I was able to get it working (only by) using the following, however it required the external library being inside the module file, which I don't want:

function phpword_xautoload($adapter) {
  $adapter->addPsr4('PHPOffice\PHPWord\\', 'phpword/src/PhpWord');
  $adapter->addClassmapSources(array('src'));
}

Furthermore, another big issue I've found so far is that if I try and use 'xautoload' => function() {} in my libraries_info definition, then clear caches, libraries_load() throws up the following error:

Serialization of 'Closure' is not allowed in serialize() (line 450 of /.../includes/cache.inc)

Libraries is the latest version too.

Curiouser and curiouser...

Cheers, Matt

Update: Seems that using a string as a reference to the function like in this fixes the closure issue!

@donquixote
Copy link
Owner

Hi!
Please have a look at existing issues on drupal.org!
This stuff is known and documented and either is already fixed in 7.x-5.x, or will be fixed.
https://www.drupal.org/node/2291101
https://www.drupal.org/node/2279981

Also interesting:
https://www.drupal.org/node/1809254

@donquixote
Copy link
Owner

I will have more time for this on the weekend or beginning of next week.
Until then, it would be great if you could look at the existing issues and confirm if what I wrote there does help.

@donquixote
Copy link
Owner

There was more stuff broken in ComposerJson and ComposerJsonTargetDir.
Now fixed, and added much-needed PHPUnit tests for this stuff.
1952498...2662bf7

@donquixote
Copy link
Owner

Serialization of 'Closure' is not allowed

I thought this is fixed since 7.x-5.0-beta2.
Any further feedback welcome!

@donquixote
Copy link
Owner

7.x-5.0-beta3 released.

@MaffooBristol
Copy link
Author

Hi Andreas,

Thanks so much for the speedy reply. I bumped the version up, played about a bit and finally actually got it to work from libraries, so I'm pretty pleased now!

Final version required the following:

'xautoload' => function($adapter) {
  $adapter->addPsr4('PHPOffice\PHPWord\\', 'src/PhpWord');
  $adapter->ComposerJson('composer.json');
}

I think partially I was confused that I would have to register it as PSR-4, as that's mentioned in the composer.json file- or is the information supplied there not enough for xautoload to register it?

Many thanks once again!
Matt

@donquixote
Copy link
Owner

I think partially I was confused that I would have to register it as PSR-4, as that's mentioned in the composer.json file- or is the information supplied there not enough for xautoload to register it?

If the addPsr4() is necessary then there is something wrong.
Is this the composer.json? https://github.com/PHPOffice/PHPWord/blob/master/composer.json

@donquixote donquixote reopened this Jun 30, 2014
@MaffooBristol
Copy link
Author

It is indeed the correct file- but yeah, it didn't work without the addPsr4()...

@donquixote
Copy link
Owner

I am playing around with this myself now.

On Mon, Jun 30, 2014 at 3:23 PM, Matt Fletcher [email protected]
wrote:

It is indeed the correct file- but yeah, it didn't work without the
addPsr4()...


Reply to this email directly or view it on GitHub
#1 (comment)
.

@donquixote
Copy link
Owner

It works for me without addPsr4(), but you might have to clear a cache.

@MaffooBristol
Copy link
Author

Strange. I commented out the addPsr4 line, drush cc all'ed, ran my test script and as before it couldn't find the classes any more, so I've not been able to reproduce it without that line unfortunately 😩

@donquixote
Copy link
Owner

Can you share the module? And the place where you use the library classes?

@MaffooBristol
Copy link
Author

I can, but as it's currently part of a big, closed-source system I'll have to migrate it out to an open repo. Bear with me!

@MaffooBristol
Copy link
Author

Okay, here it is so far https://github.com/MaffooBristol/drupal-phpword (very early stages). Libraries/xautoload hooks are in the module file, class usage is in the inc file.

Library's been added in as sites/all/libraries/phpword/etc/etc too

Thanks

(PHPWord version 0.11.1 btw)

@donquixote
Copy link
Owner

Ok, what is the class that is not found, and what is the stack trace?
I need to know from where this is called, so which phase of the request we are in.

@MaffooBristol
Copy link
Author

Here: https://github.com/MaffooBristol/drupal-phpword/blob/master/phpword.inc#L63
screenshot from 2014-06-30 15 39 28

(The first instance of trying to call a class that is in a file that's not defined within hook_libraries_info())

@donquixote
Copy link
Owner

Got it. It's a spelling thing - uppercase/lowercase. It must be "PhpOffice\PhpWord..", not "PHPOffice\PHPWord.."

And btw you don't need the \PHPOffice\PhpWord\Autoloader::register(), if you have xautoload doing the job.

@MaffooBristol
Copy link
Author

Oh god... you're right. Haha. I've seen and written it so far as phpword, phpWord, PhpWord, PHPWord, PHPword... at least we sorted out it was my fault in the end ;)

Thanks for the tip too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants