Opcache Preloading with Symfony 5.4 / PHP8 / Platform.sh - php

I am attempting to set up Opcache Preloading on Symfony 5.4 running on PHP8 at Platform.sh, and running into a fatal error.
Configuration
Necessary pieces included:
// platform.app.yaml
...
variables
php
'opcache.preload': 'var/cache/prod/App_KernelProdContainer.php'
...
hooks:
build: |
...
composer dump-autoload --no-dev --classmap-authoritative
deploy: |
...
sv restart app
...
I can verify that the App_KernelProdContainer.php file is being created at the correct location defined in the config above:
<?php
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
if (\class_exists(\ContainerF9HiXre\App_KernelProdContainer::class, false)) {
// no-op
} elseif (!include __DIR__.'/ContainerF9HiXre/App_KernelProdContainer.php') {
touch(__DIR__.'/ContainerF9HiXre.legacy');
return;
}
if (!\class_exists(App_KernelProdContainer::class, false)) {
\class_alias(\ContainerF9HiXre\App_KernelProdContainer::class, App_KernelProdContainer::class, false);
}
return new \ContainerF9HiXre\App_KernelProdContainer([
'container.build_hash' => 'F9HiXre',
'container.build_id' => '98091d49',
'container.build_time' => 1644501054,
], __DIR__.\DIRECTORY_SEPARATOR.'ContainerF9HiXre');
The included file ContainerF9HiXre/App_KernelProdContainer.php throws errors on the use statements like this:
Fatal error: Uncaught Error: Class "Symfony\Component\DependencyInjection\Container" not found in /app/var/cache/prod/ContainerF9HiXre/App_KernelProdContainer.php:17
Stack trace:
#0 /app/var/cache/prod/App_KernelProdContainer.php(7): include()
#1 {main}
thrown in /app/var/cache/prod/ContainerF9HiXre/App_KernelProdContainer.php on line 17
Use statements in the included file appear as such:
namespace ContainerF9HiXre;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

You seem to be using the wrong file for preloading.
The generated file is named something like srcApp_KernelProdContainer.preload.php. But in any case, you probably should't be adding this file directly to opcache.preload, but the Symfony generated config/preload.php.
Since on platform.sh the path is resolved relative to the project root, you can use a relative path like this:
opcache.preload=config/preload.php
If you do not have this file (e.g. incomplete recipes), you could simply re-generate it by running composer recipes:update symfony/framework-bundle, as explained here.
Or, ir you are not using Symfony Flex and want to do it manually, you can simply copy the file from the appropriate recipe, like this one.
It's a very simple script in any case:
<?php
if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
}

Related

running minimal app with xhp-lib v4 and hhvm v 4.81.1 throws error

I'm trying the following setup and am getting this error:
\nFatal error: Uncaught Error: Found top-level code in /home/user/code/xhp-simple/src/example.php:7\nStack trace:\n#0 (): main()\n#1 {main}
Setup:
composer.json
{
"require": {
"hhvm/hhvm-autoload": "^3.1",
"facebook/xhp-lib": "^4.0"
}
}
src/index.hack
use type Facebook\XHP\HTML\div;
// require_once(__DIR__."/../vendor/hh_autoload.hh"); // also tried here instead of in main
<<__EntryPoint>>
function main(): void {
require_once(__DIR__."/../vendor/hh_autoload.hh");
echo <div>{1 + 2}</div>;
}
hh_autoload.json
{"roots": ["src/"]}
run command:
hhvm -m server -p 8080 -d hhvm.server.default_document=./src/example.hack
I have hhvm v 4.83.1 installed
I think that you're running into the fact that hhvm-autoload hasn't caught up with the recent restrictions to top-level code. In particular, where require_once seems to no longer be allowed at the top level. With your hh_autoload.json, hhvm-autoload generates this hh_autoload.hh:
<?hh // partial
require_once(__DIR__.'/autoload.hack');
Facebook\AutoloadMap\initialize();
Where I believe that require_once is illegal. If you place this code in your main, it should work. I tested this with no problems on HHVM 4.84.0:
// src/index.hack
use type Facebook\XHP\HTML\div;
<<__EntryPoint>>
async function main(): Awaitable<void> {
require_once(__DIR__.'/../vendor/autoload.hack');
Facebook\AutoloadMap\initialize();
echo await (<div>{1 + 2}</div>)->toStringAsync();
}
$ # run with:
$ hhvm src/index.hack
Also note that all rendering is async now with XHP-lib, so you can't just echo XHP objects directly; instead:
Calls to $xhp->toString() need to be updated to $xhp->toStringAsync().
I've just noticed the updated README on hhvm-autoload, where indeed hh_autoload.hh has been phased out and you need to need to generate the autoload map yourself (emphasis mine):
Replace any references to vendor/autoload.php with vendor/autoload.hack and call Facebook\AutoloadMap\initialize()

