Composer & Parsedown - Class 'UserFrosting\\Parsedown' not found - php

I managed to install Parsedown using composer with
"require": {
...
"erusev/parsedown": "^1.6"
},
and added the class path to the autoload section
"autoload": {
"classmap" : [
"controllers", "middleware", "models", "plugins", "vendor/erusev/parsedown"
]
}
But when I try to execute this line...
$Parsedown = new Parsedown();
... I end up with this error:
Class 'UserFrosting\Parsedown' not found
Running php composer.phar dump-autoload did not help.
What am I missing here? Why Parsedown is expected under UserFrosting - UserFrosting\Parsedown?
Here is the full composer.json:
{
"name": "userfrosting/UserFrosting",
"type": "project",
"description": "A secure, modern user management system for PHP.",
"keywords": ["php user management", "usercake", "bootstrap"],
"homepage": "https://github.com/userfrosting/UserFrosting",
"license" : "MIT",
"authors" : [
{
"name": "Alexander Weissman",
"homepage": "https://alexanderweissman.com"
}
],
"require": {
"birke/rememberme" : "1.0.4",
"illuminate/database" : "5.0.33",
"league/csv": "8.1.*",
"nikic/php-parser" : "~1",
"php" : ">=5.4.0",
"phpmailer/phpmailer" : "5.2.10",
"twig/twig" : "~1.0",
"slim/slim" : "2.*",
"slim/views" : "0.1.3",
"userfrosting/fortress" : "1.*",
"wikimedia/composer-merge-plugin": "~1",
"components/highlightjs": "9.8.0",
"aws/aws-sdk-php": "3.*",
"erusev/parsedown": "^1.6"
},
"extra": {
"merge-plugin": {
"include": [
"plugins/*/composer.json"
],
"recurse": true,
"replace": false,
"merge-dev": true,
"merge-extra": false
}
},
"autoload": {
"classmap" : [
"controllers", "middleware", "models", "plugins", "vendor/erusev/parsedown"
]
}
}

Looks like you are trying to execute this line of code $Parsedown = new Parsedown(); in a class with namespace UserFrosting.
Either add a use block at the top of your php file, like this: use Parsedown; (this should come after namespace declaration), or type a backslash before the class name when you are using it, like so: $Parsedown = new \Parsedown();. The latter will start looking for this class in a root namespace.
You don't need to add this class to your autoload classmap section of composer.json file. If a package is pulled by composer, the composer will automatically add everything to autoloader after running dump-autoload.

Related

How to autoload a class inside the composer file and run a script?

