Newby question - How to include private github package to production build

Hey all,

I’m new to Sage, relatively new to composer and npm. And I’m working on a Sage 9 project. This far I’m loving it :slight_smile: I’ve successfully created my custom theme and even a Sage 9 child theme.

Now, I come to a point where I would like to make use of reusable code from my private github repos. For example I would like to:

  • Reuse php Classes for the admin backend in multiple projects. For example a class to check if my theme has a newer version available on my github and update.
  • Reuse Gutenberg blocks in multiple porjects.

What I’ve got so-far (not working properly)
Alright, for public repo’s I would add to my package.json

    "dependencies": {
      "theme-updater": "myGithubUsername/theme-updater#branch",
    }

Then in app/setup.php I could load it like:

    include_once dirname(__FILE__) . "/../node_modules/theme-updater/ThemeUpdater.php";
    new ThemeUpdater();

Alright, this might work in development. But it won’t work in production like this. Because, when I push the code, after building. to github it’s, obvious, without the node_modules. And my wordpress website doesn’t build the project. It simply downloads the theme zip and installs it regularly (I presume, I havn’t finished the update class yet :slight_smile: )

So two main questions:

  • How would I include my github snippits to my sage project in a way they are included in the local build.
  • How would I handle private github repos to grab the code from

Thoughts

  • Would this work in Sage 9 and Sage 10?

webpack is used in sage to bundle all the scripts and styles into single, minified files.
You don’t want to deploy the node_modules to production, hence the approach of requiring the PHP files in that npm package is the wrong one.
You have published your ThemeUpdater package already via npm, so you could also release the PHP part as a composer package or composer-installable git repository.
As this ThemeUpdater seems to be a functionality of the theme itself, it would make most sense to require it as a composer PHP library dependency in your sage theme.

Thanks @strarsis for the reply!

yes, I think I’m one step further. From what I understand now :

  • I should not add php libraries tot package.json
  • I should add them to require in composer.json
  • this installs it to /vendor
  • Sage will autoload the Class
  • After this I could call it with use
  • This will include it in the build (right?)

So as an example that is not yet working:

composer.json (is the repositories set correct?)

    "repositories": [
      {
        "type": "package",
        "package": {
          "name": "elliotjreed/php-package-boilerplate",
          "version": "7.4",
          "type": "git",
          "source": {
            "url": "https://github.com/elliotjreed/php-package-boilerplate.git",
            "type": "git",
            "reference": "php-7.4"
          }
        }
      }
    ],
    "require": {
      "elliotjreed/php-package-boilerplate" : "^7.4"
    },
cli: composer update

Now, from what I understand is that I could call it in every php file after functions.php line 19. From her on out it is autoloaded. Correct?

So, for example in Sage 10
/app/View/Composers/App.php

namespace App\View\Composers;

use Roots\Acorn\View\Composer;
use Example\Greeting;


class App extends Composer
{
    protected static $views = [
        '*',
    ];
    
    public function with()
    {
        return [
            'greetings' => $this->greetings(),
        ];
    }
    
    public function greetings(){
        $greeting = new Greeting('Ada Lovelace');
        return $greeting->sayHello();
    }
}

Then use $greetings in the page views.

This is after example from
https://github.com/elliotjreed/php-package-boilerplate/blob/master/public/example.php

But this does not seem to work. I get a blank screen in the browser using $greetings

If I place this code in functions.php after line 19 I get an error

use Example\Greeting;
$greeting = new Greeting('Ada Lovelace');
echo $greeting->sayHello();

The error is :

**Fatal error** : Uncaught Error: Class 'Example\Greeting' not found in /Applications/MAMP/htdocs/mysite/wp-content/themes/my-theme/functions.php:23

What am I missing here.

1 Like

Might have found a missing part:
within the composer.json I should add it to autoloader :

  "autoload": {
    "psr-4": {
      "App\\": "app/",
      "Example\\": "vendor/elliotjreed/php-package-boilerplate/src/Example/"
    }
  },
1 Like

Fixed.
Had to change the type to "type": "vcs", And now it works

1 Like

For anyone else looking into this, this guide covers how to do this:

https://roots.io/guides/private-or-commercial-wordpress-plugins-as-composer-dependencies/

2 Likes

:slight_smile: Yes!

So far I’ve only skimmed it, but what I don’t directly see in the post is how to use the code from the git.
You need to autoload and require/include it with use Classname in the file where you would like to use it, right?

There isn’t a single answer to this question because it depends on what kind of package you’re importing, and where you’re importing it (i.e. in Sage, Bedrock, etc).

The general guidelines are:

  • If what you’re pulling in is a WP plugin or mu-plugin, and you’re using bedrock, then its files will be put into plugins or mu-plugins and you would “use” them just as you would a normal WP (mu-) plugin.
  • If what you’re pulling in is a composer package, then Composer handles all of the autoloading for you when it’s loaded in Sage’s functions.php (or wherever it is that Bedrock loads Composer dependencies). To use it, you just need to call it by its namespace–i.e. \Classname\SomethingElse::method() or whatever. use is just for convenience–it aliases a full namespace to something shorter, so you don’t have to keep typing out the full namespace.

You might find it helpful to read up on how autoload and namespacing works: https://roots.io/namespacing-and-autoloading/

2 Likes

Thanks, makes more sense now.

This topic was automatically closed after 42 days. New replies are no longer allowed.