php composer class not found when library is installed

I'm quite new to using composer and I am not too sure what I am doing at this point. I am currently trying to use this Nmap library I found. now once I have this library installed using this commandcomposer require willdurand/nmap
I created a index.php file with
<?php
require __DIR__ . '/vendor/autoload.php';
$hosts = Nmap::create()->scan([ 'example.com' ]);
$ports = $hosts->getOpenPorts();
echo $ports;
?>
This is what my composer.json file
{
"require": {
"willdurand/nmap": "^0.5.0"
}
}
When I run this I get PHP Fatal error: Uncaught Error: Class 'Nmap' not found in /var/www/html/nmap.php:5. I have Nmap installed on my Unix system. Any help on this issue would be great.
When you do not define a current namespace, PHP looks for any references classes in the root namespace. However, it cannot find Nmap in the root namespace because it is defined in the ´Nmap´ namespace.
You have to either add the namespace to the class defenition:
$hosts = \Nmap\Nmap::create()->scan([ 'example.com' ]);
Or, add a using statement for this class at the top of your file: (under <?php ofcourse)
use Nmap\Nmap;

Aspect mock 'Go\ParserReflection\ReflectionFile' not found

I am trying to use aspect mock with codeception tests.
From their documentation it is not clear how to configure.
https://github.com/Codeception/AspectMock
Include AspectMock\Kernel into tests/_bootstrap.php.
I do not have such file. Should I create it? Where should I include it?
My directory structure of codeception is:
test/codeception/acceptance.
I have file SummaryCest.php in test/codeception/acceptance.
Since I do not have _bootstrap.php file, I decided to try in SummaryCest - before declaring a class:
include __DIR__.'/../../../vendor/autoload.php'; // composer autoload
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [__DIR__.'/../../../'],
'excludePaths' => [__DIR__.'../../../vendor'],
'cacheDir' => '/tmp/datamanager',
]);
I do not know do I really need to exclude vendor directory, but I saw such suggestions. If that is mandatory, it should be written probably in readme which I did not see.
In includePaths there should be visible all my project files.
I have function in SummaryCest.php
public function correctSummaryCounts(AcceptanceTester $I)
{
\AspectMock\Test::double(SummaryController::class, ['get' => null]);
}
and when I run test
php codecept.phar run test/codeception/acceptance/SummaryCest.php
I get message
==== Redirecting to Composer-installed version in vendor/codeception ====
Codeception PHP Testing Framework v2.3.5
Powered by PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
PHP Fatal error: Uncaught Error: Class 'Go\ParserReflection\ReflectionFile' not found in /var/www/warehouseDataManager/vendor/codeception/aspect-mock/src/AspectMock/Intercept/BeforeMockTransformer.php:16
Stack trace:
#0 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php(124): AspectMock\Intercept\BeforeMockTransformer->transform(Object(Go\Instrument\Transformer\StreamMetaData))
#1 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php(83): Go\Instrument\Transformer\CachingTransformer->processTransformers(Object(Go\Instrument\Transformer\StreamMetaData))
#2 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/ClassLoading/SourceTransformingLoader.php(134): Go\Instrument\Transformer\CachingTransformer->transform(Object(Go\Instrument\Transformer\StreamMetaData))
#3 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/ClassLoading/SourceTransformingLoader.php(101): Go\Instrument\ClassLoading\SourceTran in /var/www/warehouseDataManager/vendor/codeception/aspect-mock/src/AspectMock/Intercept/BeforeMockTransformer.php on line 16
Can you explain me how to configure this?
Also I saw in readme
$userModel = test::double('UserModel', ['tableName' => 'my_users']);
but test is not even found. So I tried to use \AspectMock\Test which is at least found.
Notice that the error is throw before even running my test function. When I tried running before class declaration
$kernel->init();
it already gives same error.
_bootstrap.php files are no longer created automatically by Codeception.
To enabled them you have to add
settings:
bootstrap: _bootstrap.php
to codeception.yml file
and manually create _bootstrap.php files in tests directory and in every suite.
http://codeception.com/docs/reference/Configuration
ReflectionFile issue looks like autoloading issue.

