Elasticsearch-php 5.x - Can't put templates - php

Using the official elasticsearch-php client, version 5.x.
I'm trying to put a template inside my Elasticsearch instance, but it simply
doesn't return anything.
$data = [
'name' => 'testTemplate',
'body' => [
'template' => 'testTemplate-*',
'mappings' => [
'foo' => 'string',
'bar' => 'string',
],
],
];
$this->client = ClientBuilder::create()
->allowBadJSONSerialization()
->setHosts($host)
->build();
$this->client->putTemplate($data);
I must use allowBadJsonSerialization() because i'm running on PHP 5.4, but on 5.6 it does the same thing.
I don't know if it's a bug or i'm doing something wrong.
Can you help me? Thanks.

I solved this by using CURL according to documentation.
Unfortunately the library doesn't work for the 5.x branch.

Related

Using Native Enum in Api-Platform / OpenApi

I am learning OpenApi/Swagger API with Api-Platform. I created a new endpoint, that accepts values of an enum as a parameter:
#[ApiResource(
itemOperations: [
'get_by_name' => [
'openapi_context' => [
....
'parameters' => [
[
'in' => 'header',
'name' => 'X-Server-Region',
'schema' => [
'type' => 'string',
'enum' => ['server1', 'server2'],
'example' => 'server1',
],
'description' => 'Server to select',
'required' => true
],
...
)]
However, this is a rather common param and values can be updated frequently (as more servers are added), I'd like to use some kind of template.
So I tried:
<?php
namespace App\Enum;
enum Server: string
{
case SERVER1 = 'server1';
case SERVER2 = 'server2';
...
}
with
'enum' => [...Server::cases()],
or
'enum' => [Server::class],
and many other forms of that, to no avail.
I tried to understand the concept of components, but could not find a way to use them in Symfony/Api Platform.
How could I reuse an enum at different endpoints?
Enums being fairly new to PHP, they are not yet directly supported by Api-Platform.
Support will come, but for the time being you'll have to explicitly list each of the cases manually on the configuration.
While you could also store the list of 'cases' in a constant in a class (you could even do it in the enum itself):
enum Server : string {
const CASES = ['server1', 'server2']
case SERVER1 = 'server1';
case SERVER2 = 'server2';
}
And then use that constant directly in annotations or attributes:
parameters' => [
[
'in' => 'header',
'name' => 'X-Server-Region',
'schema' => [
'type' => 'string',
'enum' => Server::CASES,
'example' => 'server1',
],
'description' => 'Server to select',
'required' => true
],
... this wouldn't be really using the enum advantages, as you would still need to edit the cases in two places instead of only just the one; and would only be convenient for annotations or attributes. If you used XML or YAML configuration for your API resources, it wouldn't be that great.
To temporarily solve the problem until the Enum support is available, you can create a class with a prototype of your data to reuse it in your API. This way you can reuse the same data set without duplicating it. This will make things easier for you in the future.
And so you create a prototype of your data like this:
class Server
{
public const PROTOTYPE = [ 'select1', 'select2', 'select3'];
}
Import calss and then call it in your annotations like this:
'enum' => Server::PROTOTYPE,

Laravel log file specific to a package

I'm writing a couple of laravel packages and I'm wondering if it is possible to have the package write to a specific log file but only for messages related to the package?
I tried making a logging.php file in the packages/myorg/mypackage/config (below) but it doesn't seem to do anything.
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
]
];
I am using "jeroen-g/laravel-packager" to set up the packages. It appears to manually load the mypackage.config in the ServiceProvider bootForConsole
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
mypackage.'/../config/mypackage.php' => config_path('mypackage.php'),
], 'mypackage.config');
}
I'm not sure how to add custom logging to that though. I'm still learning Laravel and I'm not quite sure what or how the main applications config/logging.php is read so I'm not quite sure how to inject a custom version for an add-on package.
EDIT:
I found a post that suggested using the following in the ServiceManager boot() method:
$this->app->make('config')->set('logging.channels.mychannel', [
/* settings */
]);
I used the package config to set a 'logging' => [ 'channels' => [ 'mychannel' => [ /* settings */ ] ] ] and could then do the same thing as above with:
$this->app->make('config')->set('logging.channels.mychannel', config('mypackage.logging.channels.mychannel');
But that still required something in the code. The next best thing I have found thus far is to change my config/logging.php to config/logging.channels.php and include something like:
return [
'mychannel' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
];
Then in the service provider register() method add:
$this->mergeConfigFrom(__DIR__ . '/../config/logging.channels.php', 'logging.channels');
I tried doing it from the original 'logging.php' with channels array nested in a 'logging' key, but array_merge doesn't appear to merge the nested elements so my channel never showed up in logging.channels.
I'm not sure if this is ideal, however. I'd still like to know if there is a 'better' or best practices way of adding custom package logging parameters and whether there is a need to publish it in any way (and how).

how to use or import ffmpeg in a laravel controller

i have already installed the laravel-ffmpeg via composer in my project
composer require pbmedia/laravel-ffmpeg
then i added class to config/app.php file as documentation says
// config/app.php
'providers' => [
...
ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider::class,
...
];
'aliases' => [
...
'FFMpeg' => ProtoneMedia\LaravelFFMpeg\Support\FFMpeg::class
...
];
then publish this config file
php artisan vendor:publish --provider="ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider"
now my question is to use this package in my project, how should i use this into my controller
i saw many post and ques regarding this and i get these
use FFMpeg;
use FFMpeg\FFMpeg;
use Pbmedia\LaravelFFMpeg\FFMpeg;
use ProtoneMedia\LaravelFFMpeg\FFMpeg;
.
.
.
which one is correct, my goal is to trim a video, but which one works for that i am so confused
//config/laravel-ffmpeg.php
<?php
return [
'ffmpeg' => [
'binaries' => env('FFMPEG_BINARIES', 'ffmpeg'),
'threads' => 12,
],
'ffprobe' => [
'binaries' => env('FFPROBE_BINARIES', 'ffprobe'),
],
'timeout' => 3600,
'enable_logging' => true,
'set_command_and_error_output_on_exception' => false,
];
please help me out, thanks in advance
Use below code in your controller above class
use FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
Open file using FFMpeg in your controller method
$media = FFMpeg::open($file_path);

How Visual Studio Code will use to format PHP like PhpStorm?

I have used phpfmt extension to indent the code which formatted code like this
'cms' => [
'class' => 'yii2mod\cms\Module',
'controllerNamespace' => 'backend\controllers',
'defaultRoute' => 'cms',
],
And when I merge it return the code intend error. I need the code to format like PhpStorm did this
'cms' => [
'class' => 'yii2mod\cms\Module',
'controllerNamespace' => 'backend\controllers',
'defaultRoute' => 'cms',
],
Which extension and how I use it in Visual Studio Code to get rid of PHP intended error in Visual Studio Code?
You can use the following settings that i use for my development env, would be closest to what you are looking for.
Add the following into your settings.json in VSCode.
//phpfmt
"phpfmt.php_bin": "php",
"phpfmt.passes": [
"AlignPHPCode",
"AlignTypeHint",
"AddMissingParentheses",
"ConvertOpenTagWithEcho",
"DocBlockToComment",
"IndentTernaryConditions",
"JoinToImplode",
"PSR2KeywordsLowerCase",
"PSR2LnAfterNamespace",
"PSR2CurlyOpenNextLine",
"PSR2ModifierVisibilityStaticOrder",
"PSR2SingleEmptyLineAndStripClosingTag",
"ReindentSwitchBlocks",
"RemoveUseLeadingSlash",
"StripExtraCommaInArray",
"SpaceBetweenMethods",
],
"phpfmt.exclude": [
"ReindentComments",
"StripNewlineWithinClassBody"
],
"phpfmt.psr2": false,
For double array formatting you will find in settings like the attached image.
And if you want to do it in settings.json, add AlignPHPCode into phpfmt.passes array.

elasticsearch-php create index returns \BadRequest400Exception

I have a fresh installation of elasticsearch 5.0.0 and elasticsearch-php . I am trying to create an index.
I run the code from this index management documentation:
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index'
];
// Create the index
$response = $client->indices()->create($params);
and it works. I create an index successfully.
I try the next code snippet:
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'my_type' => [
'_source' => [
'enabled' => true
],
'properties' => [
'first_name' => [
'type' => 'string',
'analyzer' => 'standard'
],
'age' => [
'type' => 'integer'
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
and I get:
Elasticsearch\Common\Exceptions\BadRequest400Exception with message 'No handler found for uri [/my_index] and method [POST]'
any ideas why?
This code used to work when I used elasticsearch 2.0
EDIT: I found this question so either it is a problem with elasticsearch-php or I need to update it I guess
I am using elasticquent which I have just realized requires elasticsearch-php version <2.2 so this is what is causing the problem
Looking at the error message:
No handler found for uri [/my_index] and method [POST]
This means that your create index call is using an HTTP POST method under the hood. In previous versions (i.e. pre 5.0), the elasticsearch-php client used to create indices with an HTTP POST but since ES 5.0 only HTTP PUT is accepted to create a new index.
This change back in september made this create call compatible with ES 5.0 again.
The only possible explanation is that you have ES 5.0 installed but you don't have the 5.0 version of the elasticsearch-php client installed.
Since you're running Elasticquent which doesn't yet support ES 5, you can temporarily go around this issue by modifying the Endpoints/Indices/Create.getMethod() method to always return PUT instead of POST and your call will work again, but you might run into other incompatibilities.

Categories