Cakephp 3.x. Execute custom console command from a Model - shell

Thanks in advance to all who wants to help...
I'm following the console commands guide that appears on cakephp 3.x cookbook. I have created the first example for trying to understand how it works
<?php
namespace App\Command;
use Cake\Console\Arguments;
use Cake\Console\Command;
use Cake\Console\ConsoleIo;
class HelloCommand extends Command
{
public function execute(Arguments $args, ConsoleIo $io)
{
$io->out('Hello world.');
}
}
I have saved that on src/Console, and when I execute as they said in the guide "bin/cake hello", everything is perfect, however, I want to be able to run the console command from the model. I have cheched by internet and I have tried some things but I'm not succesfull on that.
Thanks again!

Related

Using an Android library that extends AppCompatDialog

So I'm trying to create a plugin that uses PrettyDialog (https://github.com/mjn1369/PrettyDialog) using the latest NativeScript seed.
However I've run into the following error when compiling:
Error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
That happens using the following code, and calling show() (TypeScript):
export class PrettyAlert {
show() {
const alert = this.createAlert();
}
createAlert(width?: number) {
return new libs.mjn.prettydialog.PrettyDialog(app.android.context);
}
}
I've been looking into the error here (pure Android): You need to use a Theme.AppCompat theme (or descendant) with this activity
But none of the solutions have worked.
I figure it possible somehow, but I'm new a plugin building, and I'm sure there's some quirks I need to understand.
There are similar plugins - fancyalert / cfalert already if you are not very choosy about PrettyDialog.
NativeScript introduced support for AppCompatActivity from v5.x which seems just hit live. You should bypass this error if you upgrade to latest version.

laravel file_put_contents(): Exclusive locks are not supported for this stream

I tried to upload my site to laravel to host and be configured but I couldn't load the project. The error is as follows:
file_put_contents(): Exclusive locks are not supported for this stream
How do I fix this?
Try to remove the following line of code in your path:
public function put($path, $contents, $lock = false)
{
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
}
and replace it with this one below:-
public function put($path, $contents, $lock = false)
{
return file_put_contents($path, $contents, 0);
}
The problem is that the host is not supporting "exlusive locks". I had this problem with Vagrant on a Ubuntu host.
Then you can fix it like described here: https://stackoverflow.com/a/35743592/2707570
If you have the problem on a remote server you need to contact the provider and ask why exclusive locks are not supported.
I had a similar issue recently. Running the artisan command: php artisan config:cache fixed it for me
I had the same error, while using NFS to share my code from my developer machine to my Centos server. The problem seems to be that the server tried to access my NFS-shared files using NLM locking by default, so I had to disable locking in my settings. This is done by adding nolock in the NFS options in /etc/fstab
This fixed the issue for me
The problem occurs when running on a system that doesn't support exclusive file locks.
An exclusive or write lock gives a process exclusive access for writing to the specified part of the file. While a write lock is in place, no other process can lock that part of the file.
There are currently 2 places in Laravel where such locks are enforced for data security reasons: when writing cache entries and sessions. Note the third argument to Filesystem::put() is true in both these calls, which causes it to try and gain an exclusive lock before writing.
The way to resolve this issue is to get the filesystem adapter to not attempt that lock. The approach suggested in one answer is to edit Laravel's library files. This is, as the kids say, the smooth-brain approach. Your changes will be lost on upgrade so must be continually managed.
Instead, write your own custom class, override that method, and configure your application to use it.
Step 1: create app/Filesystem/Filesystem.php with these contents:
<?php
namespace App\Filesystem;
use Illuminate\Filesystem\Filesystem as FilesystemBase;
class Filesystem extends FilesystemBase
{
public function put($path, $contents, $lock = false)
{
// don't pass the value of $lock to the parent method
return parent::put($path, $contents);
}
}
Step 2: edit app/Providers/AppServiceProvider.php to register the new class. Don't remove existing registrations, if any, just add this one:
<?php
namespace App\Providers;
use App\Filesystem\Filesystem;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('files', fn () => new Filesystem);
}
}
And that's all. Confirm your provider is working by firing up php artisan tinker and entering app('files'). You should get an instance of your new class.
Follow these steps which solved the same issue when I tried to run laravel9 on my Android tablet with termux.
Go to vendor/laravel/framework/src/illuminate/filesystem.php
Then replace this code inside put method by these lines like image
Run laravel on linux
Simply Change LOCK_EX to LOCK_SH.
Go to filesystem.php inside laravel vendor
Change the put method with these lines:
public function put($path, $contents, $lock = false) { return file_put_contents($path, $contents, $lock ? LOCK_SH : 0); }
In short term LOCK_EX to LOCK_SH
You have to close the server by closing your batch window you used in starting the server initially.
Refresh your browser and make sure the the server has been closed.
Start a new development server by typing $ php artisan serve
Go to your browser and enter the server address generated by the artisan. It's usually http://127.0.0.1:8000
Best Solution for Laravel
Go to /laravel/framework/src/Illuminate/Filesystem/Filesystem.php
delete content function " public function put($path, $contents, $lock = false)" line 120.
Finish

Laravel Behat DB connection

