composer package only for development in laravel providers - laravel

If I have a package (that I have to add to app/config/app.php providers/aliases) that I only really want on my development boxes (b/c I only use for development) is there an approach that will allow me to do this leveraging composer require --dev <package> along w/ composer install --no-dev
I need to add an index into both the providers and aliases array...but I would prefer NOT to manage those large arrays in multiple config environment folders if possible

Create a folder in your config directory that matches your environment name. dev in this case.
Next, recreate any files you wish to override and place them into that folder. app.php in this case
Next open bootstrap/start.php and find $app->detectEnvironment. The array passed to this method determines which environment you are working with. Setting the key to this array as dev will overwrite any of your config files with their counterparts in your dev folder.
If you do not wish to create an entirely new providers array in app.php file for each environment and say you only wanted to add a certain provider to your dev environment, you may use a helper method like so...
'providers' => append_config(array(
'DevOnlyServiceProvider',
))
All this information was pulled from the Laravel documentation found here: http://laravel.com/docs/configuration

For anyone looking how to do this in Laravel 5, you can still emulate the L4 behaviour.
On the top of app/Providers/AppServiceProvider add:
use Illuminate\Foundation\AliasLoader;
And then edit the register() method:
public function register()
{
$path = $this->app->environment() .'.app';
$config = $this->app->make('config');
$aliasLoader = AliasLoader::getInstance();
if ($config->has($path)) {
array_map([$this->app, 'register'], $config->get($path .'.providers'));
foreach ($config->get($path .'.aliases') as $key => $class) {
$aliasLoader->alias($key, $class);
}
}
}
Then just add config/<YOUR ENVIRONMENT NAME>/app.php config file and define additional providers and aliases, same as in app.php
For example:
return [
'providers' => [
Barryvdh\Debugbar\ServiceProvider::class,
],
'aliases' => [
'Debugbar' => Barryvdh\Debugbar\Facade::class,
],
];

Related

How to install laravelcollective html in laravel 5 without using composer

I'm using laravel 5.5 and I want to install laravelcollective without using composer. I have followed steps mentioned in other questions to install it in laravel 5.2 but it is generating the following error.
Class Collective\Html\HtmlServiceProvider not found
Please suggest the possible reasons for the error.
Shashank Simha M R, I think the best way to go is by installing it via composer like this: composer require laravelcollective/html "5.5.*".
Here, you just specify the version of the Laravel you've installed. Otherwise, composer will not be able to install it.
After that, you just update the config/app.php file like so:
Add the following line to the $provider array:
Collective\Html\HtmlServiceProvider::class
Then find the aliases array and add these two aliases to the aliases array:
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
That is it!
Go to this link:
https://github.com/LaravelCollective/html
download the package and extract it to /vendor/ folder so that you have this path
/vendor/laravelcollective/html/src/HtmlServiceProvider.php
Next, add your new provider to the providers array of config/app.php:
'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
Finally, add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],
And it should work.

How can I install Collective\Html\HtmlServiceProvider without composer?

I am on a cpanel shared hosting and don't have access to SSH. How can I install Collective\Html\HtmlServiceProvider without SSH?
If I had SSH access I would use:
composer require laravelcollective/html
But I don't know what to do now.
Another solution would be downloading it locally with composer and uploading the entire project (including the vendor folder) with a ftp client.
Step 1:
Go to https://github.com/LaravelCollective/html and download the repo.
Step 2
Copy the zip and navigate to your project. Inside the vendor folder create folder laravelcollective and then inside html. Inside the html folder place the repo.
Step 3
On your composer.json file on the root directory of your project add the following lines.
"autoload": {
"psr-4": {
"Collective\\Html\\": "vendor/laravelcollective/html/src/"
},
}
and then do
composer dump-autoload
Step 4
Next, add your new provider to the providers array of config/app.php:
'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
Finally, add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],

composer.json: change the vendor-dir dynamically through a script

