How to create an Aura DI container? - php

I installed a composer, downloaded the Aura, created an index.pxp and wrote in it:
require('vendor/autoload.php');
use Aura\Di\ContainerBuilder;
$builder = new ContainerBuilder();
$di = $builder->newInstance();
$object = $di->newInstance('Vendor\Package\ClassName');
But phpStorm says:Undefined namespase DI
And i have error: Fatal error: Class 'Aura\Di\ContainerBuilder' not found in... on line 4
I do as follows: http://auraphp.com/packages/3.x/Di/getting-started.html#1-1-1

In order for the composer auto-loader to pick up \Aura\Di, the dependency needs to be managed by composer.
You can easily do this by executing
composer require aura/di
which will add the dependency to your composer.json file and register with the auto-loader.
If you have manually downloaded and installed aura/di, you can revert that.

Related

Having real problems with Composer

I have a dedicated server with WHM and cPanel. It already has composer installed on it.
I'm trying to run the following php lines in a webpage:
require __DIR__ . '/../vendor/autoload.php';
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', '[my api key]');
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
I've created a composer.json in the public_html folder and put the following in it:
{
"autoload": {
"psr-4": { "Swagger\\Client\\" : "lib/" }
}
}
And then I ran composer update in the terminal which seemed to install the dependencies and all the relevant files.
It's seeing the autoload.php file but I'm still getting a class not found error:
Fatal error: Uncaught Error: Class 'Swagger\Client\Configuration' not found in /home/mywebsite/public_html/converter/sms.php:9 Stack trace: #0 {main} thrown in /home/mywebsite/public_html/converter/sms.php on line 9
I've been at this for 4 hours now. What am I doing wrong? I can't find anything online that will guide me in the right direction.
In Composer Basic Usage documentation about autoloading it says
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work
So, as #NicoHaase said in the comments, if you installed Swagger with Composer (adding a require key and running composer update, for example) you don't need to specify the autoload path to Swagger in composer.json.

Class not found exception in PHP from composer

I am trying to include a package from composer, but I am receiving a class not found error.
I have tried the below possibilities.
$supermeteor = new \Supermeteor\Supermeteor('XXXXXXXX');
and
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor('xxxxxxxx');
Packages composer.json:
"psr-4": {
"Supermeteor\\": ""
}
Packages namespace :
namespace Supermeteor;
Packages class name :
class Supermeteor() {}
Error message
Uncaught Error: Class 'Supermeteor\Supermeteor' not found in
C:\path\to\my\file.php:16
I just tested your package locally, and it seems to work fine for me using the same code as you provided in your question. This is how I tested it.
1. Create a new project
Create a new directory on your computer.
2. Add the package to a new project using Composer
Locate your new directory on the command line and add the package to your projects autoloader by running the below composer command.
composer require supermeteor/sdk-php
3. Use the package
Create an index.php file in the same directory as your composer.json and add the below code.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor("xxx");
4. Test the results
In the terminal window start a new php server to serve your project.
php -S localhost:8089
Now access the site via your browser at http://localhost:8089.

doctrine odm annotations or composer autoload.php not working?

I'm trying to use Doctrine MongoDB ODM 2.0 beta on a project with the Yii2 framework, with composer version 1.8.4 and PHP 7.2, but I keep getting the error Fatal error: Uncaught Error: Call to a member function add() on boolean where the code runs $loader->add('Documents', __DIR__);
bootstrap.php file (in DIR/bootstrap.php):
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('fsa');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));
$dm = DocumentManager::create(null, $config);
I already tried looking at How to properly Autoload Doctrine ODM annotations? and Laravel & Couchdb-ODM - The annotation "#Doctrine\ODM\CouchDB\Mapping\Annotations\Document" does not exist, or could not be auto-loaded and a host of other threads I can't quite recall for help, but I couldn't figure out a solution.
I also tried commenting out the lines below
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
and ran composer dump-autoload and on command line it returned Generated autoload files containing 544 classes, but then I got the problem
[Semantical Error] The annotation "#Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Documents\Message does not exist, or could not be auto-loaded.
So the annotations are not auto-loading, and I have no idea how to fix that.
In the model I have:
<?php
namespace Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use \Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
/** #ODM\Document */
class Message
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="int") */
private $sender_id;
...
I also posted a thread on github at https://github.com/doctrine/mongodb-odm/issues/1976. One commenter stated that "By default, the composer autoload file returns the autoloader in question, which seems to not be the case for you." How can I fix that? The only information I can find online is to put (inside composer.json) the lines:
"autoload": {
"psr-4": {
"Class\\": "src/"
}
},
but then what class should I be loading?
I'm very confused and being pretty new to all these tools (mongodb, yii2, etc.) doesn't help at all. I'm not sure what other information would be helpful else I would post it.
Thanks in advance.
So turns out that the problem (as was mentioned in https://github.com/doctrine/mongodb-odm/issues/1976) was that autoload.php was required twice - once in bootstrap.php and once in web/index.php (of the framework). After the require line in index.php was removed, everything worked fine.

Calling Flysystem, why do I get PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found?

I try to run Flysystem's basic example code for the Local adapter and get a Class 'League\Flysystem\Adapter\Local' not found error. This is my process:
version check:
php -v
PHP 5.5.9-1ubuntu4.23 (cli) (built: Feb 8 2018 21:59:47)
install Flysystem:
composer require league/flysystem
output shows I'm up-to-date (this is my 2nd time running it):
Using version ^1.0 for league/flysystem
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
now there's a vendor folder in web root. And within ./web/vendor/league/flysystem/src/Adapter$ are these files:
AbstractAdapter.php
AbstractFtpAdapter.php
CanOverwriteFiles.php
Ftpd.php
Ftp.php
Local.php
NullAdapter.php
Polyfill/
SynologyFtp.php
...just showing that it seems to be installed correctly(?) I create one test file and one test directory in my web root:
fly-local.php
myfiles/
Into fly-local.php I paste the text from their docs (https://flysystem.thephpleague.com/docs/adapter/local/):
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
...and change the adapter's root folder to myfiles (is that correct?). Then I run it:
php fly-local.php
It outputs:
PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found in /[PROJECT DIR]/web/fly-local.php on line 6
PHP Stack trace:
PHP 1. {main}() /[PROJECT DIR]/web/fly-local.php:0
What am I doing wrong?
You used composer, then you need to include the composer autoload.php file.
The fly-local.php should be:
<?php
require __DIR__.'/vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
If you use a framework, you can see it includes the autoload php file for you (index.php, in general). If your test/custom file is not included to framework, you need to include the file manually.

Asset Function without Symfony in Twig

I'd like to use the asset function into my project where I'm only using Twig.
I tryed to use it but php return me this :
Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "asset" function in "./Default/base.html.twig"
The solution is on sensiolabs.org.
I added this in conf.php:
//config/config.php
require_once('../vendor/autoload.php');
//twig
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('../app/views');
$twig = new Twig_Environment($loader);
$twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) {
// implement whatever logic you need to determine the asset path
return sprintf('../assets/%s', ltrim($asset, '/'));
}));
That's because Assetic itself is a standalone PHP library. So to use asset in twig you need to install the bundle.
Run the following composer command to install assetic bundle.
composer require symfony/assetic-bundle
Without installing the standalone package, twig won't able to find the asset function.

Categories