Composer Install Magento 2 as a Dev Dependency - php

I'm trying to develop a theme for Magento 2 by creating a composer module and loading Magento 2 as a dev dependancy by installing it into a subfolder and linking my theme in from a separate src/ subdirectory.
I'm currently trying the following in my composer.json file which results in the module being installed to the root directory:
"require-dev": {
"composer/installers": "*",
"magento/project-community-edition": "2.0.*"
},
"extra": {
"installer-paths": {
"magento/": ["type:product"]
}
}
From working on Magento 1 I was able to have the following directory structure to develop and test my module:
magento/
src/
composer.json
modman
With composer.json set as follows:
"require-dev": {
"composer/installers": "~1.0",
"magento/ce": "1.9.2.*"
},
"extra": {
"installer-paths": {
"magento": ["type:magento-source"]
}
}
Is this possible to do with Magento 2? Or would I be better running 2 different projects specifying my theme as a dependency and how would I link these together to develop on if so?

You can make use of composer's repository with artifact as
"repositories": [
{
"type": "artifact",
"url": "path/to/directory/with/zips/"
}
],
where path/to/directory/with/zips/ should contain the zipped package of your theme.
Also, you do not need dependencies on "composer/installers" or magento/project-community-edition only required will be "magento/framework"

Related

Composer - autoload classes in CodeIgniter outside vendor folder

I've been working on setting up a CodeIgniter project with composer. I'm wanting to include php classes stored in files outside the vendor folder - in a shared folder.
My directory structure:
/
--application/
--shared/
-application/
-class1.php
-class2.php
-class3.php
-base/
-classb1.php
--vendor/
--composer.json
--composer.lock
Looking at the composer documentation, I see there is an autoload property in the root package that I'm trying to use to load the classes in the shared directory. These classes aren't namespaced.
My composer.json file is as follows:
{
"description" : "The CodeIgniter Application with Composer",
"require": {
"php": ">=5.3.2",
"codeigniter/framework": "3.1.*"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*"
},
"autoload":{
"psr-0":{
"":"shared/application/",
"":"shared/base/",
"":"shared/data/"
}
}
}
My search led me to this question, but the classes are still not being loaded. I've ran composer update on the terminal.
Well after looking further, there's a property called classmap (documentation) in the root package.
"autoload":{
"classmap":["shared/application/","shared/base/", "shared/data/"]
}
This loads all the required files in the folders.

Composer not autoloading files included through repositories > type > path

The Problem
I have two PSR-4 composer projects and I wish to classes from one in the other, the file structure is as follows:
projectfoo
-public
--index.php
-src
--CompanyName
---Foo
----Foo.php
-composer.json
projectbar
-src
--CompanyName
---Bar
----Bar.php
-composer.json
The composer.json files are defined for projectfoo as (note the repositories > type > path dependency):
{
"name": "companyname/foo",
"require": {
"companyname/bar": "*"
},
"repositories": [
{
"type": "path",
"url": "../projectbar"
}
],
"autoload": {
"psr-4": {
"CompanyName\\": "src/CompanyName"
}
}
}
And projectbar as:
{
"name": "companyname/bar",
"version": "1.0.0",
"autoload": {
"psr-4": {
"CompanyName\\": "src/CompanyName"
}
}
}
Running composer update correctly produces the vendor folder containing the companyname/foo folder.
In index.php in projectfoo I have:
require_once '../vendor/autoload.php';
$bar = new \CompanyName\Bar\Bar();
However, when run \CompanyName\Bar\Bar is undefined.
The question
Why is Bar not being included in the autoload.php file?
What I've tried
Refreshing the autoload file using:
composer install
composer update
composer dump-autoload
Changing the vendor name CompanyName to something different.
Looking at the installed.json file in the vendor\composer folder the bar project is listed as:
#
{
"name": "companyname/bar",
"version": "1.0.0",
"version_normalized": "1.0.0.0",
"dist": {
"type": "path",
"url": "../companyname/bar",
"reference": "f35ed0ad82c8280db9b603712dd256074f99e196",
"shasum": null
},
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"CompanyName\\": "src/CompanyName",
}
}
}
Strangely, when bootstrapping the autoload.php file to phpunit the files autoload correctly and are available in my test classes.
The issue revolves around the way composer links "path" projects using symlinks.
In short
Because I was developing on a windows system and executing the code on an Ubuntu vagrant box the symlinks set up on windows, when running composer install were resolving to broken paths inside the box. This also explains why tests run on windows managed to locate Bar but not tests run in the virtual box.
Solutions
To solve this I initially forced composer to mirror the project rather than symlink it, however, this still caused issues with symlinks in the vendor folder.
I have now started hosting my libraries in seperate repositories and importing them using the "type": "vcs" tag which works well. Composer also handles updating package when new commits are made which doesn't happen when using the "type": "path" tag with mirroring.

