I want to put some code that I shall use in different projects outside the app's bundles. I put it in a new folder in the vendor folder but I can't included in the project. Which are the steps?
Should I follow a special structure of folder and namespaces
How do I included in my project.
On Google I found a lot of ambiguous answers. I tried also this but it did not work.
You need to get your library loaded. You do this by telling composer where to find your library.
The best thing would be to set up a git repository for this source code and then use composer to load it, keep it up to date and include it into the autoloader. You may need to get used to composer to do this, but as composer is getting the default dependency management tool for php, the time is not wasted. You can find the docs here: http://getcomposer.org/doc/
If you don't want to do this, you can add a new src code folder to composer. Let's say your folder structure is like this:
symfony2/
vendor/
yourlibrary/
Bla/
Blub/
MyClass.php
First, the MyClass.php should have the namespace Bla/Blub defined. Else Then you can add your library like this to the composer.json file:
"autoload": {
"psr-0": {
"": "src/",
"Bla": "vendor/yourlibrary/"
}
}
You already have autoload defined, so you need to overwrite it!
If your library doesn't have namespaces, you can define composer to load it nonetheless. I am not using this, so I can just link the docs where you can read it up: http://getcomposer.org/doc/04-schema.md#autoload
Related
Im trying to setup a pleasant way of working with wordpress and its plugins using composer. My question will be quite broad. How would you do it?
What i want is basically so it installs wordpress (which is now doing), but the plugins i specify get installed in a folder named "vendor" and not in the "plugins" folder. Why is that?
Here is my composer.json.
{
"name": "name",
"description": "name Wordpress",
"repositories":[
{
"type":"composer",
"url":"https://wpackagist.org"
}
],
"require": {
"timber/timber": "^1.3",
"johnpbloch/wordpress-core-installer": "^0.2.1",
"johnpbloch/wordpress": "^4.4"
},
"extra": {
"installer-paths": {
"wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
"wp-content/themes/{$name}/" : ["type:wordpress-theme"]
},
"wordpress-install-dir": "wp"
}
}
I'm pretty new to this idea of using composer for package management inside WP. But I found this interesting, so I looked into it.
The installation path is specific to the plugin. Also look for wpackagist-plugin vendor name. Others will probably not put code inside the wp folder.
If you require "wpackagist-plugin/advanced-custom-fields": "^4.4" for example it is installed inside the plugins folder, as desired. The vendor prefix ('wpackagist-plugin') is important, I believe, as the packages in their search have no prefixes.
Quick solution
Try using "wpackagist-plugin/timber-library": "^1.3"
It is placed into the plugins folder nicely and comes with all it's dependencies inside the plugin folder.
Some more explanation
timber/timber is actually pulled from packagist.org (https://packagist.org/packages/timber/timber) instead of wpackagist.org.
On wpackagist.org you can find timber-library which is the packaged equivalent (includes composer autoloader and other deps).
This recipe for paths control (for plugin developers) says that:
To make use of it your extension's composer.json should contain:
"type" : "wordpress-plugin",
After you installed your packages using composer install you'll see, that the package inside of vendor/timber/timber doesn't have that type.
In fact there's an older WordPress plugin called timber, that's stuck on version 0.8. It's succeeded by timber-library.
Using "timber/timber": "^1.3" as from packagist.org
If you want to use timber as pulled from packagist.org you can do so, if you place the following line at the top of your wp-config.php:
require __DIR__ . '/wp-content/vendor/autoload.php';
Then you'll have to deploy both, the vendor and the wp folder.
There's also a discussion on GitHub about how to use vendor libraries in WP projects.
Hope you get along with that info.
I've read Magento's DevDocs and Googled this problem but the usual missing registration.php answer doesn't apply here.
I'm on the released CE 2.0.0 version and I'm simply trying to enable my first minimum test module in magento/vendor/ but
bin/magento module:enable -c Tchsl_Test
results in:
Unknown module(s): 'Tchsl_Test'
I am basing this on the naming conventions and file positions of modules in vendor/magento/
In vendor/tchsl/module-test/etc/module.xml I have
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Tchsl_Test" />
</config>
In vendor/tchsl/module-test/registration.php I have
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Tchsl_Test',
__DIR__
);
What am I missing?
I had this same problem and after some tinkering, discovered that our Modules actually belong under app/code/Tchsl/Test/. Move your module files there, and running the shell command module:status should show your disabled module.
You don't need to put your module under app/code/. In app/code/ Magento2 will search and find your module's registration.php. In Composer's vendor/ dir it doesn't do that, so we need to trick Composer into loading your module's registration.php.
If you'd check any Magento2 module's composer.json in vendor/magento/module-*, you'll see an "autoload" section which references the registration.php file. So Composer will autoload your module's registration.php which will "tell" Magento2 where your module is located.
This is a fragment from the Magento Checkout module's composer.json:
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\Checkout\\": ""
}
}
So if you have your module in a separate repository and loaded via composer, then that is the way to go. If you do not have it in a separate repository, then your module does not belong on vendor/ but in app/code/.
See also this Magento Stack Exchange post.
I'm using Symfony 2.3 and Composer and using Composer to install my bundle from a remote repository. Doing this on its own works fine, but the problem I have is when I try to require another package from my Symfony2 bundle.
My bundle is in the src/ folder.
I've added this to my bundle's composer.json:
"require": {
"rackspace/php-opencloud": "1.10.*"
},
Everything installs but I now need to ensure the classes are autoloaded (registering namespaces I think), but it just won't do it.
I have actually solved this problem in two other ways, but they are not clean enough. I want my bundle to handle the dependency not the app folder or main Symfony2 composer.json.
Solutions that aren't good enough:
(1) If I add the Rackspace package to my symfony2/composer.json then everything works perfectly. But this is poor as the dependency is on my bundle not the whole Symfony2 framework.
(2) If I add these to app/autoload.php then it also works:
$loader->add('OpenCloud', __DIR__.'/../src/rackspace/php-opencloud/lib');
$loader->add('Guzzle\\Http', __DIR__.'/../src/guzzle/http');
$loader->add('Guzzle\\Common', __DIR__.'/../src/guzzle/common');
...but this is messy and again the dependency is on the bundle not the application.
I have tried various variations of this (in my bundle's composer.json) but it just won't work:
"autoload": {
"psr-0": { "OpenCloud": ["rackspace/php-opencloud/lib/", "rackspace/php-opencloud/tests/"] }
}
Normally, only relative paths are required.
"autoload": {
"psr-0": { "OpenCloud": ["lib/", "tests/"] }
}
I did not test with multiple paths for the same prefix. This may not be supported.
You can alternatively use the classmap autoloader, which will go through an entire folder and generate a static map from them. No prefix required.
"autoload": {
"classmap": ["lib/", "tests/"]
}
I rarely saw tests being included in the autoloading for external application however. PHPUnit handles class loading just fine.
I'm very new to symfony2 and I cannot find this info:
To register a bundle located in my vendor dir, how to determine the namespace and Bundle name for autoload.php & Appkernel.php?
For example, I have downloaded Luiggio's PHPExcel Bundle. I have place it in vendorDir/ExcelBundle/
Where content is:
namespace Liuggio\ExcelBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class LiuggioExcelBundle extends Bundle
{
}
What lines should I put in Appkernel and namespace.php?
This and this does not work:
new Liuggio\ExcelBundle\LiuggioExcelBundle()
//'Liuggio\\ExcelBundle' => array($vendorDir. '/PHPExcel'),
I cannot use composer or github repo at all, too many proxies and restrictions where I am.
You shouldn't place bundles in your vendor directory manually. Let Composer do this for you. Not only does Composer know where vendor libraries / bundles should be located, it also adds them to your autoload files and performs some other automated tasks.
To tell Composer which libraries are required, you should add them to your composer.json:
"require" : {
(...)
"liuggio/ExcelBundle": "~2.0"
},
Next, using the command line, run the composer update command:
$ php composer.phar update
(if you don't have a file composer.phar in your project directory, but you have Composer installed globally instead, use the following:)
$ composer update
This will tell Composer to download the required dependencies, update the autoload script, etc, all automatically. When it's finished, you're ready to go.
If you can't use Composer on your server, then run it locally before you upload your files (although I strongly recommend moving to a server that does allow you to use Composer).
The line you're trying to add to AppKernel.php is correct, however it only works after running Composer (or you'd indeed have to download the files and update the autoloader manually, but I'd strongly recommend against that).
Edit
If you really can't use Composer, do the following:
Place the files of ExcelBundle in the following directory:
vendor/liuggio/ExcelBundle/Liuggio/ExcelBundle
Your line in AppKernel.php was already correct.
Add this line to autoload_namespaces.php:
'Liuggio\\ExcelBundle' => array($vendorDir . '/liuggio/ExcelBundle'),
Last but not least, complain to your system administrator that he's making your work impossible with his stupid security measures.
new Liuggio\ExcelBundle\LiuggioExcelBundle(),
in AppKernel should be working fine.
This is the namespace of the LuiggioExcelBundle class + the class name. Look how your bundles are loaded, its the same.
What's your error?
You say vendorDir/ExcelBundle/ but its vendor/ExcelBundle right?
And what do you mean by namespace.php? :o
https://github.com/liuggio/ExcelBundle ==> there readme is rly easy to understand, it should help you.
For your "proxies and restriction", composer is a powerful tool, I can help you to use it. Download this soft, the free version is enough http://www.frozenway.com/ (if you cant read french, the 1st input in the header is to translate the website, English is Anglais) With this, you wont have any ports restriction.
In my symfony 2.2 app I wanted to use PHPExcel library. So I downloaded it, and copied contents of Classes library to /vendor/phpexcel directory:
vendor/
phpexcel/
PHPExcel/
PHPExcel.php
After that I added the following to app/autoload.php directly below $loader = require ... line:
$loader = require __DIR__.'/../vendor/autoload.php';
//The following was added
$loader->registerPrefixes(array(
'PHPExcel' => __DIR__ . '/../vendor/phpexcel'
));
// intl
...
Now if I browse to my web app, it returns HTTP Error 500 (Internal Server Error). I read the following post, but wasn't able to solve the problem:
How to use PHPExcel correctly with Symfony 2
Can someone help me correct this?
You should never manually download something and put it in the vendor directory. Composer manages the vendor directory, and hence it should be save to delete this directory and run composer install again. The vendor directory also is excluded from Git by default.
To install PHPExcel using composer, add it to composer.json:
"require": {
...
"phpexcel/phpexcel": "1.7.*"
}
When installed with Composer, you should not need to worry about the autoloading either.
I installed https://github.com/liuggio/ExcelBundle for PHPExcel. Bundle includes PHPExcel (addes related links to composer). You can easily use PHPExcel without wondering what the bundle says. Call new \PHPExcel(); then you move. I hope this bundle helps.
Composer seems to have a problem with SELinux. See this. Though not recommended, setting SELinux to permissive can be a workaraound.