Symfony 2.1, composer and git repositories - symfony

With Symfony 2.0.x I store all my client side dependencies (jQuery, etc) in the deps file so I can easily update them all at once with vendor/install, with the switch to composer in 2.1 this is not possible. My options appear to be:
Fork all repos and add in the composer.json file (pain in the butt and waste of time)
Manually download them all and stick them inside my repo somewhere (also a pain in the butt)
Write my own Grunt script or something similar
Does anyone have a solution for handling this, or am I going about it all wrong?

Composer does have support for downloading libraries that are not Composer-aware. It's a little more work, but you can define each of your dependencies like this:
{
"repositories": [
{
"type": "package",
"package": {
"name": "jquery/jquery",
"version": "1.8.1",
"dist": {
"url": "http://code.jquery.com/jquery-1.8.1.min.js",
"type": "file"
}
}
}
],
"require": {
"jquery/jquery": "1.8.1"
}
}
Read more about it here: http://getcomposer.org/doc/05-repositories.md#package-2.
This will download jQuery to vendors/jquery/jquery by default. I don't think there's a way to specify a directory outside of vendors at the moment, so that may considerably limit the usefulness of this suggestion.
FWIW, I would consider submitting a pull request/issue to the Composer Github project. This actually would make a whole lot of sense.

Related

can i use PHP standard recomendation(PSR) in wordpress?

Can i use PSRs in wordpress vs the WordPress Coding Standards?
I am going to create a plugin but I want to use psrs and take advantage of all its features.
psr4
libs
Absolutely, and it is best to use it for plugin development.
But as you know it is not recommended by the wordpress community one reason is it can be problematic if other plugins or the theme share identical packages installed via composer
But to use PSR4 or somthing similar just start your project with a composer.json and structure it with the correct file path for development for your plugin. Look at the example snippet below declaring autoload in composer.json followed by the PSR and the path
EXAMPLE: composer.json
{
"name": "author/myplugin",
"type": "wordpress-plugin",
"description": "Description",
"require": {
"php": ">=7.4"
},
"autoload": {
"psr-4": {
"MYPLUGIN\\": "src/Includes/"
}
}
}

WordPress Bedrock Advanced Custom Field as dependency

I am setting up a WordPress site with BedRock and was thinking if there is a way to "require" the ACF plugin in my composer.
So the plan is to have clean and simple repo where you do a git pull and composer install and then everything gets installed (including plugins)
But I don't find any documentation on the ACF site on how to do this.
Is this even possible? lol
I found this package but it's for ACF Pro and I just need the free version.
https://github.com/PhilippBaschke/acf-pro-installer
If anyone has any experience with BedRock and can help me out that would be greatly appreciated :)
Many thanks in advance!
If you add the wpackagist repository to Composer's "repositories", you can then require the ACF free version.
In Bedrock's composer.json add the wp-packagist repo:
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
],
...
Then in the same file, require the plugin:
"require": {
"php": ">=5.6",
"composer/installers": "~1.0.12",
"vlucas/phpdotenv": "^2.0.1",
"johnpbloch/wordpress": "4.7.2",
"oscarotero/env": "^1.0",
"roots/wp-password-bcrypt": "1.0.0",
"roots/soil": "3.7.1",
"wpackagist-plugin/advanced-custom-fields" : "4.1.*"
},
...
Run composer update and it should fetch the plugin.
Side note, the https://github.com/PhilippBaschke/acf-pro-installer tool is no longer supported and doesn't work with Composer 2 or ACF versions > 5.10.x
To fix the second issue with ACF versions, in src/ACFProInstaller/Plugin.php edit line 187 to:
$major_minor_patch_optional = '/\A\d\.\d{1,2}\.\d{1,2}(?:\.\d)?\Z/';
For Composer 2 compatibility, see
https://github.com/pivvenit/acf-pro-installer and https://github.com/ffraenz/private-composer-installer are two options for replacements.
https://github.com/PhilippBaschke/acf-pro-installer/issues/44 describes the differences concisely.

Composer install wp-plugin to vendor dir

I'm using Roots/Bedrock for my WordPress structure and I want to use WebDevStudios/CMB2 as a library and not as a plugin.
The Roots/Bedrock composer.json specifies that dependencies of type:wordpress-plugin be installed in app/plugins. The WebDevStudios/CMB2 composer.json declares that it is a wordpress-plugin type, so it gets installed into app/plugins which is not where I want it.
How can I get this dependency to be installed into vendor and not app/plugins?
I have a suspicion I might have to fork CMB2 and change it's type from wordpress-plugin to library, but I'm hoping there is a cleaner solution.
I'm not using Roots/Bedrock but I had a similar problem when adding CMB2 as a dependency to a plugin (rather than loading it as a separate plugin). It was installing the plugin in wp-content/plugins instead of vendor. The following worked for me.
{
"require": {
"webdevstudios/cmb2": "^2.2",
},
"autoload" : {
"files": [
"vendor/webdevstudios/cmb2/init.php"
]
},
"extra": {
"installer-paths": {
"vendor/webdevstudios/cmb2": ["webdevstudios/cmb2"]
}
}
}
The key was the installer-paths entry that tells Composer where we want to install webdevstudios/cmb2.
I wrote a blog post about this at https://salferrarello.com/cmb2-composer-dependency/