Version Git Repo for Composer Use - Git/Composer

Scenario
I have a Plugin in PHP that I've written and want to keep it as a private repository. I want to set up the versioning in Git. I know there are tags for this, but don't know how the convention works for Composer.
Current composer.json for my package/plugin:
{
"name": "Test/Upload",
"description": "Useful functions for image uploading.",
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
}
}
Current composer.json for my project to include the above package/plugin:
{
"name": "multistepform",
"require": {
"cakephp/cakephp": "2.6.*",
"cakephp/debug_kit": "2.2.*"
},
"config": {
"vendor-dir": "Vendor/"
}
}
Currently I have no tags in the package/plugin.
Qs
1) Does Composer require tags in a repository in order for it to use it?
2) How should one tag their private repository for use with Composer?
3) How to include this package in a different project (using the above context)?
Just to add to #tigrang's comment and list out the explicit steps:
add your git repo using "repositories" in your composer.json
tag your package with a version number (I suggest using Semantic versioning, Keep A Changelog and my git plugin git-semver)
VERSION=0.1.0 && git tag ${VERSION} && git push origin ${VERSION}
add your package under require
you can also use "dev-master" in your require if you don't want to add version tags and want to get the latest changes to package when running composer update. This requires setting:
minimum-stability: dev
Final composer.json:
{
"name": "multistepform",
"repositories": [
{
"type": "git",
"url": "https://github.com/Test/Upload.git"
}
],
"require": {
"cakephp/cakephp": "2.6.*",
"cakephp/debug_kit": "2.2.*",
"Test/Upload": "0.1.*"
},
"config": {
"vendor-dir": "Vendor/"
}
}

Composer : how to add a dependency without network connection?

My profesionnal network block internet access. Some month ago I download the Silex framework from an archive (which contains composer.json file) and the composer.phar one's, then I transfer them on my desktop throught HDD.
My composer.json that I customized:
{
"name": "user/silex",
"require": {
"silex/silex": "1.2"
, "twig/twig": ">=1.8,<2.0-dev"
, "doctrine/dbal": "2.2.*"
, "symfony/security": "~2.3"
, "symfony/security": "~2.3"
},
"autoload": {
"psr-4": {
"Portal\\": "src/"
}
}
}
It works fine, my autoload customization too.
Today I want to add the monolog/monolog package, so I manually import it from an other computer.
I place it into my vendor folder, I add the following line to my composer.json file:
, "monolog/monolog": ">=1.0.0"
I run on the console:
php composer.phar dumpautoload
It outputs:
Generating autoload files
Then it stop without error, but the monolog namespace doesn't appear into my /vendor/composer/autoload_*.php files.
What did I miss?
Thanks to edmondscommerce's comment I found the solution:
I update my main composer.json file with an artifact respository (and I disable the packagist one):
{
"name": "user/silex",
"repositories": [
{
"type": "artifact",
"url": "artifact/"
}, {
"packagist": false
}
], "require": {
"silex/silex": "1.2"
, "twig/twig": ">=1.8,<2.0-dev"
, "monolog/monolog": "1.*"
, "doctrine/dbal": "2.2.*"
, "symfony/security": "~2.3"
},
"autoload": {
"psr-4": {
"Portal\\": "src/"
}
}
}
Then I put a folder called artifact according to the url put in the composer.json file.
I create into this folder a zip called monolog-monolog-1.8.zip with the library I want to add.
Then just launch a composer update command!
Be carefull, zip's root must contain a composer.json file, and this composer.json file must contain a version!
If you do not want to create a custom repository, you can also run composer install (or composer update) on a copy that is on a network-connected computer. Then you can copy over the newly added and extracted component into the vendor folder on the machine without internet access. Note that you also need to copy vendor/composer/installed.json to let composer know that the new package has been installed. Once you have copied all these files, you can run composer install on the machine without internet access and it will not try to install anything and dump autoload files.

Composer, Laravel and local packages

