Elasticsearch serivce not found - symfony

I'm trying to integrate elasticsearch into my symfony application. But I couldn't find any appropriate example on the internet. Now I'm getting this service not found error:
Service "fos_elastica.finder.inscriptions.inscription" not found: even though it exists in the app's container, the container inside "App\Controller\SearchController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session", "templating" and "twig" services. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "SearchController::getSubscribedServices()".
For:
$finder = $this->container->get('fos_elastica.finder.inscriptions.inscription');
As:
Symfony\Component\DependencyInjection\Exception\
ServiceNotFoundException
I don't know how to solve this problem because of insufficient experience with Symfony. Does anyone know how to solve this or any Elasticsearch example with Symfony on the internet?
Thanks!

In your config/services.yaml file you have to bind this service as argument for your controller like that:
App\Controller\SearchController:
bind:
$finder: '#fos_elastica.finder.inscriptions.inscription'
Then you can use it in your controller by injecting it through autowiring:
public function index($finder) {
// your code here
}
For more information refer to the documentation.

Related

Access monolog configuration values

I need to get the list of available channels from monolog configuration file , whene i try to inject these parameters into my custom service like this
logger:
class: App\Services\Logger
arguments:
$channels: '%monolog.channels%'
i got
You have requested a non-existent parameter "monolog.channels".
is odd that symfony don't provide a simple way to access all configurations (even for other bundles like monolog )
is their a way to do this or am i missing something ?

Getting a Simple Symfony 5 App On Amazon AWS As An API Gateway/Lambda For Amazon Cognito

