CMB2 / CMB2

CMB2 is a developer's toolkit for building metaboxes, custom fields, and forms for WordPress that will blow your mind.
GNU General Public License v3.0
2.95k stars 573 forks source link

Support composer autoload #1185

Open slaFFik opened 6 years ago

slaFFik commented 6 years ago

At this moment developers need to mainly include init.php file to make the lib working, when the lib is installed via autoloader.

Please add support for composer autoloader, which is basically preserving composer.json file in a lib version zip bundle with several new lines in composer.json file: https://getcomposer.org/doc/04-schema.md#files

Downside: that file (and the whole lib) will be included on each request where it's used.

tivnet commented 5 years ago

A very important task.

jtsternberg commented 5 years ago

This is an oft-repeated request: https://github.com/CMB2/CMB2/search?q=composer+autoload&type=Issues

As composer isn’t my strongest suite, I’d encourage you to read through all those previous discussions before +1-ing here.

slaFFik commented 5 years ago

Yes, Justin, good threads over there, thank you. I will think about this.

szepeviktor commented 4 years ago

@jtsternberg Composer-izing the current CMB2 plugin could be done.

adding this to the current composer.json (in a PR)

"autoload": {
    "classmap": [
        "includes/"
    ],
    "exclude-from-classmap": [
        "includes/shim/WP_REST_Controller.php"
    ],
    "files": [
        "includes/helper-functions.php",
        "bootstrap.php"
    ]
},

install WordPress by Composer

{
  "name": "company/wordpress-project",
  "description": "Install WordPress with Composer.",
  "license": "MIT",
  "require": {
    "php": ">=7.0",
    "johnpbloch/wordpress": "^4.9",
    "wpackagist-plugin/wp-redis": "^0.8.0",
    "cmb2/cmb2": "^2.6.0"
  },
  "extra": {
    "installer-paths": {
      "vendor/cmb2/cmb2/": [
        "cmb2/cmb2"
      ],
      "public/wp-content/mu-plugins/{$name}/": [
        "type:wordpress-muplugin"
      ],
      "public/wp-content/plugins/{$name}/": [
        "type:wordpress-plugin"
      ],
      "public/wp-content/themes/{$name}/": [
        "type:wordpress-theme"
      ]
    },
    "wordpress-install-dir": "public/core"
  },
  "repositories": [
    {
      "type": "composer",
      "url": "https://wpackagist.org"
    }
  ]
}

and include this in your WordPress application's bootstrap

<?php

declare(strict_types = 1);

/**
 * Loader for CMB2.
 */

// Init action is soon enough.
\add_action(
    'init',
    function () {
        define('CMB2_LOADED', 9964);
        define('CMB2_VERSION', '2.6.0');
        define('CMB2_DIR', \APP_DIR . '/vendor/cmb2/cmb2/');
        \load_textdomain('cmb2', \CMB2_DIR . 'languages/cmb2-' . function_exists('determine_locale') ? determine_locale() : get_locale() . '.mo');
        \cmb2_bootstrap();
    },
    0,
    0
);

// APP_DIR may come from your code or determine vendor dir with these 2 lines of code.
// $reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class);
// $vendorDir = dirname($reflection->getFileName(), 2);

What do you think?

jtsternberg commented 4 years ago

Nooo, that is the absolute wrong way. It's crucially important CMB2 is loaded through init.php for a bunch of reason, but primarily so that it will only load one version of itself, the most recent. (so other plugins or the theme won't trigger a fatal error, etc). More is written here: https://github.com/CMB2/CMB2/wiki/Basic-Usage#caveats-for-bundling-and-including-cmb2

szepeviktor commented 4 years ago

I see. You are talking about a WordPress installation ran by a non tech-savvy person. The above code is for agency-type projects where you have full control over the development and installation environment, and using CI to deploy.

jtsternberg commented 4 years ago

In that case I would argue you should be able to control initiating it the way it was intended. Needless to say, if you do it your own way, your on your own with running into oddities.

szepeviktor commented 4 years ago

I've shortened init.php as it reflects that the WordPress installation ran by a non tech-savvy person which is not sustainable, e.g. 4 kinds of text domain loading.

jtsternberg commented 4 years ago

In your application's bootstrap, why would you not just do the following?

<?php

declare(strict_types = 1);

require_once <path_to_cmb2_dir> . '/init.php';

I can't see anything you would stand to gain trying to autoload with composer, and hooking things in manually than doing it this way and letting CMB2 handle the autoloading.

szepeviktor commented 4 years ago

I can't see anything

szepeviktor commented 4 years ago

I think there is no resolution: this is the clash of two Worlds 😿 projects where only the displayed pixels count and agency-type projects.