Composer not auto-loading required classes

I'm new to Composer and I'm really struggling to auto-load my classes with composer. What am I missing in the following process?
I installed the package in my PHP includes folder (which is outside the document root - I'm not sure if that matters) like this:
composer require monolog\monolog
It stated it completed successfully and I confirmed the project was added to my vendor folder.
My entire composer.json file looks like this:
{
"require": {
"monolog/monolog": "^1.22"
}
}
My entire test file looks like this:
<?php
require_once "vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
?>
And I get this error when I load the page:
Fatal error: Uncaught Error: Class 'Monolog\Logger' not found in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php:6 Stack trace: #0 {main} thrown in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php on line 6
It includes the vendor/autoload.php file without any error.
I've tried to run these commands in composer without any change:
composer update
composer dump-autoload -0
I've also tried it with different packages and I get the same error, so I'm pretty sure it has nothing to do with the monolog package.
Is there a step here I'm missing? I don't need to manually define which classes to autoload in a json file if I require them in composer, do I?
Edit 1:
As requested, here's the paths to my different files.
Path to the test page:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php
Path to the composer.json file (outside the document root but in my includes path):
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\composer.json
My vendor folder is here:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\vendor\
And inside my vendor folder I have these folders and file:
bin/
composer/
monolog/
psr/
autoload.php
You need to include autoload in your qbtest.php as following:
require_once "../wwwincludes/vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");

Propel ORM and PHP

I am making a php application using propel ORM. It gives me the following message when I try to run it:
Fatal error: Uncaught Error: Class 'Propel\Runtime\Propel' not found in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php:2 Stack trace: #0 C:\MAMP\htdocs\Conference\vendor\bin\list.php(6): require_once() #1 {main} thrown in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php on line 2.
In my config.php generated file I have this written:
'classname' => '\\Propel\\Runtime\\Connection\\ConnectionWrapper'
What does it all mean? Am I missing some file or what?
I think you are missing a step in the building.
I assume you have your schema.xml file complete and you also have a propel.yaml (or with allowed extension file) properly configured. Also I assume you got Propel with Composer.
If you have all that the next steps are:
1) Open a terminal and go to your project directory, where the schema.xml and propel.yaml files are.
2) Execute the following command to get yout generated-sql (I have to do it this way on Windows):
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:build
3) Get your model classes with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel model:build
4) After generating the classes, you have to autoload them. Open your composer.json file with your text editor and add the following:
"autoload": {
"classmap": ["generated-classes/"]
}
It should look like this, for example:
{
"require": {
"twig/twig": "~1.0",
"propel/propel": "~2.0#dev"
},
"autoload": {
"classmap": ["generated-classes/"]
}
}
5) To finish the classes autoloading, you need to execute on your console:
composer dump-autoload
6) And for the runtime connection settings run this for comunicate classes at runtime:
c:\MAMP\htdocs\Conference\vendor\bin\propel config:convert
7) Assuming you have created your database, the last thing you need to do is create the tables, this is with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:insert
And there you go! That works for me every time I build a project.

Categories