How to compile .less files on save in Visual Studio 2015 (preview)

Ok, so I've created a new ASP.Net 5/MVC 6 project in Visual Studio 2015 Preview. In keeping with our current method of doing things, for styling I want to use .less files. Creating the files is straightforward, but Web Essentials no longer compiles them.
So my question is this: what precisely do I need to do to get my .css files generated when I save the .less files?
Based on my adventures getting Typescript to work nicely, I will have to use Grunt to accomplish this task, but I am brand-new to Grunt and so I'm not sure how one would do it?
Please help!
With VS 2015 Web Essential is split into multiple extensions you can download
the Web Compiler extension from here and it also has details on how to use it.
It is certainly not elegant as it used to be, but if you are using existing project and want to use a compiler for LESS then this may do the basic job.
So here's how to do it (compile on build and non-elegant compile on save):
Step 1
Open up your package.json file (it's in the root of your project) and add these lines:
"grunt-contrib-less": "^1.0.0",
"less": "^2.1.2"
Obviously you can change the version numbers (you'll get helpful intellisense), these are just the current versions.
Step 2
Right-click on the NPM folder (under Dependencies) and click Restore Packages. This will install less and grunt-contrib-less.
Step 3
Once those packages are restored, go to your gruntfile.js file (again, in the root of the project). Here, you'll need to add the following section to grunt.initConfig
less: {
development: {
options: {
paths: ["importfolder"]
},
files: {
"wwwroot/destinationfolder/destinationfilename.css": "sourcefolder/sourcefile.less"
}
}
}
You'll also need to add this line near the end of gruntfile.js:
grunt.loadNpmTasks("grunt-contrib-less");
Step 4
Then just go to View->Other Windows->Task Runner Explorer in the menu hit the refresh icon/button, then right-click on less under Tasks and go to Bindings and tick After Build.
Hooray, now less files will compile and we (I) learned about grunt, which seems really powerful.
Step 5: Compiling on save
I still haven't got this working to my satisfaction, but here's what I've got so far:
As above, add another NPM package grunt-contrib-watch (add to package.json, then restore packages).
Then add a watch section in gruntfile.js, like this (obviously this can work for other types of files as well):
watch: {
less: {
files: ["sourcefolder/*.less"],
tasks: ["less"],
options: {
livereload: true
}
}
}
So you'll now have something like this in your gruntfile.js:
/// <binding AfterBuild='typescript' />
// This file in the main entry point for defining grunt tasks and using grunt plugins.
// Click here to learn more. http://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409
module.exports = function (grunt) {
grunt.initConfig({
bower: {
install: {
options: {
targetDir: "wwwroot/lib",
layout: "byComponent",
cleanTargetDir: false
}
}
},
watch: {
less: {
files: ["less/*.less"],
tasks: ["less"],
options: {
livereload: true
}
}
},
less: {
development: {
options: {
paths: ["less"]
},
files: {
"wwwroot/css/style.css": "less/style.less"
}
}
}
});
// This command registers the default task which will install bower packages into wwwroot/lib
grunt.registerTask("default", ["bower:install"]);
// The following line loads the grunt plugins.
// This line needs to be at the end of this this file.
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-less");
grunt.loadNpmTasks("grunt-contrib-watch");
};
One can then simply set this task to run on Project Open (right-click on watch under Tasks in the Task Runner Explorer (it's under View->Other Windows in the top menu) and you're done. I would expect you'd have to close and re-open the project/solution to get this to kick in, otherwise you can manually run the task.
(Note: there is now a new question asked here directly concerning sass. I tried to alter the question and tags in this question to include sass, but someone didn't allow it.)
I would like to add the answer to the same question for sass (.scss). The answer is so related I think these may best be combined as two answers in this same post (if you disagree, please let me know; else, we might add "or sass" in the post title?). As such, see Maverick's answer for some fuller details, here's the nutshell for sass:
(Pre-step for Empty Projects)
If you started with an empty project, first add Grunt and Bower:
Right click solution -> Add -> 'Grunt and Bower to Project' (then wait for a minute for it to all install)
package.json:
"devDependencies": {
"grunt": "^0.4.5",
"grunt-bower-task": "^0.4.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-contrib-sass": "^0.9.2"
}
(FYI: grunt-contrib-sass link)
Then:
Dependencies -> right-click NPM -> Restore Packages.
gruntfile.js
1) Add or make sure these three lines are registered near the bottom (as NPM tasks):
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-sass");
2) Again in gruntfile.js, add init configurations, something like the following.
{ Caveat: I am no expert on such configurations. I found the sass configuration on an excellent blog post some time ago that I can't locate at this time in order to give credit. The key was I wanted to find all files in the project within a certain folder (plus descendants). The following does that (notice "someSourceFolder/**/*.scss", and see important related note here). }
// ... after bower in grunt.initConfig ...
"default": {
"files": [
{
"expand": true,
"src": [ "someSourceFolder/**/*.scss" ],
"dest": "wwwroot/coolbeans", // or "<%= src %>" for output to the same (source) folder
"ext": ".css"
}
]
},
"watch": {
"sass": {
"files": [ "someSourceFolder/**/*.scss" ],
"tasks": [ "sass" ],
"options": {
"livereload": true
}
}
}
Now follow the instructions for Task Runner Explorer as given in the other answer. Make sure to close and reopen project. It seems you have to run (double click) 'watch' (under 'Tasks') every time the project is started to get the watch watching, but then it works on subsequent saves.

Contributing to open source bundles from vendor directory?

Ideal Situation
Often while working on a Symfony2 project I will spot something I want to change in one of my dependencies. If I could find a way to simply change it in vendor and then easily push the changes as a pull request then I would probably contribute more often (rather than overriding the part with a local child bundle).
The Problem
I can't change a vendor directory without composer freaking out on the next update. If I submit a pull request then it may take quite some time before I can actually use the code in vendors, which is actually a deterrent from contributing my new functionality.
How I do it now
The way I typically contribute to a bundle is to make a fork, put the fork in a barebones symfony standard-edition app, make the change and then submit a pull request.
Put fork in composer.json?
The only solution I can think of, is removing the packagist dependency of the bundle I am editing, and then including my fork with composer (as a package) from github. That way I get my code immediately and can still contribute.
Is this the only solution? How do you do it?
Any tips/advice for contributing to a bundle while working on a different project at the same time would be appreciated!
Nah... this is broken.
I've tried the official way to include a fork, here's an example (original:kitano, fork: jstoeffler) of the composer.json :
(For those who are in a hurry: THIS DOESNT WORK)
"repositories": [
//...
{
"type": "vcs",
"url": "https://github.com/jstoeffler/KitanoConnectionBundle",
},
//...
],
It keeps using the original bundle. Don't know what the problem is, and I don't get how everything works, but here's how I successfully add a fork to a project.
"repositories": [
//...
{
"type": "package",
"package": {
"name": "kitano/connection-bundle",
"version": "dev-master",
"source": {
"url": "https://github.com/jstoeffler/KitanoConnectionBundle.git",
"type": "git",
"reference": "master"
},
"autoload": {
"classmap": [""]
}
}
},
//...
],
[UPDATE: Answer Not Valid Anymore]
As pointed out in one of the comments, this answer is a couple years old and not correct anymore. See answers below for the correct way to proceed.
[Original answer below]
This is the approach recommended by Jordi Boggiano (#Seldaek), creator of composer.
See from his talk at Symfony Live San Francisco earlier this year (at the 2 minutes mark):
http://www.youtube.com/watch?list=PLo7mBDsRHu11ChvScWUE7MN1Qo5QVHQEz&feature=player_detailpage&v=P3NwF8RV1lY#t=120s
As of 2017 the proper way to do it is:
Add your GitHub branch link to the repositories
"repositories": [
{
"type": "vcs",
"url": "https://github.com/crimson-med/yii2-link-preview"
}
],
Add the source to the require of your composer.json
"require": {
"yii2mod/yii2-link-preview": "dev-master"
},
FYI, I just tried the very first option:
"repositories": [{
"type": "vcs",
"url": "https://github.com/thujohn/twitter"
}],
"require": {
"laravel/framework": "4.2.*",
"thujohn/twitter": "dev-master",
"anahkiasen/flickering": "^0.1.2",
"fairholm/elasticquent": "dev-master",
"facebook/php-sdk-v4" : "~5.0"
},
An it worked fine.
vagrant#dev:/var/www$ sudo php composer.phar update
Loading composer repositories with package information Updating dependencies (including require-dev)
- Removing thujohn/twitter (2.0.4)
- Installing thujohn/twitter (dev-master 7a92118)
Downloading: 100%
Writing lock file
Generating autoload files
> php artisan clear-compiled
> php artisan optimize
Generating optimized class loader
I just needed to specify the "master" branch name as "dev-master".

Resources