In a project using Composer, shared by many developers and environments, I need to have the vendor-dir config parameter of composer.json to be set dynamically. That is, a script that runs when composer install/update is launched, must be able to change the value of this entry:
//composer.json
"config": {
"vendor-dir": "/var/www/html/......",
I tried the following:
//composer.json
"scripts": {
"pre-install-cmd": "MyBundle\\Composer\\Hook::setVendorDir",
The class Hook has this method:
//MyBundle/Composer/Hook.php
public static function setVendorDir(Event $event)
{
// ... some code set the $vendorDir variable here depending on many thing
$event->getComposer()->getConfig()->merge([
'config' => [
'vendor-dir' => $vendorDir
]
]);
// ...
}
The result is that the file autoloader.php and a composer folder are created in the right vendor directory, but all other packages are still installed in the default vendor directory!
The composer folder I mentioned only contains some PHP files (ClassLoader.php, _autoload\_*.php_, and LICENSE)
Notice: When I change the vendor-dir parameter in composer.json, it works flawlessly.
How may I set vendor-dir dynamically and have it taken into account for every package installations?
You can e.g. write a batch script (Windows) or bash script (Linux) or even a PHP script which you run instead of composer install. The script sets the correct vendor-dir in the composer.json and then runs composer install or whatever.
To set vendor dir just run in your script:
composer config vendor-dir /your/path/to/your/vendor/dir
For more info about composer config see the Composer documentation.

Adding a paths to Laravel 4.1 paths.php

Laravel 4.1 has a file paths.php and it has some paths: app, public, base and storage.
I wish to add another path so what I did:
I wrote this in paths.php
'upload' => __DIR__.'/../some/path/uploads',
Then I copied and pasted and edited this in helpers.php
if ( ! function_exists('upload_path'))
{
function upload_path($path = '')
{
return app()->make('path.upload').($path ? '/'.$path : $;
}
}
But after a few days it disappeared!
I am wondering if I did it wrong.
Never edit files in the vendor directory, it and its contents is entirely managed by Composer, and its contents will be overwritten when doing composer update.
If you want to add your own custom functions, autoload them in your composer.json using the "files" autoload rule.
"autoload": {
"files": ["app/helpers.php"]
}
The "Best" way I found to handle this were the included configuration folders.
Inside root/app/config/ you can add a host specific folder (there is one called local by default).
You can add someFolderName and then inside root/bootstrap/start.php update this array:
$env = $app->detectEnvironment(array(
'local' => array('outLocalHostsName'),
'someFolderName' => array('SomeOtherMachinesHostname'),
));
Now go back to the folder and create an app.php file inside it (remember root/app/config/someFolderName/app.php) and add some value in it like so:
'someKeyNameHere' => 'some path string here'
Now you can access it anywhere using:
Config::get('app.someKeyNameHere')
This seems to work great for me.

Integrating Doctrine with CodeIgniter

I have successfully installed the latest version of CodeIgniter and have basic MVC pattern working. The problem that I've noticed is that CI doesn't naturally allow for prepared statements when it comes to queries. So, I decided to download Doctrine 1 from GitHub. I'm very new to Doctrine and needed some help integrating it with CI so I followed this tutorial.
In one of my controllers, I have
$this->load->library('doctrine');
$this->em = $this->doctrine->em;
But, when I go to load the view in my browser, I'm greeted with an error reading
Message: require_once(/Applications/MAMP/htdocs/CodeIgniter/application/libraries/Doctrine/Common/ClassLoader.php): failed to open stream: No such file or directory
Upon further inspection of the Doctrine download from GitHub, there doesn't even seem to be a folder titled "common" anywhere in there. I'm very new to CI and especially Doctrine. Does anyone have some advice that can help me get this working? Also, is it possible to use the MySQLi driver instead of the PDO one with Doctrine?
Downloading the Doctrine ORM straight from GitHub doesn't include the other dependencies. These are managed by Composer. If you look inside the composer.json file you can see these dependencies. If you want to install them manually, they are:
doctrine/common
doctrine/inflector
doctrine/cache
doctrine/collections
doctrine/lexer
doctrine/annotations
doctrine/dbal
symfony/console
I believe that's all of them. You will have to merge these files in their appropriate directories as they follow PSR-0 standards for the autoloading of classes.
Alternatively, install Doctrine 2 with Composer with the following composer.json file and any other dependencies will be installed automatically. Then integrate with CodeIgniter.
{
"minimum-stability": "stable",
"require": {
"doctrine/orm": "2.3.*"
}
}
Edit the index.php file of your CodeIgniter app by adding a single line to include the autoloader file before requiring the CodeIgniter core.
require_once BASEPATH.'../vendor/autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
Also if installing with Composer, use this edited version of the bootstrap as the contents of application/libraries/Doctrine.php, which is what worked for me
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager;
class Doctrine
{
public $em;
public function __construct()
{
// Load the database configuration from CodeIgniter
require APPPATH . 'config/database.php';
$connection_options = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database'],
'charset' => $db['default']['char_set'],
'driverOptions' => array(
'charset' => $db['default']['char_set'],
),
);
// With this configuration, your model files need to be in application/models/Entity
// e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
$models_namespace = 'Entity';
$models_path = APPPATH . 'models';
$proxies_dir = APPPATH . 'models/Proxies';
$metadata_paths = array(APPPATH . 'models');
// Set $dev_mode to TRUE to disable caching while you develop
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
$this->em = EntityManager::create($connection_options, $config);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
Note: Version 3 of CodeIgniter when released, will be installable with Composer, but version 2 is not.
For those looking for a tutorial to integrate Doctrine 2 with CodeIgniter, this question and others answers are outdated (for CI 2).
This is a new tutorial for CI 3 I made and I checked is working:
How to install Doctrine 2 in CodeIgniter 3
I repeat it here.
Install Doctrine
Doctrine 2 ORM’s documentation - Installation and Configuration
Doctrine can be installed with Composer.
Define the following requirement in your composer.json file:
{
"require": {
"doctrine/orm": "*"
}
}
Then call composer install from your command line.
Integrating with CodeIgniter
Doctrine 2 ORM’s documentation - Integrating with CodeIgniter
Here are the steps:
Add a php file to your system/application/libraries folder called Doctrine.php. This is going to be your wrapper/bootstrap for the D2 entity manager.
Put the Doctrine folder (the one that contains Common, DBAL, and ORM) inside the third_party folder.
If you want, open your config/autoload.php file and autoload your Doctrine library: $autoload[‘libraries’] = array(‘doctrine’);
Creating your Doctrine CodeIgniter library
Now, here is what your Doctrine.php file should look like. Customize it to your needs.
<?php
/**
* Doctrine 2.4 bootstrap
*
*/
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
class Doctrine {
public $em = null;
public function __construct()
{
// load database configuration from CodeIgniter
require_once APPPATH.'config/database.php';
// include Doctrine's ClassLoader class
require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php';
// load the Doctrine classes
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'third_party');
$doctrineClassLoader->register();
// load the entities
$entityClassLoader = new ClassLoader('Entities', APPPATH.'models');
$entityClassLoader->register();
// load the proxy entities
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
$proxiesClassLoader->register();
// load Symfony2 classes
// this is necessary for YAML mapping files and for Command Line Interface (cli-doctrine.php)
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
// Set up the configuration
$config = new Configuration;
// Set up caches
if(ENVIRONMENT == 'development') // set environment in index.php
// set up simple array caching for development mode
$cache = new \Doctrine\Common\Cache\ArrayCache;
else
// set up caching with APC for production mode
$cache = new \Doctrine\Common\Cache\ApcCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// set up annotation driver
$driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models/Mappings');
$config->setMetadataDriverImpl($driver);
// Proxy configuration
$config->setProxyDir(APPPATH.'/models/Proxies');
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
$config->setSQLLogger($logger);
$config->setAutoGenerateProxyClasses( TRUE ); // only for development
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager, and store it for use in our CodeIgniter controllers
$this->em = EntityManager::create($connectionOptions, $config);
}
}
Setting up the Command Line Tool
Doctrine ships with a number of command line tools that are very helpful during development.
Check if these lines exists in the Doctrine.php file, to load Symfony classes for using the Command line tools (and for YAML mapping files):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
You need to register your applications EntityManager to the console tool to make use of the tasks by creating a cli-doctrine.php file in the application directory with the following content:
<?php
/**
* Doctrine CLI bootstrap for CodeIgniter
*
*/
define('APPPATH', dirname(__FILE__) . '/');
define('BASEPATH', APPPATH . '/../system/');
define('ENVIRONMENT', 'development');
require APPPATH.'libraries/Doctrine.php';
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
?>
Now run this script through the PHP command-line and should see a list of commands available to you.
php cli-doctrine.php
Generate mapping classes from database:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
if you get this error:
Fatal error: Call to undefined function Doctrine\Common\Cache\apc_fetch()
install the APC extension for PHP:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
For production mode you'll want to use a real caching system like APC, get rid of EchoSqlLogger, and turn off autoGenerateProxyClasses in Doctrine.php
Find the link for the doctrine integration in CI
https://github.com/mitul69/codeigniter-doctrine-integration
Please note that code igniter 2 has a little difference in its code organization. In code igniter 2 it is better to put the Doctrine folder in application/third_party folder, instead of application/libraries folder (or else it will not work!).
You can read more about it here
I had the same problem when I tried to follow this tutorial from doctrine user guide
http://doctrine-orm.readthedocs.org/en/latest/cookbook/integrating-with-codeigniter.html
This problem happens when I tried to install via composer so then I went to this web site
http://www.doctrine-project.org/downloads/ and do manually download the DoctrineORM-2.3.3-full.tar.gz version and the error was gone.
The original poster's problem seems to be an issue with autoloading. I was presented with a similar issue when trying to set up CodeIgniter and Doctrine with Composer. In CodeIgniter 3, you can enable the use of composer autoloading, which should allow you to load all Doctrine files correctly. You must point your Composer vendor dir to application/vendor for this to work. You can also do it in older versions, but then you have to include the Composer autoload file manually in your Doctrine library file for CodeIgniter.
If you want more information: I wrote a blog post describing exactly how to do it. http://blog.beheist.com/integrating-codeigniter-and-doctrine-2-orm-with-composer/
you can use this
via composer :
composer create-project rbz/codeigniter your-project
via git :
git clone https://github.com/dandisy/cihmvctwig.git

Resources