My issue is I have a package which isn't a repository and I am trying to get it to play nice with Laravel and composer. It is still located under the vendor folder, the only issue is that if I simply set:
"psr-0": {
"Test\\Test": "vendor/test/test/src/"
}
This will load the service provider but none of the controllers etc will autoload. What is the correct way to implement a package with larval that does not have it's own repository. Or does this go against the nature of packages and this should simply be structured under the applications controllers.
The package was created by me using workbench but I found i did not really need this as a separate repository but it would still be good to keep it as a package. Therefore the structure is exactly the same as a regular package:
vendor
testvendor
testpackage
public
src
tests
.gitignore
composer.json
phpunit.xml
UPDATE:
As a solution for the time being I am using:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"vendor/package"
]
},
As an entry in the class map. Looking forward I will probably refactor this into the app folder or create a repository for this package.
If you have some classes that you're calling "package", you're not supposed to add those files to your vendor folder. This folder is managed by composer and at any time you might loose it. Create a subfolder in your application and put those files there.
You have to be sure your PSR-0 autoloading will work for every single file in your folder structure. So, if your root is vendor/test/test/src/ and your namespace is
Test\\Test
All your files must be in
vendor/test/test/src/Test/Test/ClassFileName.php
PSR-4 is easier to deal and understand, this
"psr-4": {
"Test\\Test\\": "vendor/test/test/src/"
}
Means that your files would have to be like:
vendor/test/test/src/ClassFileName.php
Doublecheck your namespaces. It's easy to make mistakes when using namespaces with PSR-0 and remember that
composer dump-autoload
Must be ran every time you change things in composer.json or create new files. If it's a simple class autoloading, every time you create a file, if it's a PSR-X autoloading, everytime you create or update a namespace in your composer.json file.
If what you have is is really a package you should use Composer: when your package is structured as a composer package (check Laravel's composer.json as an example), the correct way of adding it to your application, if it's not list in Packagist, is via repositories.
You can have (non-packagist) packages in a public VCS repository:
{
"require": {
"monolog/monolog": "dev-bugfix"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/igorw/monolog"
}
]
}
You can have (non-packagist) packages in a protected by password VCS repository (git, bitbucket...):
{
"require": {
"vendor/my-private-repo": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:vendor/my-private-repo.git"
}
]
}
You can have your packages zipped in your hard drive and load them via the artifact repository type:
"repositories": [
{
"type": "artifact",
"url": "path/to/directory/with/zips/"
}
],
Though #Antonio Carlos Ribeiro's answer is really nice, I had problem with installing custom packages locally(which is also stated in the last part of his answer)
Let's assume this is the directory structure of the package we are trying to install:
D:/test_pack
src/
composer.json
If you do not want to upload your custom package (that most likely you have developed, yourself) to online repositories you can use one of the following two methods:
Method I
(You have to specify version for your package, otherwise you'll get this error: The requested package could not be found in any version, there may be a typo in the package name.)
1) In composer.json, Add version to your package. your package's json should look something like this:
{
"name": "gandalf/test_pack",//This is your package's name
"description": "some desc",
"version": "1.0.0",//This is the version that you have to specify
"authors": [
{
"name": "gandalf the grey",
"email": "fake#yahoo.com"
}
],
"minimum-stability": "dev",
"require": {
"laravel/framework": "~5.4"
},
"autoload": {
"psr-4": {
"Gandalf\\BotPack\\": "src/"
}
} }
2) zip your package(let's assume the zip file is in D:/test_pack/test_packa.zip)
3) In laravel's composer.json add your package name (in our case gandalf/test_pack into require part of json) and add the repository array to the composer.json file and in that array specify the directory in which your package's zip file exists(in our case D:/test_pack) . like this
{
...,
"require": {//adding our package name to laravel's composer.json
...,
"gandalf/test_pack": "*"//package's name
},
...,
"repositories": [
{
"type": "artifact",
"url": "D:/test_pack"
}
]
}
Method II(My Favorite method, You have to initialize your package directory as git local repository using git init and then git add . and git commit -m "your message")
1) initialize the package directory as git directory and commit all your changes to the local repository
(let's say D:/test_pack is the directory that contains your package(src/ directory and composer.json))
go to D:/test_pack directory and run these commands
git init
git add .
git commit -m "your message for this commit"
2) In your packages composer.json file add minimum-stability
{
"name": "gandalf/test_pack",
"description": "some desc",
"authors": [
{
"name": "gandalf the grey",
"email": "fake#yahoo.com"
}
],
"minimum-stability": "dev",//setting minimum-stability
"require": {
//dependencies that your package needs
},
"autoload": {
"psr-4": {
"Gandalf\\BotPack\\": "src/"
}
}
}
3)In laravel's composer.json file require the "dev-master" of your package
{
...,
"require": {
...,//some dependencies that laravel needs
"gandalf/test_pack": "dev-master"//requiring dev-master from repository
},
"repositories": [
{
"type": "git",
"url": "D:/test_pack"//path of the local repository directory which contains your package
}
]
}
To any Laravel project load local packages. which is stored in your machine.
In laravel's (Project) composer.json file add
"autoload": {
"psr-4": {
"YourPackage\\Namespace\\": "./local_Package_path/src"
}
},
and fire command in Laravel Project directory
composer dump-autoload
Optional
If package is still not available in your Project. then
Register your package's Service Provider.
To register your service provider, you just need to add an entry to the array of service providers in the config/app.php file.
'providers' => [
/*
* Laravel Framework Service Providers...
*/
...
YourPackage\Namespace\PackageServiceProvider::class,
],
Hope now your package loaded successfully in your laravel project.

Categories