I'm trying to organize my project with a base code and external bundles as a plugins for the base project.
I've already registered the bundles in packagist and the "composer.phar install" command works as expected. The autoloader also works as expected, so everything is OK but the "php console doctrine:generate:entities ACSACSPanelDynHostBundle" command don't work at all. It returns the next message:
[RuntimeException]
Can't find base path for "ACSACSPanelDynHostBundle" (path: "/var/www/acspanel/vendor/acs/paneldynhost", destination: "/var/www/acspanel/vendor/acs/paneldynhost").
I tryied to use psr-4 in composer.json to define the namespace but it don't works either.
I would like to know what is the proper way to use the console commands with my vendor bundles.
The code of my bundle composer.json is:
{
"name": "acs/paneldynhost",
"description": "Dynhost service creation for ACSPanel",
"keywords": ["dynamic domain","acspanel"],
"type": "
"autoload": {
"psr-4": { "ACS\\ACSPanelDynHostBundle\\": "" }
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
Related
I am writing a composer package that I want to use in multiple CodeIgniter projects. Answers to that question can explain how to modify both package and application.
The package
I created a composer package called steevedroz/codeigniter-twig. Its composer.json looks like this:
{
"name": "steevedroz/codeigniter-twig",
"type": "library",
"description": "A simple CodeIgniter library that allows using Twig along with the usual CI helpers",
"keywords": ["codeigniter", "twig"],
"homepage": "https://gitlab.com/SteeveDroz/codeigniter-twig",
"license": "MIT",
"authors": [{
"name": "SteeveDroz",
"role": "Developer"
}],
"require": {
"php": ">=7.0",
"twig/twig": "^2.5"
},
"autoload": {
"psr-4": {
"CodeigniterTwig\\": "/"
}
}
}
It contains a main class that I'd like to load from CodeIgniter, located at the root of the package. It's called Twig.php and contains:
<?php
namespace CodeigniterTwig;
class Twig {
// ...
}
The CodeIgniter project
My POC project is a basic CodeIgniter project with a composer.json file at the root (next to application/, system/, etc.) that contains:
{
"require": {
"steevedroz/codeigniter-twig": "dev-master"
}
}
I ran composer install that loaded my package correctly.
In application/config.php, I set:
$config['composer_autoload'] = '/vendor/autoload.php';
I one of my controllers, I'd like to load my Twig class as a library like this:
$this->load->library('CodeigniterTwig/twig');
But when I check the result, I get the error:
Unable to load the requested class: Twig
I tried with different namespaces (see below), but the result is always the same!
steevedroz/CodeigniterTwig/twig
CodeigniterTwig/twig
twig
steevedroz/codeigniter-twig/twig
I guess the problem has to do with namespaces.
Any clue?
I want to make a composer package. However, I am still in the development phase, would or would but the earlier test out.
I have an empty vendor folder with the autoloader from composer:
/vendor
/composer
autoload.php
So now I've tried my package "simulate" and creates my folder structure and composer.json:
/vendor
/composer
/me
/package
/src
/tests
composer.json
autoload.php
This is my composer.json:
{
"name": "me/package",
"description": "",
"license": "",
"authors": [
{
"name": "",
"email": ""
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0"
},
"autoload": {
"psr-4": {
"Me\\Package\\": "src/"
}
}
}
And here is my class:
namespace Me\Package;
class Test {
// ...
}
If I want to call it:
if(file_exists('vendor/autoload.php')) require 'vendor/autoload.php';
$test = new \Me\Package\Test();
i become Fatal error: Class 'Me\Package\Test' not found.
Of course, I also inserted a composer.json in the root directory, but I can still bad at require my package state since it was not published, right? But how do I test it then and say to composer he should autoload my package?
If you want to use composer to include a package that is not listed on http://Packagist.org/ you would add a 'repositories' stanza into the composer.json (project root file). This reads the project, and gets the composer.json from it, using the name for the main-'requires' section.
"repositories": [
{
"type": "vcs",
"url": "https://github.com/example/private-repo.git"
}
}
The 'url' part, can also in fact be any valid URL for a git, SVN or HG repository - even a file:// based reference.
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.
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.
Background
I know what I'm trying to do sounds a bit wrong but I do have my reasons.
Basically I have a central core app that's a default laravel app with a few tweaks and boilerplate code, I have then developed a series of packages that can be used to extend the app through composer. These packages are not meant to function without the core framework so a dependency upon it is fully expected.
What I want to do
What I would like to do is have a BaseController in my core app and have the various controllers in my package extend this BaseController to provide universal functionality throughout the various module packages.
I was expecting to be able to place the base controller in app/controllers/BaseController.php
and then extend it from my package using:
class PackageController extends \BaseController{}
Unfortunately when I do this it still looks within the package (currently workbenched) for the controller and I get the error:
include(/var/www/l4core.dev/workbench/myvendor/mypackage/src/controllers/BaseController.php):
failed to open stream: No such file or directory
Can anyone tell me what I'm doing wrong here. I am looking for a solution which allows me to easily move my packages between vendor dir and workbench for development. Any help greatly appreciated
Update
The previously mentioned error message appears to have been due to an include in my packages /vendor/composer/classloader.php - I have now deleted the vendor directory and done a fresh composer install. This has not solved the problem but it has at least shifted it as I now get the following error message:
Class 'BaseController' not found
My Packages composer.json
{
"name": "modules/sesame",
"description": "",
"authors": [
{
"name": "any",
"email": ""
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.0.x",
"zizaco/confide": "dev-master",
"zizaco/entrust": "dev-master",
"conarwelsh/mustache-l4": "dev-master"
},
"autoload": {
"classmap": [
"src/controllers",
"src/models",
"src/migrations",
"src/seeds"
],
"psr-0": {
"Modules\\Sesame": "src/"
}
},
"minimum-stability": "dev"
}
Be sure to execute:
php artisan dump-autoload
And verify that your class BaseController is in /vendor/composer/autoload_classmap.php.
OR like the OP stated, removing the vendor directory and running composer install again could sometimes solve the problem.