I just want to use PHPWord for Symfony 2. But Im new on Symfony 2.
I paste PHPWord source into my util folder bundle but there is an error loading autoloader class from PHPWord...
Question is simple, How can I "wrap" code (in this case PHPWord) in to bundle? or someway I can use it.
Any solution?
Thanks for answering, that gives me some light. Altough, This doen't work for me.
I download composer.phar and I edit my composer.json as you say: adding in my require region "phpword/phpword": "0.6.2" and creating new repository phpword...
Doesn't work, I run "php composer.phar" command but It doesn't download anything...
Any tutorial or tip to make this work?
Thank you
composer require phpoffice/phpword dev-master did the work for me.
It should download and place install PhpOffice\PHPWord under the vendor folder in your Symfony2 installation (besides modifying your composer.json automatically).
In the controller you are about to use PhpWord:
use PhpOffice\PhpWord\PhpWord;
And inside the action, you can create a new instance as follows:
$doc = new PhpWord();
You shouldn't be copying the source code yourself. Use composer to install PHPWord.
Since PHPWord doesn't use composer (yet), you'll need to define a repository in your composer.json:
{
"require": {
"phpword/phpword": "0.6.2"
},
"repositories": [
{
"type": "package",
"package": {
"name": "phpword/phpword",
"version": "0.6.2",
"dist": {
"url": "http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=phpword&DownloadId=138035&FileTime=129545976016270000&Build=19692",
"type": "zip"
},
"autoload": {
"classmap": ["."]
}
}
}
]
}
Than you'll be able to use PHPWord in your Symfony project (any PHP project in fact):
$word = new PHPWord();
$writer = new PHPWord_Writer_Word2007($word);
$writer->save('test.doc');
Update: I just realized the URL to download changes with time. It'd be better to replace it with something that doesn't change (if possible, otherwise host it somewhere yourself).
As the URL changes, you can use the github repository if you've no problem with the dev version :
{
"require": {
"phpword/phpword": "dev"
},
"repositories": [
{
"type": "package",
"package": {
"name": "phpword/phpword",
"version": "dev",
"dist": {
"url": "https://github.com/PHPOffice/PHPWord/archive/develop.zip",
"type": "zip"
},
"autoload": {
"classmap": ["."]
}
}
}
]
}
You can use this simple bundle as wrapper for PhpWord:
https://packagist.org/packages/ggggino/wordbundle
Related
Hi I am trying to setup my new Drupal 8 site with composer, but I got few issues.
I tried to setup the site site by following the Guide from here and was able to setup the site successfully.
After that I tried to install a custom module which is hosted on Bitbucket and I am able to download the package using composer, but the problem is the module has some other contributed module dependency but the dependency module is not downloaded along with the custom module.
I followed the guide from here and added composer.json file to my custom module along with the dependency but after running composer require custom/custom_module only the custom module is installed but not the dependency.
My root directory composer.json file repositories section looks like this :
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
},
{
"type": "package",
"package": {
"name": "custom/custom_module",
"version": "master",
"type": "drupal-custom-module",
"source": {
"type": "git",
"url": "git#bitbucket.org:username/custom-module.git",
"reference": "master"
}
}
}
],
and the composer.json file from the custom module looks as below :
{
"name": "custom/custom_module",
"description": "This is a Custom Module with Different functionalities.",
"type": "drupal-custom-module",
"minimum-stability": "dev",
"require": {
"drupal/restui": "~1.16"
}
}
I also chaged the line "drupal/restui": "~1.16" as "drupal/restui": "^1.16" but with no success.
I even tried running composer update in the custom module directory as I was not sure whether dependencies will be installed along with custom module.
After running composer update in the custom module directory I got the following error:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package drupal/restui could not be found in any version, the re may be a typo in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your min imum-stability setting
see https://getcomposer.org/doc/04-schema.md#minimum-stability for more det ails.
- It's a private package and you forgot to add a custom repository to find it
Read https://getcomposer.org/doc/articles/troubleshooting.md for further commo n problems.
But on Drupal.org I can find the module with that version here
Please help me to solve the issue.
Try this:
Root composer.json section
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
},
{
"type": "vcs",
"url": "git#bitbucket.org:username/custom-module"
}
],
Module composer.json example that has two modules as dependency
{
"name": "username/custom-module",
"version": "1.2.3",
"type": "drupal-custom-module",
"description": "Custom module",
"keywords": ["Drupal"],
"license": "GPL-2.0+",
"minimum-stability": "dev",
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
}
],
"require": {
"drupal/admin_toolbar": ">=1.2",
"drupal/chosen": ">=2.6"
}
}
I am using Symfony for a Dockerized app. I need to move my composer vendors directory, so I did set the environnement variable $COMPOSER_VENDOR_DIR to /root/app-vendors.
The vendors are correctly located to this directory, but it causes troubles with the Symfony VarDump component. By default in Symfony 2, it is loaded from composer like this:
"autoload-dev": {
"files": [
"vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
}
In my case, this path becomes wrong. I'd like to do something like this:
"autoload-dev": {
"files": [
"{$COMPOSER_VENDOR_DIR}/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
},
But I don't see anything in the composer documentation about using env variables in a composer.json filepath.
I want to move the vendors in dev mode, but I want them to be in the default location if $COMPOSER_VENDOR_DIR is not set ; so I cannot put absolute filepath in autoload-dev section like this:
"autoload-dev": {
"files": [
"/root/app-vendors/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
},
A simple solution after a few days of step back:
Create this file on app/dump.php
<?php
$vendorsDir = getenv('COMPOSER_VENDOR_DIR')
?: __DIR__.'/../vendor';
require "$vendorsDir/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php";
Then autoload this file instead of the Symfony one
"autoload-dev": {
"files": [
"app/dump.php"
]
},
Note:
COMPOSER_VENDOR_DIR default is "vendor", see https://getcomposer.org/doc/03-cli.md#composer-vendor-dir
I've started to learn Symfony and I am following some tutorials where there is nothing about this:
When I create a new project with symfony installer and run composer install and then php app/console server:start I can open that project in my browser.
BUT! When I create a new bundle with command php app/console generate:bundle I get this error message:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "BlogBundle" from namespace "BlogBundle".
Did you forget a "use" statement for another namespace? in /home/user/Symfony/myapp/app/AppKernel.php:19
And then I need to go to my composer.json file and append my new generated bundle after AppBundle like this
{
"name": "user/myapp",
"license": "proprietary",
"type": "project",
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"BlogBundle\\": "src/BlogBundle" // <-- this is the new appended one
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
..........
And then when I try to start the server again it works and it shows Hello world in my browser.
So the question is do I have to do this every time (append new generated bundle in composer.json file)?
Yes. This is the reason, why you usually have an namespace above that. Some abbreviation of your name or company for example. This way you only add that namespace for the src folder and all bundles are "found" automatically.
Example:
{
...
"autoload": {
"psr-4": {
"Acme\\": "src"
},
}
...
}
Now of course your bundles need to use that namespace, eg.:
namespace Acme\BlogBundle;
class BlogBundle {
}
On a fresh scotch box https://box.scotch.io/ (which I generally recommend)
and with this composer:
{
"name": "silverstripe/installer",
"description": "The SilverStripe Framework Installer",
"require": {
"php": ">=5.3.3",
"silverstripe/cms": "3.5.1",
"silverstripe/framework": "3.5.1",
"silverstripe/reports": "3.5.1",
"silverstripe/siteconfig": "3.5.1",
"silverstripe-themes/simple": "3.1.*",
"silverstripe/silverstripe-omnipay": "^2.1",
"omnipay/paymentexpress": "^2.2",
"firebase/php-jwt": "^4.0"
},
"require-dev": {
"phpunit/PHPUnit": "~3.7#stable"
},
"extra": {
"branch-alias": {
"3.x-dev": "3.5.x-dev"
}
},
"config": {
"process-timeout": 600
},
"prefer-stable": true,
"minimum-stability": "dev"
}
And using payment.yml from https://github.com/silverstripe/silverstripe-omnipay
Silverstripe builds Payments, but none of the Omnipay classes are included. I have used Omnipay before with SS with no problems.
Anybody know what is going on?
Make sure you run the following on the command-line:
$> ./framework/sake dev/build flush=all
Also always worth just blowing away the contents of SS' cache (You're using Vagrant, so assuming this is a Dev env) which is usually located in /tmp if you're using the F/S and not memcache or some such, then running dev/build again. This will both clear and rebuild your cache, and in the process tell SS about all the new classes it has available to it.
silverstripe-omnipay makes use of php namespaces for many of its files, it just so happens that ServiceFactory is one of them so in order for SilverStripe to find the correct file to include you must specify its use at the top of files you intend to use ServiceFactory.
<?php
use SilverStripe\Omnipay\Service\ServiceFactory;
...
It is not entirely obvious because modules made for SilverStripe rarely make use of namespaces fo and silverstripe-omnipay makes no mention that ServiceFactory is namespaced in its examples.
What errors do you get? and how are you trying to access the classes?
You should be able to call the classes like this (depending on the Omnipay version)
<?php
use Omnipay\Omnipay;
class PaymentPage extends Page
{
function ...
{
try {
$response = $gateway->purchase([...
}
}
I found this on SO: How to use PHPExcel correctly with Symfony 2
This works, but I want to use it with composer.
The first part I already solved: to load PHPExcel for a special tag (the last stable release)
I don't find out how to fetch a tag with this syntax:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/umpirsky/SyliusAssortmentBundle"
}
]
So I use the Package notation:
I found out, the reference should be the tag name on github.
And the version cannot be the same value (PHPExcel_1.7.8). Seems that alphabetical characters are not allowed, so it's only the version as a number (1.7.8)
"repositories": [{
"type": "package",
"package": {
"name": "PHPOffice/PHPExcel",
"version": "1.7.8",
"source": {
"url": "https://github.com/PHPOffice/PHPExcel.git",
"type": "git",
"reference": "PHPExcel_1.7.8"
}
}
}]
The next step I didn't solve. I tried out every combination for the autoloading: psr-0, classmap, different paths, relative to project/vendor/phpexcel, update composer everytime, but nothing worked.
It only works, if I put this line
$loader->add('PHPExcel', __DIR__.'/../vendor/PHPOffice/PHPExcel/Classes');
into the app/autoload.php. I found out, that the first string (PHPExcel) can also be an empty string: ''.
Is there a differnece if I use PHPExcel or ''?
So my primary question is, how can I avoid to write this line into the autoload.php, put the equivalent commands into my project's composer.json?
Regarding your primary question, the problem is that once the package is installed, if you update the definition and add autoload stuff, then running composer update will not change anything. Composer still has the old package that was already installed in its "cache", so it uses that to generate the autoload and that fails.
To resolve this you should remove the vendor/PHPOffice/PHPExcel directly and run composer update, which will reinstall it with the latest information from your composer.json, including autoload, etc. You should specify autoloading as such:
"repositories": [{
"type": "package",
"package": {
"name": "PHPOffice/PHPExcel",
"version": "1.8.0",
"source": {
"url": "https://github.com/PHPOffice/PHPExcel.git",
"type": "git",
"reference": "1.8.0"
},
"autoload": {
"psr-0": {
"PHPExcel": "Classes/"
}
}
}
}],
"require": {
"PHPOffice/PHPExcel": "1.8.*",
...
Regarding the secondary question and '' vs 'PHPExcel': '' just says that any namespace can be found in this directory. That means the autoloader will always scan this directory to find classes, which is convenient but slower than mapping namespaces to directories explicitly. So both work, but the more specific form is preferred, especially in packages you publish publicly.