I'm on Drupal 9.3.x trying to add a script to run with composer, following this guide
I've tried either to add the autoload and script part to the drupal project composer.json file
{
"name": "drupal/d9-starter-kit",
"description": "Project template for D9 starter kit",
"type": "project",
"license": "GPL-2.0-or-later",
"homepage": "https://www.drupal.org/project/drupal",
"support": {
"docs": "https://www.drupal.org/docs/user_guide/en/index.html",
"chat": "https://www.drupal.org/node/314178"
},
"autoload": {
"classmap": [
"Drupal\\d9_starter_kit\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
},
// require, extra, etc...
}
or to create a composer.json inside the custom module I've made
{
"name": "drupal/d9_starter_kit",
"type": "drupal-custom-module",
"description": "a module to script the start",
"license": "proprietary",
"authors": [
{
"name": "My name",
"email": "mymail#mail.com"
}
],
"require": {},
"autoload": {
"classmap": [
"src\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
}
}
and include that to the project composer file, according the the guide on Drupal site
The path of the Start class is web/modules/custom/d9_starter_kit/src/Starter.php
<?php
namespace Drupal\d9_starter_kit;
class Starter {
public static function doStart() {
print('test');
// TODO
}
}
I've tried various other syntax for the autoload part - e.g. d9_starter_kit\\Starter.php - but none works.
I get the following error
Could not scan for classes inside "Drupal\d9_starter_kit\Starter.php" which does not appear to be a file nor a folder
Class Drupal\d9_starter_kit\Starter is not autoloadable, can not call doStart script
What is the correct way to autoload the class and run a script? The objective of the latter is the same of the guide, copy, rename and edit some files in order of automate the "start up" of a custom project template.

Namespace issue in Model in Laravel - Package Development

I am developing a package in laravel which uses model for CRUD operations.
I have put it up in packagist as well, but when I try to install it in laravel application and visit a route defined by the package, it says
Class 'Zusamarehan\Tourify\Model\Tourifies' not found
The following is the folder structure of my package
rehan
tourify
src
assets
database
Http
Model
Tourifies.php
resources
routes
TourifyServiceProvider.php
composer.json
The following is the contents of my Tourifies.php
<?php
namespace Zusamarehan\Tourify\Model;
use Illuminate\Database\Eloquent\Model;
class Tourifies extends Model
{
}
The following is my composer.json file
{
"name": "zusamarehan/tourify",
"description": "A Package for adding Tour/Help to your Laravel Projects.",
"keywords": ["laravel", "tour", "tourify", "product-tour", "product-help"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "zusamarehan",
"email": "zrehan286#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"extra": {
"laravel": {
"providers": [
"Zusamarehan\\tourify\\TourifyServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Zusamarehan\\tourify\\": "src"
}
}
}
The Model class is not loading I suppose? I am not sure.
Can someone point out the mistake?
The namespaces in your classes use Zusamarehan\Tourify, however, in your composer.json you've used Zusamarehan\tourify. These should match.
You'll need to update your composer.json file so that the namespaces uses the correct case:
{
"name": "zusamarehan/tourify",
"description": "A Package for adding Tour/Help to your Laravel Projects.",
"keywords": ["laravel", "tour", "tourify", "product-tour", "product-help"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "zusamarehan",
"email": "zrehan286#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"extra": {
"laravel": {
"providers": [
"Zusamarehan\\Tourify\\TourifyServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Zusamarehan\\Tourify\\": "src"
}
}
}
"Zusamarehan\\tourify\\": "src" in your composer.json is wrong. Needs uppercase T. Looking at mine I also have a trailing / after src so you can try that as well.
You have the same lowercase t in the provider.

Class not found when I use namespaces

I'm new with namespaces and I'm making tests but when I run app.php I get the next error:
Fatal error: Class 'ProtocoloWT\models\status' not found in
C:\xampp\htdocs\wt.uptkd\protocoloWt\app.php
.
app.php
use Neomerx\JsonApi\Encoder\Encoder;
use Neomerx\JsonApi\Encoder\EncoderOptions;
use ProtocoloWT\models\status;
require '../vendor/autoload.php';
$status = status::instance(http_response_code(204));
$encoder = Encoder::instance([
'\status' => '\statusEsquema',
], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com/api/v1'));
echo $encoder->encodeData($status);
status.php
namespace ProtocoloWT\models;
class status
{
public static function instance ($status)
{
$estado = new status();
$estado->status = $status;
return $estado;
}
}
composer.json
{
"name": "neomerx/json-api",
"description": "Framework agnostic JSON API (jsonapi.org) implementation",
"keywords": [
"jsonapi.org",
"json-api",
"jsonapi",
"neomerx",
"json",
"api"
],
"homepage": "https://github.com/neomerx/json-api",
"support": {
"issues": "https://github.com/neomerx/json-api/issues"
},
"license": "Apache-2.0",
"authors": [
{
"name": "neomerx",
"email": "info#neomerx.com"
}
],
"require": {
"php": ">=5.5.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.6 || ^5.0 || ^6.0",
"mockery/mockery": "~0.9.4",
"scrutinizer/ocular": "^1.3",
"squizlabs/php_codesniffer": "^2.5",
"monolog/monolog": "^1.18",
"phpmd/phpmd": "^2.6"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Neomerx\\JsonApi\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Neomerx\\Tests\\JsonApi\\": "tests/",
"Neomerx\\Samples\\JsonApi\\": "sample/"
}
},
"scripts": {
"test": ["#test-unit", "#test-cs", "#test-md"],
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
"test-unit-with-coverage": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming",
"perf-php": "docker-compose run --rm cli_php php /app/sample/sample.php -t=10000",
"perf-hhvm": "docker-compose run --rm cli_hhvm hhvm /app/sample/sample.php -t=10000"
}
}
File app.php is in a directory called protocoloWT/ and status.php is in protocoloWT/models.
I installed a framework with composer and I can use those namespaces (Neomerx\JsonApi), but I can't create my own namespaces in my files because I get that error. What could be the issue?
PS: Sorry for my english, I have tried to explain it the best I can

Fatal error when instanciating Pusher - Can't find class

I have a project to test and play around, with the following structure:
app/
controllers/
HomeController.php
handlers/
models/
vendor/
composer/
psr/
pusher/
pusher-php-server/
src/
Pusher.php
PusherException.php
PusherInstance.php
tests/
composer.json
autoload.php
index.php
I tried to require the Pusher autoloader in my index file:
require 'vendor/autoload.php';
Which is the following:
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInite16b90ab01042d2a69b1d54243c9e23a::getLoader();
Now, in my HomeController.php, I have the following code:
namespace App\controllers;
use \App\handlers\Views as View;
use \App\models\Home_model as Home;
use \App\controllers\SessionController;
use \Pusher\Pusher as Pusher;
class HomeController {
private $pusher;
public function __construct() {
$options = array(
'cluster' => 'hmm',
'encrypted' => true
);
$this->pusher = new Pusher(
'secret',
'secret',
'secret',
$options
);
}
public function index() {
$data['message'] = 'hello world';
$this->pusher->trigger('my-channel', 'my-event', $data);
return $this->view->render('views/home/index.php');
}
}
But this returns me an error:
Fatal error: Class 'Pusher\Pusher' not found in
And I'm not sure what I'm doing wrong. Could someone explain me what I'm doing wrong?
In composer.json I get the following:
{
"name": "pusher/pusher-php-server",
"description" : "Library for interacting with the Pusher REST API",
"keywords": ["php-pusher-server", "pusher", "rest", "realtime", "real-time", "real time", "messaging", "push", "trigger", "publish", "events"],
"license": "MIT",
"require": {
"php": "^5.4 || ^7.0",
"ext-curl": "*",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.7"
},
"autoload": {
"psr-4": {
"Pusher\\": "src/"
}
},
"autoload-dev": {
"psr-4": { "": "tests/" }
},
"config": {
"preferred-install": "dist"
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
On Github, they mention that the library depends on cURL and JSON modules.
Not realy sure if this has something to do with my issue?
I'm still stuck, so any help is greatly appreciated.
Also, I'm using a .htaccess file, to rewrite my urls.
I have managed your code to work with right composer.json next to index.php:
{
"name": "awesome/project",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Author",
"email": "author#gmail.com"
}
],
"autoload": {
"psr-4": {
"": ""
}
},
"require": {
"pusher/pusher-php-server": "dev-master"
}
}
Then just run composer install.
My index.php contents:
<?php
require 'vendor/autoload.php';
use app\controllers\HomeController;
$ctrl = new HomeController();

Fatal error: Uncaught Error: Class 'Kreait\Firebase\ServiceAccount' not found in

I have to learn just now write mobile app web service via fire base.
I followed this link: https://firebase-php.readthedocs.io/en/stable/
In my core website i create web service folder and then create my fire.php file.This file code here,
<?php
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');
$apiKey = 'AIzaSyC_vb5G9qs3NJsywbR34el1RaPj2HDhwNg';
$firebase = (new Factory)
->withServiceAccountAndApiKey($serviceAccount, $apiKey)
->withDatabaseUri('https://workarea-cb10b.firebaseio.com')
->create();
$database = $firebase->getDatabase();
$newPost = $database
->getReference('blog/posts')
->push([
'title' => 'Post title',
'body' => 'This should probably be longer.'
]);
$newPost->getKey(); // => -KVr5eu8gcTv7_AHb-3-
$newPost->getUri(); // => https://my-project.firebaseio.com/blog/posts/-KVr5eu8gcTv7_AHb-3-
$newPost->getChild('title')->set('Changed post title');
$newPost->getValue(); // Fetches the data from the realtime database
$newPost->remove();
I have to call my support file here: https://github.com/kreait/firebase-php/
But still I got a:
Fatal error: Uncaught Error: Class 'Kreait\Firebase\ServiceAccount'
not found in
/opt/lampp/htdocs/workarea/webservice/firebase/fire.php:13 Stack
trace: #0 {main} thrown in
/opt/lampp/htdocs/workarea/webservice/firebase/fire.php on line 13
issue have to fix this issue. Kindly check and help me.
My composer.json file
{
"name": "kreait/firebase-php",
"description": "Firebase Admin SDK",
"keywords": ["firebase", "google", "sdk", "api", "database"],
"homepage": "https://github.com/kreait/firebase-php",
"license": "MIT",
"authors": [
{ "name": "Jérôme Gamez", "homepage": "https://github.com/jeromegamez" }
],
"require": {
"php": "^7.0",
"ext-mbstring": "*",
"ext-openssl": "*",
"fig/http-message-util": "^1.1",
"google/auth": "^0.11.0|^1.0",
"guzzlehttp/guzzle": "^6.2.1",
"kreait/firebase-tokens": "^1.1.1",
"lcobucci/jwt": "^3.2",
"mtdowling/jmespath.php": "^2.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0",
"phpstan/phpstan-phpunit": "^0.9.2",
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {
"Kreait\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Kreait\\Tests\\": "tests"
}
},
"config": {
"platform": {
"php": "7.0"
},
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
}
}
I am sure you already got your problem solved, but for upcoming developers it might be helpful, Its issue with your composer install kreait/firebase-php using composer in cmd inside project directory and your issue will be resolved.
I actually had the same issue and I address it by install php-psr exteion:
pecl install psr
Created /etc/php/7.x/mods-available/psr.ini with the following lines:
; configuration for psr module
; priority=20
extension=psr.so
and enable the extension by excuting:
sudo phpenmod psr
and reload/restart apache, php-fpm.....
service apache2 restart
service php7.4-fpm restart

Categories