Access to composer autoloaded files in laravel 5 - laravel

Trying to use a non-Laravel package: https://packagist.org/packages/luceos/on-app
Edited composer.json to require it and did the composer install, update, then dump-autoload -o.
This package requires an initialization: vendor/luceos/on-app/src/OnAppInit.php
Which isn't a class and only has the one method. But it doesn't seem to be loaded when I try to bind it in a service provider. The version for the cloud is initiated in the OnAppInit.php but that isn't being done so the "version isn't supported" error comes up of course.
I know that I am missing a small detail but can't find it. Maybe in the service provider??
composer.json
"require": {
"luceos/on-app": "~3.5"
"autoload": {
"psr-4": {
"Luceos\\OnApp\\": "vendor/luceos/on-app/src/"
config/app.php
'providers' => [
'App\Providers\OnAppServiceProvider',
app/Providers/OnAppServiceProvider.php
public function register()
{
$this->app->bind('onapp', function($app)
{
$hostname = 'http://cloud';
$username = 'email#foo.com';
$password = 'api_key';
$factory = new \OnApp_Factory($hostname, $username, $password);
$setting = $factory->factory('Settings')->getList();
return $setting;
});
}
Looks like its there...
vendor/composer/autoload_files.php
$vendorDir . '/luceos/on-app/src/OnAppInit.php',
vendor/composer/autoload_psr4.php
'Luceos\\OnApp\\' => array($vendorDir . '/luceos/on-app/src'),

Regarding the Guzzle question:
Just include it in your composer.json file:
"guzzlehttp/guzzle": "~5.0"
And then just use the normal
$client = new GuzzleHttp\Client();
Just don't forget to to composer dump-autoload

Related

I'm trying to use guzzlehttp/guzzle in Magento 2.2.9 but it's not working

It's throwing me an error
PHP Error: Cannot instantiate interface GuzzleHttp\\ClientInterface in vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 111, referer: http://127.0.0.1/local_m2/admin/admin/system_config/edit/section/active_campaign/key/6a9ce672c9414c57acd889eb50fb82020e13e651b74cf3a81b9cd8b30da45306/ here
I have already run all Magento required commands Like Setup: upgrade, di:compile and deploy but still it's throwing me this error.
I have already checked GuzzleHttp in the vendor folder, it's already installed in Magento 2.2.9
I have tried the composer require guzzlehttp/guzzle:^6.0 to reinstall the library but having no luck.
import this library:
use GuzzleHttp\Client;
use GuzzleHttp\ClientFactory;
use GuzzleHttp\Exception\RequestException;
on __construct use:
$this->client = new Client([
"base_uri" => url_base,
"timeout" => 2,
]);
and then call:
$response = $this->client->get(
url_base . "/" . url_api_point,
['headers' =>
[
'Authorization' => "Bearer {$this->token}" /* if have */
]
]
);
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
try this way to create an instance of GuzzleClient, im currently using a similar in a magento 2.4.4 and works fine, you don´t have to inyect that on __construct()
/**
* #return \GuzzleHttp\Client
*/
public function getClient()
{
$client = new \GuzzleHttp\Client(["base_uri" => "your url", "verify" => false]);
return $client;
}

Upload to Dropbox not working from Laravel with Dropbox driver

I'm trying to upload a (DomPDF generated) PDF file to Dropbox with the Dropbox driver in Laravel 8. I've installed spatie/flysystem-dropbox and created a DropboxServiceProvider.php with following contents:
<?php
namespace App\Providers;
use Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client;
use Spatie\FlysystemDropbox\DropboxAdapter;
class DropboxServiceProvider extends ServiceProvider
{
public function boot()
{
Storage::extend('dropbox', function ($app, $config) {
$client = new Client([$config['key'], $config['secret']]);
return new Filesystem(new DropboxAdapter($client));
});
}
public function register()
{
//
}
}
The service provider is also added to my config/app providers:
'providers' => [
...
App\Providers\DropboxServiceProvider::class,
...
]
In my config/filesystems I've added the dropbox driver (dropbox app key and secret are also set in .env file):
'dropbox' => [
'driver' => 'dropbox',
'key' => env('DROPBOX_APP_KEY'),
'secret' => env('DROPBOX_APP_SECRET'),
]
Now, when I try to run the following code, it returns false and the file doesn't appear in my Dropbox. When I change the disk to 'local', the file gets uploaded to my local storage.
$path = "pdf/file.pdf";
$storage_path = Storage::path($path);
$contents = file_get_contents($storage_path);
$upload = Storage::disk('dropbox')->put($path, $contents);
return $upload;
I've already tried clearing my config by running php artisan config:clear. After trying many different things, I have no idea what I'm doing wrong, so any advice will be appreciated!
The problem was not in the code, but in the permissions in my dropbox app: files.content.write wasn't enabled yet.

laravel use SSH in controller

Im trying to use SSH:: command in my controller but I'm facing error:
Class 'SSH' not found
my controller:
namespace App\Http\Controllers;
use Session;
use DB;
use SSH;
and call action:
SSH::run(
array(
'cd /var/www/laravel',
'node accept.js '
)
);
so where the problem could be?
You need to follow the installation guide of Laravel SSH package:
1. Install it via Composer:
Add this to your composer.json:
"require": {
"laravelcollective/remote": "~5.0"
}
Then run composer update.
2. Add new provider to the providers array of config/app.php:
'providers' => [
// ...
'Collective\Remote\RemoteServiceProvider',
// ...
],
3. Add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'SSH' => 'Collective\Remote\RemoteFacade',
// ...
],

PSR-4 autoloader Fatal error: Class not found

I have my project structure like so:
src/
├─ Model/
└─ User.php
My User.php file looks like this:
<?php
namespace Bix\Model;
class User {
And my composer.json autoloader is this:
"autoload": {
"psr-4": {
"Bix\\": "src/"
}
}
Finally my bootstrap.php is this:
use Bix\Model\User;
// PSR-4 Autoloader.
require_once "vendor/autoload.php";
However if I try and create a new User(), I get the error Fatal error: Class 'User' not found in /var/www/public/api/v1/index.php on line 8
Looking at the composer autoload_psr4.php file it looks ok:
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname($vendorDir));
return array(
'XdgBaseDir\\' => array($vendorDir . '/dnoegel/php-xdg-base-dir/src'),
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
'KeenIO\\' => array($vendorDir . '/keen-io/keen-io/src'),
'Bix\\' => array($baseDir . '/src'),
);
Can anybody point out where I am going wrong with the above?
First of all, Linux (I'm not sure which PC you use) is case-sensitive. In your autoloading, you defined src/bix, while it is src/Bix.
But more importantly, with PSR-4, the specified namespace prefix is not included in the directory structure (to avoid directories containing just one directory). In your case, if you configure "Bix\\": "src/", a class Bix\Model\User should be located in src/Model/User.php.
EDIT: You're misunderstanding PHP namespaces. In PHP, you're not saying "import everything from Bix\Model into the global namespace for this file" with use Bix\Model;. Instead, it means: "Alias Model in this file to Bix\Model".
So you should either do:
require_once "vendor/autoload.php";
use Bix\Model;
$user = new Model\User();
or:
require_once "vendor/autoload.php";
use Bix\Model\User;
$user = new User();

Laravel macros not autoloading

I have the following directory structure
app
| ...
| lib
| | Macro.php
| | Events.php
| ...
In my composer.json I have
"autoload": {
"classmap": {
...
"app/lib"
...
}
}
Events.php loads fine and updates the DB when the event triggers:
<?php
Event::listen('auth.login', function($user) {
$user->last_login = new DateTime;
$user->save();
});
Macros.php will always return the error Method messageBox does not exist if the macro is used in a view:
<?php
Form::macro('test', function()
{
return 'test';
});
I have run composer dump-autoload. Why is the Macros.php not autoloading?
The autoload classmap section in composer.json maps excatly that: classes. So it won't help you in this instance, because you don't have a class declaration in that file. You can however autoload files as well with composer, by adding this to the autoload section:
"autoload": {
"classmap": [
...
],
"files": [
"app/lib/Macros.php"
]
}
As an alternative you could always manually include the file in Laravel's app/start/global.php like so:
require app_path() . '/lib/Macros.php';

Resources