RedBean failed on composer autoload manually generated - php

im learning something about RedBeanPHP ORM and add the
code downloaded from http://www.redbeanphp.com/downloadredbean.php
to my project autoload using 'composer dump-autoload' command and
the configuration 'composer.json' in the root directory is:
{
"autoload": {
"classmap": [
"vendor/redbeanphp/src/rb.php",
"vendor/myowncode/src/Model.php"
]
}
}
on 'vendor/composer/installed.json' i put this:
[
{
"name": "gabordemooij/redbean",
"version": "5.4",
"require": {},
"autoload": {
"psr-4": {"RedBeanPHP\\": "src"}
}
},
"name": "myowncode/src",
"version": "1.0",
"require": {},
"autoload": {
"psr-4": {"MyCode\\": "src"}
}
}
]
and all works fine, at least until i try the example from the RedBean web
about 'Models' and the code:
<?php
require 'vendor/autoload.php';
class Model_Band extends RedBean_SimpleModel {
public function update() {
if ( count( $this->bean->ownMember ) >4 )
throw new Exception( 'Too many members!' );
}
}
results in error:
PHP Fatal error: Cannot declare class RedBeanPHP\RedException, because the name is already in use in /opt/lampp/htdocs/testing/vendor/redbeanphp/src/rb.php on line 8358
Fatal error: Cannot declare class RedBeanPHP\RedException, because the name is already in use in /opt/lampp/htdocs/testing/vendor/redbeanphp/src/rb.php on line 8358
but, if i dont use autoload and do this:
require 'vendor/redbean/src/rb.php';
class Model_Band extends RedBean_SimpleModel {
public function update() {
if ( count( $this->bean->ownMember ) >4 )
throw new Exception( 'Too many members!' );
}
}
it works, but i want that works with the autoload, i know, i can just open composer.json file and add the package name ("gabordemooij/redbean": "dev-master"), but i want to learn more about autoload and
get a good comprehension of whats wrong on my configuration/code.

The problem was the code from http://www.redbeanphp.com/downloadredbean.php its not
prepared for use with composer autoload, is some kind of amalgamation, all the code
in a single file, and i try downloading a release from:
https://github.com/gabordemooij/redbean/archive/v5.4.2.zip, i do the same process
to generate autoload, but we must edit the file loader on RedBeanPHP dir on the
release and change the REDBEANPHP_MAIN_DIR from phar://rb.phar/RedBeanPHP/
to vendor/redbean-5.4.2/RedBeanPHP/, i put the code on vendor/redbean-5.4.2,
and thats all problem solved :)

Related

composer autoload not work with global funtion [duplicate]

I am trying to make my own mock MVC framework as a project. This is my first time using composer outside of using it for requiring dependencies for Laravel. The actual autoloading works well, but when I try to autoload the helpers.php something weird happens. The file is autoloaded(if I change the path of the file I get the file not found error) but the contents inside it are not. In another file I try to call any function from the helpers.php file and I get
Fatal error: Uncaught Error: Call to undefined function
This is the file structure of the example
composer.json
App
Utils
helpers.php
public
index.php
This is my composer.json file:
{
"name": "admin/projecttest",
"autoload": {
"psr-4": {
"Admin\\Projecttest\\": "src/",
"App\\": "App/"
},
"files": [
"App/Utils/helpers.php"
]
},
"minimum-stability": "dev"
}
The helpers.php
<?php
namespace App\Utils;
use Leonlav77\Frejmcore\helpers\DotEnv;
function config($config){
$config = explode(".", $config);
$file = $config[0];
$configFile = require "../config/$file.php";
return $configFile[$config[1]];
}
function env($key, $default = null){
(new DotEnv(__DIR__ . '../../.env'))->load();
return getenv($key) ? getenv($key) : $default;
}
function baseDir(){
return __DIR__ . "/../";
}
index.php (where I call the function from the helper)
<?php
require "../vendor/autoload.php";
var_dump(function_exists('baseDir'));
var_dump(baseDir());
from the function_exists I get false
As the user Foobar suggested the the problem was in the namespace of the helpers.php . Since it had a namespace the functions also had a namespace, so insted of baseDir() I needed to use App/Utils/baseDir().
The solution was to simply remove the namespace from helpers.php

Composer: Autoloaded file helper.php is autoloaded but the functions inside it are not

I am trying to make my own mock MVC framework as a project. This is my first time using composer outside of using it for requiring dependencies for Laravel. The actual autoloading works well, but when I try to autoload the helpers.php something weird happens. The file is autoloaded(if I change the path of the file I get the file not found error) but the contents inside it are not. In another file I try to call any function from the helpers.php file and I get
Fatal error: Uncaught Error: Call to undefined function
This is the file structure of the example
composer.json
App
Utils
helpers.php
public
index.php
This is my composer.json file:
{
"name": "admin/projecttest",
"autoload": {
"psr-4": {
"Admin\\Projecttest\\": "src/",
"App\\": "App/"
},
"files": [
"App/Utils/helpers.php"
]
},
"minimum-stability": "dev"
}
The helpers.php
<?php
namespace App\Utils;
use Leonlav77\Frejmcore\helpers\DotEnv;
function config($config){
$config = explode(".", $config);
$file = $config[0];
$configFile = require "../config/$file.php";
return $configFile[$config[1]];
}
function env($key, $default = null){
(new DotEnv(__DIR__ . '../../.env'))->load();
return getenv($key) ? getenv($key) : $default;
}
function baseDir(){
return __DIR__ . "/../";
}
index.php (where I call the function from the helper)
<?php
require "../vendor/autoload.php";
var_dump(function_exists('baseDir'));
var_dump(baseDir());
from the function_exists I get false
As the user Foobar suggested the the problem was in the namespace of the helpers.php . Since it had a namespace the functions also had a namespace, so insted of baseDir() I needed to use App/Utils/baseDir().
The solution was to simply remove the namespace from helpers.php

