I am using the Lithium framework version 1.1.1 in PHP 5.6 with MongoDB. I have updated MongoDB from 3.4 to 3.6 and this ended up requiring the PHP ini variable mongo.long_as_object be set to true for the aggregateCursor() methods to work properly in the legacy MongoDB driver for PHP. This version of Lithium does not yet support the newer MongoDB PHP module. This causes a problem with the way NumberLong values are handled in Lithium since they are converted to a MongoInt64 in PHP.
For example: When calling $results->data() on a DocumentSet, a BSON result such as { viewers: NumberLong(12345) } will decode to [ 'viewers' => [ 'value' => '12345' ] ]. Instead I need the PHP array to be [ 'viewers' => 12345 ].
If I add an appropriate handler directly in the lithium\data\entity\Document::_init method then everything works as I expect. For example:
$this->_handlers += [
'MongoId' => function($value) { return (string) $value; },
'MongoDate' => function($value) { return $value->sec; },
'MongoInt64' => function($value) { return (int) $value->value; }
];
However, directly editing the Lithium library is likely not the best approach especially when upgrading the library to newer version as they are released. Is there a proper way to add this handler elsewhere? Such as in the Connections::add(...) method in the connections.php bootstrap file?
Unfortunately, handlers aren't directly configurable, however, they're not too hard to override. You can pass a classes key to Connections:add(), which allows you to extend one of the two classes where handlers are specified, i.e.:
Connections::add([
/* ... */,
'classes' => [
'entity' => 'my\data\Document'
// -- or --
'schema' => 'my\data\Schema'
]
]);
From there, you can implement your custom class that extends the appropriate core class, adding extra handlers as appropriate. Also, a PR to add MongoInt64 support to Li3 core would be gratefully accepted. :-)
Related
I have two databases, MYSQL and MongoDB, as MongoDB works with Documents I need a way to fetch data from MongoDB using in-URL parameters. Sensio/Framework-Extra-Bundle had this feature, but its going to be abandoned and I decided to switch on Symfony Attributes instead.
With Sensio/Framework-Extra-Bundle #[ParamConverter] that would look something like this:
#[Route('/{slug}', methods: ['GET'])]
#[ParamConverter('slug', options: ['finder' => 'CustomFinder', 'attributes' => ['id']])]
Since they switched over to Symfony Attributes and now they are using #[MapEntity] I'm not sure this is possible anymore.
I have tried to do similar with #[MapEntity] like this:
#[MapEntity(mapping: ['finder' => 'CustomFinder', 'slug' => 'id'])] MyDocument $document
But I'm getting this error:
Uncaught Error: App\Controller\MyController::get(): Argument #1 ($document) must be of type App\Document\MyDocument, string given, called in /app/vendor/symfony/http-kernel/HttpKernel.php on line 163
Try this:
#[MapEntity(mapping: ['finder' => 'CustomFinder', 'id' => 'slug'])] MyDocument $document
I have 2 simple questions overall. Im currently looking into some event handling in Laravel and would like to use RabbitMQ as my event store. Therefor i installed this package to start with: https://github.com/php-enqueue/enqueue-dev
To get started i registered it and i am able to push messages on to RabbitMQ:
$job = (new Sendemail())->onQueue('email')->onConnection('interop');
dispatch($job);
The problem however is that Laravel pushes a certain format on the queue and i can't figure out how to change that. An example message would be:
{
"job":"Illuminate\\\\Queue\\\\CallQueuedHandler#call",
"data":{
"command":"O:29:\\"Acme\\Jobs\\FooJob\\":4:{s:11:\\"fooBar\\";s:7:\\"abc-123\\";s:5:\\"queue\\";N;s:5:\\"delay\\";N;s:6:\\"\\u0000*\\u0000job\\";N;}"
}
}
So the question is, how can i change this? The main reason on this is that the consumer side is not even a PHP application which also can not interpret the PHP serialized model. Therefor im looking for a way to push a plain JSON object instead.
From the other hand i would also like to understand how you could build a custom listener? For the listener the same thing happens. Laravel tries to read the method but when i push plain JSON this will never work. Isn't there a way to register a handler on a topic and do further handling of the payload of the message within the handler itself?
There is a simple way for your purpose:
First install this package for rabbit:
vladimir-yuldashev/laravel-queue-rabbitmq
and in controller:
Queue::connection('rabbitmq')->pushRaw('{you can generate a json format here}', 'queue_name');
you can generate a json and put in this command.
There's a laravel-queue library that works with the php-enqueue library you linked to make it compatible with Laravel's built in queue system that Florian mentioned.
By default, it will still use a serialized object, but I think that can be overridden. If you look in Queue.php, createObjectPayload() on line 130 in the core Laravel Framework, that's where the job is being serialized.
If you extend the Queue class in the laravel-queue library, you should be able to change createObjectPayload to look something like this:
protected function createObjectPayload($job, $queue)
{
$payload = $this->withCreatePayloadHooks($queue, [
'displayName' => $this->getDisplayName($job),
'job' => 'Illuminate\Queue\CallQueuedHandler#call',
'maxTries' => $job->tries ?? null,
'timeout' => $job->timeout ?? null,
'timeoutAt' => $this->getJobExpiration($job),
'data' => [
'commandName' => $job,
'command' => $job,
],
]);
return array_merge($payload, [
'data' => [
'commandName' => get_class($job),
'command' => json_encode(clone $job),
],
]);
}
That should JSON encode the job data instead of serializing it. You may even be able to remove the encoding altogether, as I think it's already JSON encoded somewhere up the chain.
I'm fairly new to Mongo and I have what I thought was a simple question. How do I do MapReduce with PHP and the non legacy MongoDB driver http://php.net/manual/en/set.mongodb.php or the higher level package mongodb/mongodb found at https://packagist.org/packages/mongodb/mongodb?
Every example I've seen seems to use the legacy driver (http://php.net/manual/en/book.mongo.php). They all use the MongoCode object, which doesn't exist in mongodb.php. It exists in mongo.php (the legacy driver). When I try and use it, it will say that "Class 'MongoCode' not found".
My code looks something like:
$function = "function() { emit(this); }";
$map = new \MongoCode($function);
$command = $db->command([
"mapreduce" => "db.archiveData",
"map" => $map,
"query" => $query,
"out" => "data"
]);
To make things more confusing, when I look at the source at https://github.com/mongodb/mongo-php-library, there is a unit test for MapReduce (https://github.com/mongodb/mongo-php-library/blob/4dc36f6231df133a57ff0dc5a0123945133d25ba/tests/Operation/MapReduceFunctionalTest.php). But it uses the MongoDB\Operation\MapReduce, which doesn't seem to exist in the 1.1 version of mongodb/mongodb.
I thought maybe I would call it on the server using JavaScript. But when I look at http://php.net/manual/en/mongodb.execute.php, it says it "is deprecated in MongoDB 3.0+". So that doesn't feel like something I should use.
So is it that:
MapReduce is not supported with mongodb/mongodb. Or maybe it is not supported yet, but will be?
I have to use the legacy driver for MapReduce?
I have to figure out a way to call db.collection.mapReduce via JavaScript on the server?
I have to use the Aggregation Pipeline (https://docs.mongodb.com/manual/aggregation/) to do map reduce type of actions? But that feels much more limited.
What am I missing?
So I now have clarity on where things are at.
MapReduce will be officially supported in 1.2.0 of PHPLib (https://jira.mongodb.org/browse/PHPLIB-53)
Until then, there is a completely usable workaround by using the command object as per https://docs.mongodb.com/php-library/current/upgrade/#mapreduce-command-helper
Example is here as well:
$database = (new MongoDB\Client)->selectDatabase('db_name');
$cursor = $database->command([
'mapReduce' => 'collection_name',
'map' => new MongoDB\BSON\Javascript('...'),
'reduce' => new MongoDB\BSON\Javascript('...'),
'out' => 'output_collection_name',
]);
$resultDocument = $cursor->toArray()[0];
You can also use MapReduce via Doctrine (http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/map-reduce.html), but that is using legacy and a shim. So probably not a good choice for a new project.
foo_constants.php or fooConstants.php?
It seems laravel would do some name conversion when you use Config::get('...'), which one do you use?
foo.php
Why specify constants at all? Convention I've generally seen is single word filenames. I think in general most 'config' type settings will be constant in an environment even if it is variable between environments.
Take a look at the aws/aws-sdk-php-laravel composer package as an example. That file is named config.php in the package, but gets published to aws.php.
rydurham/Sentinel is another popular package. It also only has a single-word filename.
Update
In the situation you describe in your comment, I would do something like this:
<?php // File: foo.php
return [
'sheep' => [
'clothing' => 'wool',
'chews_on' => 'cud',
],
'wolf' => [
'clothing' => 'fur',
'chews_on' => 'sheep',
],
];
And you can access both of those via Config::get('foo.sheep') and Config::get('foo.wolf'), respectively. When they're defined on the server, they're still 'on the server' so to speak. If you wish to release the values stored in foo.sheep to the public you can, and you can do so without also exposing foo.wolf.
It should be "trivial", but after some chating on #laravel irc channel, I found it may be impossible for now. But I'll ask it here before doing it the ugly-go-horse way just to have the project done. If it's indeed impossible by current means, I'll fill a request on github (after handing over the project to my client).
I'm using Zizaco\Confide to handle authentication in my service. It uses Laravel Lang everywhere to get strings in one of the 8 bundled languages of the package. But I need to override some of those strings, and I don't want to modify the package files (which would defeat the whole purpose of Composer). How can I do this?
For example, I needed to modify confide::confide.alerts.wrong_credentials for pt_BR language. What I tried so far:
/app/lang/pt_BR/confide.php file, with contents return array('alerts' => array('wrong_credentials' => '...')). It works for Lang::get('confide.alerts.wrong_credentials') but not for the namespaced Lang::get('confide::confide.alerts.wrong_credentials')
/app/lang/pt_BR/packages/zizaco/confide/confide.php with return array('alerts' => ......)
/app/lang/pt_BR/packages/zizaco/confide/confide/alerts.php with return array('wrong_credentials' => ...)
/app/lang/packages/zizaco/confide/pt_BR/confide.php with array('alerts' => array('wrong_credentials' => '...')) - /app/lang/packages/zizaco/confide/pt_BR/confide/alerts.php with return array('wrong_credentials' => ...)
Any clue on what am I missing? Or does Laravel4 really lacks this feature?
Thanks in advance!
actually it fixed in Laravel 4.1 core
you can now overwrite it by doing
app/lang/packages/(locale)/confide/confide.php
check this
laravel 4 language issue
correct path for overriding package languages
So, as for today, Laravel really does lack this feature.
I've asked for it creating a issue on github.
Meanwhile, this functionality could be achieved seamlessly using crynobone's Orchestra Platform 2 Translation Component, which can be found here
All you need to do is require it in composer.json
{
"require": {
"orchestra/translation": "2.0.*"
}
}
and replace the original translation package ('Illuminate\Translation\TranslationServiceProvider') in /config/app.php
'providers' => array(
//'Illuminate\Translation\TranslationServiceProvider',
// ...
'Orchestra\Translation\TranslationServiceProvider',
),
That's it! Now, having app/lang/en/packages/confide/confide.php will do it!
(please note that the path should be /packages/packagename/, not /packages/vendor/packagename/
It really saved me from a big headache, hope others find this useful too.