I've been having issues with behat and laravel working together.
I configured Behat with Laravel. I have this simple scenario:
Scenario: Login as an admin
Given I add an admin to the db
When I login as an admin
Then I should be in the welcome page
In the FeatureContext Class I insert the admin user to the db using factories.
Everything works fine if I don't use the browser. But, when I start the selenium server and I add the #javascript tag to the Scenario, the test run fine, the browser start fine, but the login process fails because there is no users in the db. Yes the same user with the same credentials that I just added in the first step. Also I checked for the user by querying the db before executing the login, and the user is there.
BTW, I only have one connection, I triple-checked that.
So, what is happening here? Any Ideas?
Thanks.
I think I solved the issue or at least I found a workaround.
For some reason DB::beginTransaction() and DB::rollback() does not work properly when the browser is launched with behat and selenium. After some time of researching I came up with the next solution. Apparently, because I am using a vagrant virtual machine to launch my application, when the browser is launched by Selenium, it start a new session. Also, it uses another instance of laravel, not the same instance behat is working with. That means that the transactions I am trying to implement with behat never affect the same database the application is using with the browser. Additionally, I am using a different .env configuration file for my tests (.env.testing) where I defined all the parameters that laravel need to use for the test environment but there is no way to tell the application through the browser that it was launched by behat and selenium and to change to the test environment, instead, thw application will be using the regular environment configuration file, in my case .env (development).
Please see bellow the code I came up with:
```
/**
* #javascript
* #BeforeFeature
*/
public static function before(BeforeFeatureScope $scope)
{
putenv('APP_ENV=local');
$tableNames = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
foreach ($tableNames as $name) {
//if you don't want to truncate migrations
if ($name == 'migrations') {
continue;
}
DB::table($name)->delete();
}
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
Artisan::call('migrate');
try {
Artisan::call('db:seed');
} catch (Exception $e) {
dump('seeder failed');
}
}
/**
* #javascript
* #AfterFeature
*/
public static function after(AfterFeatureScope $scope)
{
putenv('APP_ENV=local');
Artisan::call('migrate:rollback');
$tableNames = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
foreach ($tableNames as $name) {
//if you don't want to truncate migrations
if ($name == 'migrations') {
continue;
}
DB::table($name)->delete();
//Schema::dropIfExists($name);
}
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
```
A solution to tell the application inside the vagrant machine which environment use could be a configuration in the web server inside the vagrant machine that based on a the url, loads the app with a different a environment. For example:
standar url: https://localhost:8000/auth/login
test url: https://localhost:8000/test/auth/login
In this case all the application urls will have test in it.
Hopefully this would help to somebody in the future. If anybody have a better approach, please, I will be more than welcome try it.
Cheers

Run yii2 action using windows scheduler

I want to make my action in a controller run or executed automatically because I want to send email from my yii2 project automatically. I use windows operating system, so after I search some tutorials, maybe I can use windows scheduler. But, I don't know how to configure my action controller with the windows scheduler? What must I do?
You can execute actions of controller in Command Line. So, if you can run any command in windows scheduler, then it won't be troublesome.
If you are using Yii2 Advanced Project, then you can see that it consists of two major parts: Backend and Frontend for web access. In addition it contain Console folder, which is used for calling console commands. All you need to do is to create controller in console/controllers/ directory. Example:
"MailController.php" in "console/controllers"
<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
class MailController extends Controller
{
public function actionSend() {
$mail = Yii::$app->mailer->compose('layouts/main', ['content' => 'blah-blah message'])
->setFrom('myweb#site.kz')
->setTo('myclient#gmail.com')
->setSubject('Оповещение об окончании лицензии');
if($mail->send()) {
echo 'Success';
} else {
echo 'Fail';
}
}
}
Then you can run it in console like
yii [controller]/[action]
In your case, go to you web-application directory, and just call
yii main/send
P.S sorry for my bad English, and I'm newbie in Stackoverflow

Error when including amazon library in a joomla 1.5 component

I have been creating a component that will manage some data on my amazon webservice and I would like to use the amazon library on working with it but I have been encountering an error here is the error:
Fatal error: Class 'JView' not found in C:\xampp\htdocs\joomla1\administrator\components\com_amazon\views\amazon\view.html.php on line 8
here the code coming from the view:
jimport('joomla.application.component.view');
class AmazonViewAmazon extends JView{
function display()
{
$this->setLayout('table_layout');
parent::display();
}
}
It's weird cause the error will only happen when I require the sdk library from the amazon package here is the controller code I have:
jimport('joomla.application.component.controller');
class AmazonController extends JController
{
function display()
{
require_once(JPATH_COMPONENT.DS.'lib'.DS.'amazon'.DS.'sdk.class.php');
parent::display();
}
}
If I try to comment out the require_once statement from the code above the component will load successfully, is there anyone who is able to encounter the same problem I'm encountering or anyone who has knowledge about how to resolve it? any answer is highly appreciated. :)
I impacted the same problem here. Tried to fix it with namespaces which did no help, so I switched to this solution http://undesigned.org.za/2007/10/22/amazon-s3-php-class
It's pretty simple and does the trick perfectly.

Resources