My objective is to get the schema from an open source project on a git repository below:
https://github.com/monarc-project/zm-core
I am trying to set up the doctrine CLI for this application to probe the database and generate schema but after installing the dependencies and running doctrine I get the following error:
$php vendor/bin/doctrine
You are missing a "cli-config.php" or "config/cli-config.php" file in your
project, which is required to get the Doctrine Console working. You can use the
following sample as a template:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
// replace with file to your own project bootstrap
require_once 'bootstrap.php';
// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();
return ConsoleRunner::createHelperSet($entityManager);
From error message it seems there are two paths I can take:
I should not be using doctrine and instead use bin/console. However for console to work I need to upgrade to php 8.1 which doesn't meet this projects requirements. Thus is not an alternative.
If I still try to use doctrine I cannot see cli-config.php which I am presuming should have been there before. I tried to create one but then don't know which bootstrap.php should be used by cli-config.php. There are following bootstrap files present in vendor directory.
./vendor/symfony/polyfill-intl-normalizer/bootstrap.php
./vendor/symfony/polyfill-intl-idn/bootstrap.php
./vendor/symfony/polyfill-php81/bootstrap.php
./vendor/symfony/polyfill-ctype/bootstrap.php
./vendor/symfony/polyfill-php73/bootstrap.php
./vendor/symfony/polyfill-php80/bootstrap.php
./vendor/symfony/polyfill-mbstring/bootstrap.php
./vendor/symfony/polyfill-intl-grapheme/bootstrap.php
./vendor/symfony/polyfill-php72/bootstrap.php
./vendor/cakephp/utility/bootstrap.php
Related
I am new to php and have just installed my first package via composer. I'm now trying to call a function from the package I installed as follows:
<?php
require_once 'vendor/autoload.php';
$value = 1;
$aws = AmazonGiftCode::make()->buyGiftCard($value);
echo $aws;
?>
But I get the following error:
PHP Fatal error: Uncaught Error: Class 'AmazonGiftCode' not found in
/public_html/php/test.php:4 Stack trace:
#0 {main} thrown in /public_html/php/test.php on line 4
Based on my (albeit limited) experience with other languages, I'm guessing I have to load the package that contains the class first. The package folder is in the same directory as the test.php file, in the subfolder vendor/kamerk22/AmazonGiftCode/. But I think this is where I don't know enough to troubleshoot it based on the information I could find.
Your directory structure should look like this.
test.php
composer.json
vendor
└───autoload.php
└───kamerk22
└───AmazonGiftCode
Make sure you installed the package using composer, and not by downloading it.
I'm guessing I have to load the package that contains the class first.
Unlike what you would normally expect when importing stuff, when using composer you only have to import the autoload.php file, and composer will take care of loading other packages as needed. By as needed what I mean is that as soon as composer sees you use the AmazonGiftCode class it will import the AmazonGiftCode package, but if one of your REST endpoints doesn't use anything from the AmazonGiftCode package it won't ever load it. This allows you to not have to worry about slowing down the entire application when you want to use a composer package for only a few endpoints. At least that's the way I understand how composer works.
Just run composer dump-autoload once and the class should known then.
You could also get verbose and use kamerk22\AmazonGiftCode\AmazonGiftCode;
But that AmazonGiftCode looks quite Laravel specific... that's why it may still fail, even if it may be found by auto-load. One needs to setup Laravel framework in the first place; just see this query (just in case if you may wonder where all these missing classes may come from).
I am following this documentation since I want to auto generate entity depending on the table in my database, and at the XML generation stage I am passing this line php bin/console doctrine:mapping:import --force DoctrineMappingBundle xml. Since I dont have AppBundle, I created my own DoctrineMappingBundle.php in src/Entity/
My DoctrineMappingBundle.php looks like this
namespace App\Entity;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class DoctrineMappingBundle extends Bundle
{
}
It does generate the XML files, however when I run the entity generation command
php bin/console doctrine:mapping:convert annotation ./src, it throws up this error
In MappingException.php line 317:
An error occurred in App\Entity\Entity\BlogComment
and
In AnnotationDriver.php line 60:
Class App\Entity\Entity\BlogComment does not exist
I am stuck at this point and I dont know why it could not generate the entity classes, and it seems to automatically add an extra Entity in the namespace but in my namespace Entity is only mentioned once in the namespace. I am not sure which configuration is causing this issue.
My src folder structure
src---------/
|--Controller/
|--Entity--/
|-----DoctinemappingBundle.php
|--Migrations/
|--Repository/
|--Kernel.php
First off, the docs make it clear that this functionality is no longer supported and if your read the github issue you will see why.
However the functionality is still there and your idea of using a fake bundle will work. Just need the mapping bundle to be in the src directory. And then a bit of copy/paste.
Here is a complete step by step approach to generating entities under S4.
// Install the software
mkdir mapping
cd mapping
composer create-project symfony/skeleton .
composer require orm-pack
// adjust DATABASE_URL in .env
Make a fake bundle for the mapping command:
// src/DoctrineMappingBundle.php
namespace App;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class DoctrineMappingBundle extends Bundle
{
}
// Add the bundle to config/bundles.php
App\DoctrineMappingBundle::class => ['dev' => true],
Now we are ready to make some entities:
// Generate the xml mappings
bin/console doctrine:mapping:import DoctrineMappingBundle xml
// Mapping files generated under src/Resources/config/doctrine
// Generate the entities
php bin/console doctrine:mapping:convert annotation ./src
This generates the entities under src/App/Entity. At this point you will want to copy the generated entities to src/Entity and then remove the App/Entity directory. The namespace should already be correct.
Get rid of the mapping files by removing src/Resources as well.
This is as far as I took the exercise but you should be good to go.
Resolving the issue. My Case:
Before reading the question, my issue was solved due to my development environment. Using CodeKit (an application on MacOS), upon building my code from the source folder, items such as the composer.json and other files did not transfer causing the issues described below. If this does happen to you scout the two folder to look for discrepancies the paste the missing docs from the src to the build folder.
:: QUESTION ::
I am starting to use GCP today and after following the instructions defined here:
composer require google/cloud-storage
then:
putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json");
require __DIR__.'/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$myid = "my-project-id";
$storage = new StorageClient([
'projectId'=>$myid
]);
var_dump($storage->buckets());
When running this i get the following error:
Fatal error: Class 'Google\Auth\Cache\MemoryCacheItemPool' not found in /place/to/vendor/google/cloud-core/RequestWrapperTrait.php on line 94
I have no idea how to solve the issue, as i am just getting started with GCP. No idea whether this is a problem with the platform or my code.
File structure appears as follows for the Google Auth:
vendor
google
auth
src
tests
cloud-core
cloud-storage
the /Cache/MemoryCacheItemPool exists inside both the tests and src folder, but the above is referencing it minus the src or tests folder.
I have also ran:
composer update
and uninstalled and reinstalled the package to no effect
Google Cloud Project Link
Where did you find the code that you used and pasted? Because the one present in the official documentation is different.
This portion of code is the one in the tutorial you linked, try to use the client library and post the error logs if get any!
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$projectId = 'YOUR_PROJECT_ID';
$storage = new StorageClient(['projectId' => $projectId]);
$bucketName = 'my-new-bucket';
$bucket = $storage->createBucket($bucketName);
echo 'Bucket ' . $bucket->name() . ' created.';
Remember that bucket name should be unique and therefore I would advice you to test it with a long complex name to avoid to hit already used names, and always test the result of the operation.
UPDATE
I tested also your code and it is working as well, therefore I believe that is an error in the setup of the environment.
Did you get any error while running the composer require google/cloud-storage? Because the class that is missing Google\Auth\Cache\MemoryCacheItemPool is part of Psr that is installed by the composer
[...]
Installing psr/cache (1.0.1)
Loading from cache
[...]
UPDATE2
Matthew M found the error in its configuration and posted:
Finally resolved the issue. I'm using CodeKit in my working
environment and it looks like it is changing something when it
compiles. Ran an uncompiled version and it's working fine.
Introduction
I've never worked with a framework before (Zend, CakePHP, etc) and finally decided to sit down and learn one. I'm starting with Laravel because the code looks pretty and unlike some other frameworks I tried to install, the "Hello, World!" example worked on the first try.
The Goal
For the time being, I want my app to do something very simple:
User submits a request in the form of: GET /dist/lat,lng
The app uses the remote IP address and MaxMind to determine $latitude1 and $longitude1
This request path is parsed for $latitude2 and $longitude2
Using these two positions, we calculate the distance between them. To do this I'm using Rafael Fragoso's WorldDistance PHP class
Since I plan to re-use this function in later projects, it didn't seem right to throw all of the code into the /app directory. The two reusable parts of the application were:
A service provider that connects to MaxMind and returns a latitude and longitude
A service provider that takes two points on a globe and returns the distance
If I build facades correctly then instead of my routes.php file being a mess of closures within closures, I can simply write:
Route::get('dist/{input}', function($input){
$input = explode( "," , $input );
return Distance::getDistance( GeoIP::getLocation(), $input );
});
What I've tried
Initial Attempt
For the first service provider, I found Daniel Stainback's Laravel 5 GeoIP service provider. It didn't install as easily as it should have (I had to manually copy geoip.php to the /config directory, update /config/app.php by hand, and run composer update and php artisan optimize) however it worked: A request to GET /test returned all of my information.
For the second service provider, I started by trying to mimic the directory structure and file naming convention of the GeoIP service provider. I figured that if I had the same naming convention, the autoloader would be able to locate my class. So I created /vendor/stevendesu/worlddistance/src/Stevendesu/WorldDistance\WorldDistanceServiceProvider.php:
<?php namespace Stevendesu\WorldDistance;
use Illuminate\Support\ServiceProvider;
class WorldDistanceServiceProvider extends ServiceProvider {
protected $defer = false;
public function register()
{
// Register providers.
$this->app['distance'] = $this->app->share(function($app)
{
return new WorldDistance();
});
}
public function provides()
{
return ['distance'];
}
}
I then added this to my /config/app.php:
'Stevendesu\WorldDistance\WorldDistanceServiceProvider',
This fails with a fatal error:
FatalErrorException in ProviderRepository.php line 150:
Class 'Stevendesu\WorldDistance\WorldDistanceServiceProvider' not found
Using WorkBench
Since this utterly failed I figured that there must be some other file dependency: maybe without composer.json or without a README it gives up. I don't know. So I started to look into package creation. Several Google searches for "create package laravel 5" proved fruitless. Either:
They were using Laravel 4.2, in which case the advice was "run php artisan workbench vendor/package --resources"
Or
They were using Laravel 5, in which case the docs were completely useless
The official Laravel 5 docs give you plenty of sample code, saying things like:
All you need to do is tell Laravel where the views for a given namespace are located. For example, if your package is named "courier", you might add the following to your service provider's boot method:
public function boot()
{
$this->loadViewsFrom(__DIR__.'/path/to/views', 'courier');
}
This makes the assumption that you have a service provider to put a boot method in
Nothing in the docs says how to create a service provider in such a way that it will actually be loaded by Laravel.
I also found several different resources all of which assume you have a repository and you just want to include it in your app, or assume you have "workbench". Nothing about creating a new package entirely from scratch.
PHP Artisan did not even have a "workbench" command, and there was no "workbench.php" file in /config, so anything I found related to workbench was worthless. I started doing some research on Workbench and found several different questions on StackOverflow.
After a long time and some experimentation, I managed to get laravel/workbench into my composer.json, composer update, composer install, manually build a workbench.php config file, and finally use the PHP Artisan Workbench command to make a new package:
php artisan workbench Stevendesu/WorldDistance --resources
This created a directory: /workbench/stevendesu/world-distance with a number of sub-directories and only one file: /workbench/stevendesu/world-distance/src/Stevendesu/WorldDistance/WorldDistanceServiceProvider.php
This service provider class looked essentially identical to the file I created before, except that it was in the /workbench directory instead of the /vendor directory. I tried reloading the page and I still got the fatal error:
FatalErrorException in ProviderRepository.php line 150:
Class 'Stevendesu\WorldDistance\WorldDistanceServiceProvider' not found
I also tried php artisan vendor:publish. I don't really know what this command does and the description wasn't helpful, so maybe it would help? It didn't.
Question
How do I create a new service provider as a package so that in future projects I can simply include this package and have all the same functionality? Or rather, what did I do wrong so that the package I created isn't working?
After two days of playing with this I managed to find the solution. I had assumed that the directory structure mapped directly to the autoloader's path that it checked (e.g. attempting to access a class Stevendesu\WorldDistance\WorldDistanceServiceProvider would look in vendor/stevendesu/world-distance/WorldDistanceServiceProvider)... This isn't the case.
Reading through the composer source code to see how it actually loads the files, it builds a "classmap" - essentially a gigantic array mapping classes to their respective files. This file is built when you run composer update or composer install - and it will only be built correctly if composer knows the details of your package. That is - if your package is included in your project's composer.json file
I created a local git repository outside of my app then added my package to my app's composer.json file then ran composer update -- suddenly everything worked perfectly.
As for the:
It didn't install as easily as it should have
the secret sauce here was first add the service provider to /config/app.php then, second run php artisan vendor:publish
Monolog : http://github.com/Seldaek/monolog
Symfony Class Loader : https://github.com/symfony/ClassLoader
As per the usage instructions on the Monolog site, I'm trying to load the class loader to load monolog in my php project. I cant get 'Composer' installed on my machine (firewall problems from my work machine), so I'm trying to follow the instructions on monolog and symfony sites but I'm having issues.
Here is my SAMPLE of my directory structure with my php code :
myProj/
--ClassLoader/ (symfony)
----UniversalClassLoader.php
--Monolog/
----Formatter/
----Handler/
----Processor/
----Logger.php
--myPhpFile.php
And here is my php code to attempt to 'require' monolog
require_once(realpath('ClassLoader/UniversalClassLoader.php'));
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->register();
$loader->registerNamespace('Monolog', realpath('Monolog'));
require_once(realpath('Monolog\Logger.php')); //exception generated here! :-(
and here is the php exception I'm getting inside the Monolog\Logger.php as soon as it attempts to 'require' monolog
Interface 'Psr\Log\LoggerInterface' not found
but I cant even FIND anything that looks like a 'psr\log' namespace in the monolog code. What bits am I missing?
Psr\Log is a dependency of monolog, if you are not using Composer you'll have to track down all dependencies and it's probably not going to be fun (though for monolog there is only the psr-log package). If you really want you can find it at https://github.com/php-fig/log - but you could also try to download composer via your browser, just grab https://getcomposer.org/composer.phar