Debug CController class in Yii1 app with Xdebug - php

I have a Yii1 application and I am trying to debug the application with Xdebug.
The part that I want to debug is a controller action:
<?php
namespace Scrape\controllers;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201809\cm\CampaignService;
use Google\AdsApi\AdWords\v201809\cm\OrderBy;
use Google\AdsApi\AdWords\v201809\cm\Paging;
use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use GuzzleHttp\Client;
use Scrape\domain\creator\IgnoreCreator;
use Scrape\domain\creator\MonitorCreator;
use Scrape\domain\creator\ProjectCreator;
use Scrape\domain\creator\TaskCreator;
use Scrape\domain\deleter\IgnoreDeleter;
use Scrape\domain\deleter\ProjectDeleter;
use Scrape\domain\deleter\ResultDeleter;
use Scrape\domain\deleter\TaskDeleter;
use Scrape\domain\Downloader;
use Scrape\domain\IgnoreFilter;
use Scrape\domain\repository\IgnoreRepository;
use Scrape\domain\repository\ProjectRepository;
use Scrape\domain\repository\ResultRepository;
use Scrape\domain\repository\TaskRepository;
use Scrape\domain\Google;
use Scrape\domain\TaskResetter;
use Scrape\domain\TaskSuggestionRetriever;
use Scrape\models\Ignore;
use Scrape\models\Monitor;
use Scrape\models\Project;
use Scrape\models\Query;
use Scrape\models\Result;
use Scrape\models\Task;
use Scrape\models\TaskIntention;
use Scrape\models\TaskKeywordForm;
use Scrape\models\TaskPopularity;
use Scrape\models\TaskStatus;
use User\models\UserType;
use function GuzzleHttp\Psr7\str;
class DefaultController extends \CController
{
public function actionDownloadTask($id)
{
(new Downloader)->downloadByTask(TaskRepository::findOne($id));
}
public function actionDownloadProject($id)
{
(new Downloader)->downloadByProject(ProjectRepository::findOne($id));
}
}
But I get this error:
Exception has occurred.
Fatal error: Uncaught Error: Class "CController" not found in C:\xampp\htdocs\webScraper\protected\modules\scrape\controllers\DefaultController.php:44
Stack trace:
#0 {main}
thrown
On the class definition.
So my question is: how to tackle this inconvenience?
This is my index.php file:
<?php
use application\components\WebApplication;
define('DIR_ROOT',getcwd().DIRECTORY_SEPARATOR);
$autoload = DIR_ROOT . 'protected/vendor/autoload.php';
$config = DIR_ROOT . 'protected/config/main.php';
$yii = DIR_ROOT . 'protected/components/Yii.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
require_once($autoload);
require_once($yii);
include DIR_ROOT . 'protected/components/WebApplication.php';
(new WebApplication($config))->run();
As you can see I have $autoload defined in the index.php file. Or do you mean something else?

Related

Fatal error in Library 'require' and 'use' PHP

