Roots Discourse

Sage 9 psr-4 autoload and Bedrock deploy


#1

Hi guys, can someone help to explain this?

Sage 9 comes with Composer PSR-4 autoloading. This feature is great, it allows utilization of third-party libraries while keeping code and file structure nice and neat and I immediately made use of it. I was using dump-autoload command in themes/sage directory which re-generates the vendor/autoload.php file and everything worked fine while I was developing theme locally. However when it came to deployment of my bedrock-based website via Trellis I got confused, because Bedrock itself is using Composer to manage dependencies and it already has composer.json file.

So my question is: when using Sage 9 with Bedrock should autoload be moved from composer.json in themes/sage into composer.json in Bedrock root directory?

  "autoload": {
    "psr-4": {
      "Roots\\Sage\\": "src/lib/Sage/",
      "MySiteClasses\\": "src/lib/MySiteClasses/"
    }

If not, then how do I run Composer install in themes/sage/ directory during deploys?


#2

I’m using the same feature and I find having two separate composers extremely helpful.
The reason being that if I want to include into my project components that are must use but not plugins I can include them outside of my theme, or whenever I require some plugin via wppackagist, they get managed and installed in the proper structure.
The composer within my theme serves as gathering point for all the logic that I need within my theme (e.g. helper functions, additional models, custom post types and such) and that is strictly required for the theme to function.

I’m not using trellis for deployments though so sorry but I cannot help you on that.
The way that I’m working now I simply included my vendor theme folder in my deployment process.


#3

Thanks @Nicolo_Sacchi . That what I initially thought. But when I run deploy via Trellis, I get an error on Update WP theme paths task, saying that classes from lib/ are missing, so I suspect, that composer Install command was not run in themes/sage and vendor/ directory is not being created.

TASK [deploy : Update WP theme paths] ******************************************
System info:
  Ansible 2.0.2.0; Darwin
  Trellis at "Fix #468 - Use curl to install wp-cli tab completions"
---------------------------------------------------
PHP Fatal error:  Uncaught Error: Class 'WPBasic\Navigation\NavWalker' not
found in /srv/www/staging.example.com/releases/20160616093905/web/
app/themes/sage/src/setup.php:115

#4

Have you tried adding a composer install command to /trellis/deploy-hooks/build-before.yml?

Untested pseudo-code:

- name: Run Composer install in Sage
   command: composer install
   args:
     chdir: "{{ deploy_helper.new_release_path }}/web/app/themes/sage"

#5

Added a snippet to /trellis/deploy-hooks/build-before.yml like @paul_tibbetts recommended. Even though Composer ran successfully, I’m still receiving same error as @Nicolo_Sacchi.

Snippet:

- name: Check for composer.json in sage
   stat:
     path: "{{ deploy_helper.new_release_path }}/web/app/themes/sage"
   register: plugin_composer_json

 - name: Fail if composer.json not found
   fail:
      msg: "Unable to find a `composer.json` file in the root of '{{ deploy_helper.new_release_path }}/web/app/plugins/suplifacil'. Make sure the theme has a `composer.json` file in its root."
   when: not plugin_composer_json.stat.exists

 - name: Install Composer dependencies for Sage
   command: composer install --no-ansi --no-dev --no-interaction --no-progress --optimize-autoloader --no-scripts
   args:
     chdir: "{{ deploy_helper.new_release_path }}/web/app/themes/sage"

Update:

Managed to have it working just fine by following this thread