6

I've got a problem here, which I have a feeling got a simple and clean solution which I haven't found yet... My Cake-PHP-Application looks like this:

  • Projectfolder
    • app
    • vendors
    • composer.json

Cake-PHP and the external file I want to use are installed inside the vendors-folder. In vendors I have a package for twitter bootstrap, which has a css- and a js-file I want to include inside my view, but it ain't accessable, since those file don't remain inside the webroot-Folder of my Cake-PHP project. Now my question is - How do I make those both files accessable inside my Cake-PHP project, WITHOUT copying them to the webroot-folder? Using symlinks looks a bit like a dirty hack to me... There has to be a clean solution, since otherwise using Cake-PHP with Composer would make no sence. My composer.json looks like this:

{
"name": "MyProject",
"version": "0.0.0",
"require": {
    "php": ">=5.5.11",
    "cakephp/cakephp": "2.6.3",
    "composer/installers": "*",
    "twbs/bootstrap": "3.3.4",
    "components/jquery": "2.1.3"
},
"extra" : {
  "installer-paths":{
      "plugins/{$name}":["type:cakephp-plugin"],
      "app/webroot/bootstrap":["twbs/bootstrap"]
  }
},
"config": {
    "vendor-dir": "vendors"
}

Btw: composer ignores the given installpath for bootstrap, since the package has no type...

Husky110
  • 723
  • 2
  • 10
  • 27
  • What doesn't make much sense IMHO is to use composer for frontend dependencies, you're better off using something like [bower](http://bower.io). – ndm Apr 02 '15 at 09:21
  • You might be right on that, but since I have to use Composer I use it and installing 2 packagemanagers seems weird to me... – Husky110 Apr 02 '15 at 09:35
  • Nothing weird about if you aske me, don't forget that composer is naturally a _PHP_ dependency manager, you'll run into problems with non-PHP packages all the time, resulting in having to use custom installers or the like. Anyways, check http://stackoverflow.com/q/25685722/1392379 (https://github.com/RobLoach/component-installer) or http://stackoverflow.com/q/19118367/1392379 – ndm Apr 02 '15 at 09:54
  • I see your approaches and I don't think that you are wrong on default, but there HAS to be a specific solution to this problem, since cake-php naturally supports composer. If that wouldn't be the case, the whole composer-cake-symbiosys would make no sense, except for just installating cake with composer and that's it. Every packagemanager I add, I have to install on the server as well which is bad in my case. :) – Husky110 Apr 02 '15 at 11:04
  • There are no solutions other than using custom composer installers (being it for generic packages or shim ones - is there any reason why you can't use this option?), copying/linking files, or making use of additional dependency managers, you may not like it, but that's simply how it is. – ndm Apr 02 '15 at 11:24
  • Again, composer is a PHP dependency manager (the defacto standard btw), and that's why it makes sense for a project like CakePHP, which, naturally, has PHP dependencies. Just do a quick google search, you'll see that there are a lot of PHP projects that are using separate frontend dependency managers, simply because composer has not (yet) evolved in this direction. Just wait and you'll probably also stumble over Grunt :P – ndm Apr 02 '15 at 11:25

2 Answers2

1

Using tws/bootstrap will require an additional step for the installation of these assets. This is often done by symlinking or copying.

For instance, when adding Bootstrap to Symfony2 you would require

 "require" : {
     "mopa/bootstrap-bundle": "dev-master",
     "twbs/bootstrap": "dev-master",
   },
    "scripts": {
        "post-install-cmd": [
            /* sensio commands **/
            "Mopa\\Bundle\\BootstrapBundle\\Composer\\ScriptHandler::postInstallSymlinkTwitterBootstrap"
        ],
        "post-update-cmd": [
            /* sensio commands **/
            "Mopa\\Bundle\\BootstrapBundle\\Composer\\ScriptHandler::postInstallSymlinkTwitterBootstrap"
        ]
    },

One package is the asset itselfs, the other packages provides the integration and "postInstall" handler for the asset.

For Cake you would have to find the package doing the installation job for tws/boostrap - maybe such a package is around in the Cake community.

But i would suggest using something easier, which works out of the box:

{
    "require": {
        "slywalker/boost_cake": "*"
    }
}

And then use enable CakePlugin::load('BoostCake'); and add the helpers you need.

As @ndm pointed out: you could also decide to work with Composer "bridges" to other asset managers (indirect). One of them is https://github.com/francoispluchino/composer-asset-plugin

Or you could work with these asset managers, like bower, npm directly.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

In your composer.json, make use of the scripts array.

With CakePHP 3.0, at least, this is pre-built as follows:

"scripts": {
    "post-install-cmd": "App\\Console\\Installer::postInstall",
    "post-autoload-dump": "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump"
},

The post-install-cmd script calls the postInstall function from /src/Console/Installer.php.

If you look at that function, you can see it is doing a few things after installation, such as setting file permissions.

In here, you can do anything else that you require, such as building a function to copy files from the /vendor/ folder, to your webroot folder, using standard PHP functions such as copy or rename (to move).

Warren Sergent
  • 2,542
  • 4
  • 36
  • 42