I'm trying to implement https://github.com/zytzagoo/smtp-validate-email for email validation, but I have a problem in the sintallation of the package (manually, I'm not using composer)
So here is the hierarchy of my files:
My index has this code:
<?php
require 'Library/Validator.php';
use \Library\Validator;
$email = 'someone#example.org';
$sender = 'sender#example.org';
$validator = new Validator($email, $sender);
$results = $validator->validate();
var_dump($results);
$log = $validator->getLog();
var_dump($log);
?>
And the 'Validator.php' has 1000 lines of code but here are the first 25 lines so you can have an idea:
<?php
namespace SMTPValidateEmail;
use \SMTPValidateEmail\Exceptions\Exception as Exception;
use \SMTPValidateEmail\Exceptions\Timeout as TimeoutException;
use \SMTPValidateEmail\Exceptions\NoTimeout as NoTimeoutException;
use \SMTPValidateEmail\Exceptions\NoConnection as NoConnectionException;
use \SMTPValidateEmail\Exceptions\UnexpectedResponse as UnexpectedResponseException;
use \SMTPValidateEmail\Exceptions\NoHelo as NoHeloException;
use \SMTPValidateEmail\Exceptions\NoMailFrom as NoMailFromException;
use \SMTPValidateEmail\Exceptions\NoResponse as NoResponseException;
use \SMTPValidateEmail\Exceptions\SendFailed as SendFailedException;
class Validator
{
public $log = [];
/**
* Print stuff as it happens or not
*
* #var bool
*/
public $debug = false;
So, the output of the 'index.php' is this:
Fatal error: Uncaught Error: Class "Library\Validator" not found in C:\xampp\htdocs\EmailValidation\index.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\EmailValidation\index.php on line 9
use namespace name and class name to call it.
use \SMTPValidateEmail\Validator;
example
namespace Helpers
and the CheckUser class
then the text becomes
use Helpers/CheckUser;

Converting files using PHP LibreOffice and the ncjoes/office-converter library

I am using ampps as a windows 10 apache server, my php version is 7.3. I downloaded it from the LibreOffice download page and installed it on my computer. Then I installed this library via composer https://github.com/ncjoes/office-converter. I try as in the example given, but it does not convert and gives an error. I would be very grateful if you could help me where I am wrong. Here is my code sample and the error I encountered:
<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';
use NcJoes\OfficeConverter\OfficeConverter;
use PHPUnit\Framework\TestCase;
class OfficeConverterTest extends TestCase
{
/**
* #var OfficeConverter $converter
*/
private $converter;
private $outDir;
public function setUp()
{
parent::setUp();
$DS = DIRECTORY_SEPARATOR;
$file = __DIR__."{$DS}sources{$DS}test.docx";
$this->outDir = __DIR__."{$DS}results";
$this->converter = new OfficeConverter($file, $this->outDir);
}
public function testDocxToPdfConversion()
{
$output = $this->converter->convertTo('result.pdf');
$this->assertFileExists($output);
}
public function testDocxToHtmlConversion()
{
$output = $this->converter->convertTo('result.html');
$this->assertFileExists($output);
}
}
$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();
?>
Fatal error: Uncaught Error: Call to a member function convertTo() on null in C:\Program Files\Ampps\www\converter\converter.php:29 Stack trace: #0 C:\Program Files\Ampps\www\converter\converter.php(43): OfficeConverterTest->testDocxToPdfConversion() #1 {main} thrown in C:\Program Files\Ampps\www\converter\converter.php on line 29
When running tests, we are supposed to leave running them to phpunit, but you are trying to call manually (in last 2 lines of your code).
But if you insist on calling manually, change:
$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();
Into:
$donustur = new OfficeConverterTest();
$donustur->setUp();
$donustur->testDocxToPdfConversion();
So that you call setUp() which phpunit would normally call for you automatically.
See also:
How do I run all my PHPUnit tests?
How to run single test method with phpunit?
Example for page
If you want to use this logic on a Web-Page, it should look something like:
<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';
use NcJoes\OfficeConverter\OfficeConverter;
echo 'Converting...<br>';
$input = __DIR__ . '/test.docx';
$converter = new OfficeConverter($input, __DIR__.'/results');
$output = $converter->convertTo('result.pdf');
echo 'Saved at:' . $output . '<br>';

Why the "use" command in PHP not working with include?

I have this script:
<?php
error_reporting(E_ALL);
require 'vendor/autoload.php';
use Church\Config;
use Church\SQLiteConnection;
use Church\Template;
use Church\User;
$ERROR = "";
if(isset($_POST['username']) && isset($_POST['username']))
{
$login = new User((new SQLiteConnection())->connect());
if($login->loginUser($_POST['username'], $_POST['password']))
{
}
else {
$ERROR = "login";
}
}
if(isset($_GET['go']))
{
}
else {
if (!file_exists(Config::PATH_TO_SQLITE_FILE)) {
include('init/install.php');
}
}
When I make it with this part:
include('init/install.php');
instead of this:
$tpl = new Template('templates/install.tpl');
$tpl->set('HEADER', $tpl->getFile('templates/header.tpl'));
$tpl->set('FOOTER', $tpl->getFile('templates/footer.tpl'));
$tpl->set('APP_NAME', Config::APP_NAME);
$tpl->set('APP_VERSION', Config::APP_VERSION);
$tpl->set('BASE_URL', $_SERVER['PHP_SELF']);
$tpl->render();
I get this error:
Fatal error: Uncaught Error: Class 'Template' not found in
I do not understand why it is working without include but with include is the auto loading not working. What did I miss?
Take a look at the docs here: https://www.php.net/manual/en/language.namespaces.importing.php
The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped. The following example will show an illegal use of the use keyword:
And then:
Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.
The conclusion is that you can't do what you want to do. You need to import (aka use) in your included file too.

Elephant.io integration php

Using only a simple file I can get elephant.io to work, but If I'm trying to integrate it inside an class, it won't work.
The reason why it won't work is that it says I can't use use elephant ... inside a class or function.
How would I integrate elephant.io in an already existing class ?
To be more specific , I'm trying to integrate elephant.io into codeigniter framework.
function tester() {
use ElephantIO\Client,
ElephantIO\Engine\SocketIO\Version1X;
require __DIR__ . '/vendor/autoload.php';
$client = new Client(new Version1X('http://www.textbasedmafiagame.com:8080'));
$client->initialize();
$client->emit('broadcast2', ['foo' => 'utførte et biltyveri']);
$client->close();
}
and get no response or outcome, but i do get:
use not allowed inside a function
and
Parse error: syntax error, unexpected 'use'
As the error states, the use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations.
Try something along these lines
<?php
use ElephantIO\Client, ElephantIO\Engine\SocketIO\Version1X;
require __DIR__ . '/vendor/autoload.php';
class foo{
public function tester() {
$client = new Client(new Version1X('http://www.textbasedmafiagame.com:8080'));
$client->initialize();
$client->emit('broadcast2', ['foo' => 'utførte et biltyveri']);
$ client->close();
}
}

Use parse.com in php

I am trying to use Parse in a php script.
I have done to following:
Uploaded the files 'autoload.php' and the folder 'Parse' from here to the root directory of the server
https://github.com/parseplatform/parse-php-sdk
Then I created an index.php with the following test code from here: https://www.webniraj.com/2014/08/05/parse-com-using-the-official-parse-php-sdk-v1-0-x/
// define location of Parse PHP SDK, e.g. location in "Parse" folder
// Defaults to ./Parse/ folder. Add trailing slash
define( 'PARSE_SDK_DIR', './Parse/' );
// include Parse SDK autoloader
require_once( 'autoload.php' );
// Add the "use" declarations where you'll be using the classes
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseUser;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
// Init parse: app_id, rest_key, master_key
ParseClient::initialize('xxx', 'xxx', 'xxx');
// save something to class TestObject
$testObject = ParseObject::create("TestObject");
$testObject->set("foo", "bar");
$testObject->save();
// get the object ID
echo $testObject->getObjectId();
echo '<h1>Users</h1>';
// get the first 10 users from built-in User class
$query = new ParseQuery("_User");
$query->limit(10);
$results = $query->find();
foreach ( $results as $result ) {
// echo user Usernames
echo $result->get('username') . '<br/>';
}
Of course I replaced the xxx with my app_id, rest_key and master_key
When I now open index.php I am getting
Fatal error: Call to undefined function Parse\curl_init() in /Parse/ParseClient.php on line 304
Did I miss to do something?
I don't have enough reputation points to simply comment, but wanted to suggest you confirm that you have the curl PHP extension installed on your server.
phpinfo();
or
if (extension_loaded("curl"))
{
echo "cURL extension is loaded<br>";
}
else
{
echo "cURL extension is not available<br>";
}

Categories