This may involve way too many specific technologies for anyone to be able to answer, but I thought I'd give it a shot. I've ready SO Many tutorials and feel like I'm SO close...there's maybe just one thing I'm missing.
My goal here is multi-part, and as far as I know my issue is only really with one part at this point
Take a simple Symfony app (it happens to be using version 5) with a single controller, put it up on Amazon AWS as an API Gateway. I've accomplished this with the help of bref
(https://bref.sh/) and requiring it in my Symfony project. I have a serverless.yml file for which I can use the serverless deploy command to upload it as an API Gateway to AWS. It even tests out in a browser, no problem.
Take that API Gateway and link it to an AWS Lambda function, to eventually be used as a Lambda function for an event to be triggered upon sign-in etc. using Amazon Cognito. I've achieved this using tutorials, I can see it linked to the API Gateway when I go into my Lambda function that was uploaded during step 1 using bref.
Take this Lambda and link it to the Cognito Lambda trigger for Pre sign-up. I've done this, it's a very simple setting.
Now in the single file PHP examples for how to use bref with AWS, you get something as follows:
require __DIR__.'/vendor/autoload.php';
lambda(function ($event) {
return $event;
});
I have uploaded this as a different lambda (nothing to do with Symfony here), set that to be the trigger on Cognito's Pre-sign-up event, and it works fine. It does nothing granted, but it passes the information along correctly to where it does not cause any errors of any kind.
Now when I try this same thing with a Symfony app, I have IndexController.php in my src/Controller directory, that looks like this:
<?php
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Maxbanton\Cwh\Handler\CloudWatch;
use Monolog\Logger;
use Monolog\Formatter\JsonFormatter;
class IndexController extends AbstractController
{
/**
* #Route("/", name="index")
*/
public function index(Request $request, LoggerInterface $logger)
{
$this->awsLogger($request);
return new JsonResponse(json_encode($request->query->all()));
}
private function awsLogger(Request $request)
{
/**
* This is only here for now because I couldn't get CloudWatch logging working any better way.
*/
$sdkParams = [
'region' => 'us-east-2',
'version' => 'latest',
'credentials' => [
'key' => 'AKIAIGFNOM3BKSE6HQ6A',
'secret' => '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',
]
];
// Instantiate AWS SDK CloudWatch Logs Client
$client = new CloudWatchLogsClient($sdkParams);
// Log group name, will be created if none
$groupName = 'lambda_symfony_test';
// Log stream name, will be created if none
$streamName = 'dev';
// Days to keep logs, 14 by default. Set to `null` to allow indefinite retention.
$retentionDays = 30;
// Instantiate handler (tags are optional)
$handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, 10000, ['tag' => 'lambda']);
// Optionally set the JsonFormatter to be able to access your log messages in a structured way
$handler->setFormatter(new JsonFormatter());
// Create a log channel
$log = new Logger('channel');
// Set handler
$log->pushHandler($handler);
// Add records to the log
$log->debug(print_r($request->query->all(), true));
}
}
I completely understand this will cause an error as a lambda, because the JsonResponse is not what Cognito is expecting to see back. However I was at least hoping that I'd hit the call to the logger so I could see what my request object looked like, and I could see what I was working with in terms of what Cognito is passing into this Lambda. This will help me eventually figure out what I need to return. And as far as I know I can't just use the "lambda" wrapper around a controller method like what was done in the simple example I mentioned I had working. However it's not even getting this far, instead the following is what appears in my logs on CloudWatch:
21:34:58
Fatal error: Uncaught Exception: The lambda was not invoked via HTTP through API Gateway: this is not supported by this runtime in /var/task/vendor/bref/bref/src/Runtime/PhpFpm.php:120
So my question is what am I missing here? This API works completely fine through a browser when I just pass it something as a GET request. What am I missing in my configurations? Here is my serverless.yml file for the Symfony app:
service: lambda-test-symfony
provider:
name: aws
region: us-east-2
runtime: provided
environment:
# Symfony environment variables
APP_ENV: prod
plugins:
- ./vendor/bref/bref
package:
exclude:
- node_modules/**
- tests/**
functions:
lambdatest:
handler: public/index.php
layers:
- ${bref:layer.php-72-fpm}
events:
- http: 'ANY /'
- http: 'ANY /{proxy+}'
Here are also some screenshots from my Lambda configurations on AWS:
https://i.stack.imgur.com/TDMZj.png
https://i.stack.imgur.com/ciehT.png
And here are some from the setup of the API Gateway:
https://i.stack.imgur.com/RL7D9.png
https://i.stack.imgur.com/LPcsh.png
https://i.stack.imgur.com/RVNwN.png
https://i.stack.imgur.com/4bs0u.png
I'm still fairly new to the whole AWS/serverless thing, so I could be missing something really obvious here. I'm not 100% sure whether the issue is with my code or my configuration though. Any help would be appreciated!

Find out if you are running in a command and the command parameters in Symfony

We have a service that can be called from a Symfony command and from a normal web request. Is there a way to find out if the service was called from a command or from a web request? If so, if it was called from a command, is there a way to find out the parameters that were used when running the command?
In symfony Console,
the command line context does not know about your VirtualHost or domain name
It means that you can check the request scheme, host, base_url and base path since these request properties have no values in the console context unless you configure them (https://symfony.com/doc/current/console/request_context.html#configuring-the-request-context-globally)
Hi you can use this to know if the service is used from the cli, if it runs with apache you will get this apache2handler
if(php_sapi_name() === 'cli') {
//some code
}
https://www.php.net/manual/en/function.php-sapi-name.php

Corda Whitelist not getting updated

I am using Springboot RPC webserver to run my apis. I have put java.util.Date in Whitelist still throwing error.
Class java.util.Date is not annotated or on the whitelist, so cannot be used in serialization
I created a class
import net.corda.core.serialization.SerializationWhitelist
class RequiredSerializationWhitelist : SerializationWhitelist {
override val whitelist: List<Class<*>> = listOf(java.util.Date::class.java)
}
and put the file net.corda.core.serialization.SerializationWhitelist in \src\main\resources\META-INF\services with following code
com.p2p.RequiredSerializationWhitelist
While deploying Nodes I get
No existing whitelist file found.
Calculating whitelist for current installed CorDapps..
CorDapp whitelist generated in <path>\build\nodes\whitelist.txt
Updating whitelist
Bootstrapping complete!
When I open my Whitelist file in the path given only my Contracts are shown. What am I missing.
I think there are not the same whitelist.
\build\nodes\whitelist.txt is file for Corda contract whitelist class. But your problem is net.corda.core.serialization.SerializationWhitelist that should be in JAR file under META-INF/services. Can you check that in your JAR file and this path it's contain SerializationWhitelist file?

Using Symfony expression in 3rd party bundle configuration

I am trying to configure HWIOAuth bundle to use dynamic client id. I have a service that can figure out what variable value to use depending on some request conditions.
Here's an expression in my HWI configuration, but can't seem to get it work:
hwi_oauth:
firewall_names:
- main
resource_owners:
auth:
client_id: "#=service('host.configmgr').GetClientIdParameter()"
client_secret: '%client_secret%'
The issue us with #=service('host.configmgr').GetClientIdParameter(). Can that even execute evaluate? The service is properly executes an used by another class somewhere else with success.
The %client_secret% parameter replacement works fine.
For more context, this part of the configuration lives in a dedicated file that is included in config.yml via { resource: hwiconfig.yml }
After some research looks like expressions are only supported for service definitions. This functionality is not supported for configurations outside services like the HWI bundle config.

Resources