Composer Namespaces

Hi all,
I’ve been using composer for a little while now when building little apps for smaller tasks.
But using Bedrock looked like a great way to include some extra functions we use into Wordpress.

Today I tried adding GitHub - gilbitron/PHP-SimpleCache: A simple script for caching 3rd party API calls in PHP via composer.

By adding in to the required array "gilbitron/php-simplecache": "dev-master"

But when calling $cache = new Gilbitron\Util\SimpleCache(); in our functions file.

We get the error Fatal error: Class 'Gilbitron\Util\SimpleCache'

Am I completely missing something when it comes to name spaces for functions added or did I do something wrong.

Just checking, but did you run composer install after adding that? Bedrock requires composer’s autoload.php so as long as that’s been updated (which it does after install), then it should work. Can also try composer dump-autoload to be sure.

Hi Scott,

Thanks for the reply. Yes I ran update immediately after adding the require, but doesn’t seem to have done the trick. I’ve also tried composer dump-autoload

Feels like I’m missing the obvious.

I am adding this dependency in correctly by popping it into the end of the required array o.0?

"require": {
  "php": ">=5.3.2",
  "wordpress/wordpress": "4.0",
  "fancyguy/webroot-installer": "1.1.0",
  "composer/installers": "v1.0.12",
  "vlucas/phpdotenv": "~1.0.6",
  "gilbitron/php-simplecache": "~1.4"
}

Correct. Dotenv is used in the same way.

We don’t do anything special. Example: https://github.com/roots/bedrock/blob/master/config/application.php#L12

Oh of course. Run composer update instead :slight_smile:

edit:

composer install installs what’s in your composer.lock file. So when you add a new package and run install again, it won’t find it in the lock file yet. composer update will update the lock file and find new packages/versions.

Hi Scott,

Double checked with update this morning and still no luck. I’m guessing my problem may turn out to be environmental.

I tried removing the libraries and updating
`Loading composer repositories with package information
Updating dependencies (including require-dev)

  • Removing gilbitron/php-simplecache (1.4.1)
  • Removing hampel/json (dev-master 6af61ac)
    Writing lock file
    Generating autoload files
    `
    And re-adding them

`Loading composer repositories with package information
Updating dependencies (including require-dev)

  • Installing hampel/json (dev-master 6af61ac)
    Loading from cache
    Extracting archive

  • Installing gilbitron/php-simplecache (1.4.1)
    Loading from cache
    Extracting archive

Writing lock file
Generating autoload files`

But unfortunately the same simple $cache = new Gilbitron\Util\SimpleCache(); fails with the error Fatal error: Class 'Gilbitron\Util\SimpleCache' not found in /srv/users/serverpilot/apps/step/web/app/themes/step/lib/dataconnector.php on line 2

I’ve tested this quickly and I can’t replicate the issues you are having. It all works as intended.

What’s in dataconnector.php and when/how is it being loaded?

Hi Nick,

I’m pretty sure I’m doing it wrong but I just can’t put my finger on what. dataconnector.php was a new file I’d added to the includes in my roots theme. At present the only line it contains was $cache = new Gilbitron\Util\SimpleCache();

I’m not sure quite what it is but something in the hosting environment here is definitely causing problems. Serverpilot comes with composer pre-installed though.

For the moment I’ve moved the simplecache files directly into my theme.