File structure in a PHP composer package with PHPSpec

I had a working composer package with a single files and a single class. So, now I'm trying to change the package so that it is more like SOLID.
I have a file structure like this...
PackageName.php
addresses.php
names.php
interfaces
names.php
addresses.php
When I use PHPSpec the methods in the main PackageName.php are getting validated but within one of the methods I have something like...
namespace blah\PackageName;
use blah\PackageName\ProcessNames;
class PackageName
{
public function formatData($user)
{
$place_holders = array();
$place_holders = ProcessNames::process_name($user, $place_holders);
$place_holders = ProcessAddresses::process_address($user, $place_holders);
return json_encode($place_holders);
}
}
which gives the error...
48 ! should do the address
exception [err:Error("Class 'blah\PackageName\ProcessNames' not found")] has been thrown.
The composer.json is like...
{
"name": "blah\PackageName",
"description": "Format data.",
"require": {
"nesbot/carbon": "^1.34",
"php": ">7.0.0"
},
"require-dev": {
"phpspec/phpspec": "^4.3"
},
"authors": [
{
"name": "me",
"email": "me#emailaddress.com"
}
],
"autoload": {
"psr-4": {
"blah\\PackageName\\": "src/",
"spec\\blah\\PackageName\\": "spec/"
},
"files": {
"src/interfaces/names.php",
"src/names.php"
}
}
}
I can't see how to include files in the package. I'm not sure I need the "files" part on composer.json but I'm trying to figure out how to do it. Any info much appreciated.
This is the same issue someone else had today! A simple error.
Replace:
namespace blah\PackageName;
with:
namespace blah;
The full class name includes the namespace and class name. So essentially your class was actually an instance of blah\PackageName\Packagename
Possibly take Packagename out of the composer.json, depending on your needs, and if you change that, remember to run composer dumpautoload

php:composer auto loading not working with multiple directories inside src folder

Here is my folder structure
in my package/index.php look like this
require_once 'vendor/autoload.php';
use HelloWorld\SayHello;
use Test\First;
//works fine
SayHello::world();
//returns classnot found error
First::sayTest();
In my composer.json' i've included the following.
"autoload": {
"psr-0": {
"HelloWorld": "src/"
}
}
But i got the following error in First::sayTest() line
Fatal error: Uncaught Error: Class 'Test\First' not found
But the class First.php exists under src\Test folder.
namespace Test;
class First
{
public static function sayTest()
{
echo 'test';
}
}
You only declared that one prefix resides in src. Add another one:
"autoload": {
"psr-0": {
"HelloWorld": "src/",
"Test": "src/"
}
}
Also note that when you are using namespaces, PSR-4 is better suited:
"autoload": {
"psr-4": {
"HelloWorld\\": "src/HelloWorld/",
"Test\\": "src/Test"
}
}
your composer.json contains some entry, which makes it load HelloWorld, but there is no such entry for the folder "Test".
Maybe it would help to copy your First.php to the HelloWorld-folder and change your reference in package.index.php.
Change composer.json to read:
"autoload": {
"psr-4": {
"": "src/"
}
}
Then run composer dump-autoload to regenerate vendor/autoload.php.
Read more about the autoload section of composer.json.

How to fix error of composer post-install-cmd script?

I would like to use composer script to do some post installation such as copying files from bootstrap vendor folder to my web application public folder. I have a baby experience with PHP world and web application development.
I'm trying to learn doing this by following this tutorial
This is my directory structure*
This is my composer.json
{
"name": "Composer Script",
"description": "An example to demonstrate the use of Composer scripts",
"version": "1.0.0",
"require": {
"twitter/bootstrap": ">=3.0"
},
"scripts": {
"post-install-cmd": [
"ComposerScript\\Installer::postInstall"
],
"post-package-install": [
"/var/www/test/composer-script/install.sh"
]
}
}
This is ComposerScript\Installer.php
class Installer
{
public static function postInstall(Event $event)
{
$composer = $event->getComposer();
// do stuff
}
public static function postPackageInstall(Event $event)
{
$installedPackage = $event->getOperation()->getPackage();
// do stuff
}
public static function warmCache(Event $event)
{
// make cache toasty
}
}
After execute composer install I got this error
install.sh is empty at this moment
How to fix this error, and especially what is autoload?, I don't event know what keywords to search for please suggest me some reading.
Just in case someone stumbled again to this problem. Here is a spoon feed sample. :)
In a given scenario like this
We have to set on our composer.json to the ff. settings
"autoload": {
"psr-0": {
"ComposerScript\\Installer" : ""
}
},
"scripts": {
"post-package-update": [
"ComposerScript\\Installer::postPackageUpdate"
]
}
Then the content of Installer.php is
namespace ComposerScript;
use Composer\Script\Event;
class Installer
{
public static function postUpdate(Event $event)
{
$composer = $event->getComposer();
// do stuff
}
public static function postPackageUpdate(Event $event)
{
$packageName = $event->getOperation()
->getPackage()
->getName();
echo "$packageName\n";
// do stuff
}
public static function warmCache(Event $event)
{
// make cache toasty
}
}
Then executing php composer.phar update will work without warning.
The post-package-install value is relative to the location of the composer.json file. Do not use absolute location here.
Also, Composer's install scripts expect PHP, not sh!
Kongthap, I ran into the same problem and this is how I fixed it:
"require": {...},
"autoload": {
"psr-0": {
"Dphp": "src/"
}
},
"scripts": {...}
Dphp is the root namespace of my lib.
Hope it helps.
Sometimes the problem can be global packages are outdated, you can fix it by running the command composer global update before running composer install

Categories