Include ZEND framework 2.3 in application without skeleton - php

There is a lot on the web about including ZEND, but I still don't get how to simply use it (without a skeleton) (In this case I only want to use Zend\Dom\Query)
I would like to:
1) Simply include the complete ZEND 2.3 library in a single application
or
2) Include a single ZEND 2.3 module
I'm using NGINX, but actually I just want to include it via PHP. Any hints, tips and or links?
What I did was:
include('./library/Zend/Dom/Query.php');
$c = curl_init($u);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($c);
$dom = new Zend\Dom\Query($html);
$results = $dom->execute('.foo .bar a');
...
Error is:
Fatal error: Class 'Zend_Dom_Query' not found in /SOMEDIR/myfile.php on line 12

Zend Dom and all of other Zend Framework 2 components developed independently from the framework's itself. All of them maintained separately and there are custom repositories exists for this components, respectively.
I think that the best way of using this components without framework's itself, using composer. We're living in open-source era and the year is 2014. All you need is making a search on packagist instead of trying to use Zend Dom (or any 3rd party library) via manually including/requiring into the codebase. Please use composer and follow the PSR standards.
To integrate the composer into your very own application, open the console and simply type:
cd /path/to/your/project
php -r "readfile('https://getcomposer.org/installer');" | php
After installation, use this command to add zend-dom dependency in your project:
php composer.php require zendframework/zend-dom:2.3.*
Now anywhere in your code, you can type new Zend\Dom\Query() to create a new dom query instance. (I'm assuming that you already read the autoloading process)

A simple way to include it via PHP is :
1. Make sure that the Zend directory is on your php include_path. Assuming Zend directory is in the library folder you'll have something like this in your index.php file:
$path= array();
$path[] = '.';
$path[] = './../library';
$path[] = get_include_path();
$path= implode(PATH_SEPARATOR,$path);
set_include_path($path);
2. Now just include Zend/Loader/StandardAutoloader.php first and then use it to load other classes. You can do this :
require_once 'Zend/Loader/StandardAutoloader.php';
$autoloader = new Zend\Loader\StandardAutoloader(array(
'fallback_autoloader' => true,
));
$autoloader->register();
With the $autoloader->register(), you don't need to include files each time you want to call classes. You have just to instanciate your classes :
$dom = new Zend\Dom\Query($html);

Related

PhpStorm auto include used PHP files

I want PhpStorm to auto-include (include_once) PHP files that I use in my respective .php file.
Simple example: MyTestClass.php:
<?php
namespace Test;
class MyTestClass
{
public function sayHello()
{
echo "hello world";
}
}
When I use this class in my index.php file, only the namespace is auto inserted:
<?php
use Test\MyTestClass;
$test = new MyTestClass();
$test->sayHello();
But this does not work in PHP. I've to include the PHP file that contains the class:
<?php
include_once('./namesapce.php');
use Test\MyTestClass;
$test = new MyTestClass();
$test->sayHello();
Is it possible that PhpStorm inserts these files automatically with the help of include_once?
Nope. PhpStorm cannot do that.
And it's extremely unlikely to be implemented.
The reason is simple: it's 2022 now and PHP has class autoloading functionality for many years (since PHP 5 which is 2004):
https://www.php.net/manual/en/language.oop5.autoload.php
https://www.php.net/manual/en/function.spl-autoload-register.php
BTW: you can use Composer autoloader for this purpose. It's fast and easy to use.
Are you using custom libraries or frameworks? If yes then quite likely you have it installed using Composer already. It's a standard tool these days for installing PHP packages (managing your dependencies) and classes autoloading.
You just need to tell where to find your classes in composer.json file and require_once the actual autoloader.
https://getcomposer.org/doc/01-basic-usage.md#autoloading

How to use this QR code library (php-qrcode-detector-decoder)

Please tell me step by step, I'm amateur on php.
visit here
How to use this code.
Please help or guide me to make this code work 100%.
require __DIR__ . "/vendor/autoload.php";
$qrcode = new QrReader('path/to_image');
$text = $qrcode->text(); //return decoded text from QR Code`
QR code decoder / reader for PHP
This is a PHP library to detect and decode QR-codes.
This is first and only QR code reader that works without extensions.
Ported from ZXing library
Installation
The recommended method of installing this library is via Composer.
Run the following command from your project root:
$ composer require khanamiryan/qrcode-detector-decoder
Usage
require __DIR__ . "/vendor/autoload.php";
$qrcode = new QrReader('path/to_image');
$text = $qrcode->text(); //return decoded text from QR Code
I'm making this an answer because it is too complex for a comment.
First of all you should really try to use this code before asking for help. But let's look at the proposed code anyway:
<?php
// This line calls the autoload script generated by Composer. This ensures that you can
// just use the library classes without having to manually include their specific files
require __DIR__ . "/vendor/autoload.php";
// This instantiates the QrReader class and points it to the path of a QR code image
$qrcode = new QrReader('path/to_image');
// This calls the text method on the above instance to determine the contents of the qr code
$text = $qrcode->text();
if this doesn't work for you then you should post the error(s) you got. If you can't see any errors then add the following right below the <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
I had a lot of trouble to get this working library without composer too.
The thing is it works on windows but not on linux because of the wrong capitalization of filenames and directories and such.
Then I found this blog that has a great solution:
https://www.mgcwebsites.co.uk/solved-class-zxinggdluminancesource-not-found/
Install a version before Composer support was added
Perhaps you use shared hosting that doesn’t allow you to install Composer or use SSH. Perhaps, like me, your server works perfectly and you want to keep it as clean as possible. Either way checking through the commits I was pleased to see Composer support is a fairly recent addition and that by downloading the last commit before Composer support was added I was able to forget about Composer and get the library working in seconds.
https://github.com/khanamiryan/php-qrcode-detector-decoder/tree/cda63b7f4a8cd84d72c41b25c74284b56dc7f2cc
It works very well now...

Installing PHP library for Supervisor using composer

I want to use Supervisor to manager my processes. I have got it installed on my Amazon linux Machine and the basic setup runs fine as per the config file.
Now, I want to change the processes dynamically. Since it needs the config file to be changed every time and a restart, using PHP library to do the same seems to be a good option.
Specifically I am going through SupervisorPHP config to change the configuration dynamically and SupervisorPHP to manager Supervisor through PHP.
Following the README for SupervisorPHP config, I got it installed via composer
composer require supervisorphp/configuration
I copied the sample code
<?php
use Supervisor\Configuration\Configuration;
use Supervisor\Configuration\Section\Supervisord;
use Supervisor\Configuration\Section\Program;
use Indigophp\Ini\Rendere;
$config = new Configuration;
$renderer = new Renderer;
$section = new Supervisord(['identifier' => 'supervisor']);
$config->addSection($section);
$section = new Program('test', ['command' => 'cat']);
$config->addSection($section);
echo $renderer->render($config->toArray());
When I run this code, I get the following error:
PHP Fatal error: Class 'Supervisor\Configuration\Configuration' not found in test.php on line 7
I also tried to clone the repo and include the files individually, however it shows error for other dependencies. It would be great if I could use this.
There are 2 mistakes in the above code.
The first mistake is that you do not use the autoloader provided by composer so that php can find the necessary classes. To do so just add require __DIR__ . '/vendor/autoload.php'; (If the vendor folder is in a different path relative to the sample script then adjust accordingly).
The second mistake is in the use statement for Indigophp. Apart from the obvious typo in the word Renderer, if you check the source of Indigo you will see that it must be use Indigo\Ini\Renderer;
So the correct code to test your installation is:
<?php
require __DIR__ . '/vendor/autoload.php';
use Supervisor\Configuration\Configuration;
use Supervisor\Configuration\Section\Supervisord;
use Supervisor\Configuration\Section\Program;
use Indigo\Ini\Renderer;
$config = new Configuration;
$renderer = new Renderer;
$section = new Supervisord(['identifier' => 'supervisor']);
$config->addSection($section);
$section = new Program('test', ['command' => 'cat']);
$config->addSection($section);
echo $renderer->render($config->toArray());
Running the above code, you should get the following output:
[supervisord]
identifier = supervisor
[program:test]
command = cat

locating dependencies on windows php extension builds

I am writing a php extension for a library. I have a generic swig file to build wrappers for the library. This has been very successful so far on Python.
A user is trying to build the library for php and I am trying to help out. I generated the code using swig, and I can build the .dll extension using Visual Studio. The problem is getting it into php. When I build php_mylib.dll, it needs to find mylib.dll and it can't.
So I am trying to build via the command line a la:
http://blog.benoitblanchon.fr/build-php-extension-on-windows/
I have put all the files to be compiled and the libraries needed (i.e. mylib64.lib and mylib64.dll) in a folder called mylib in the C:\php-src\ext folder with all the other extension folders.
The problem is I that I can't get the config.w32 file to communicate the location of mylib. Here is my config.w32 file (pretty standard -- you can see that I copied it from the curl config.w32 file):
// $Id$
// vim:ft=javascript
ARG_ENABLE("mylib", "mylib support", "no");
if (PHP_MYLIB != "no") {
if (CHECK_LIB("mylib64.lib", "mylib", PHP_MYLIB) &&
CHECK_HEADER_ADD_INCLUDE("mylib_cpp.h", "CFLAGS_MYLIB")
) {
EXTENSION("mylib", "mylib_c_wrap.cpp", true);
AC_DEFINE('HAVE_MYLIB', 1, 'Have mylib library');
// TODO: check for curl_version_info
} else {
WARNING("mylib not enabled; libraries and headers not found");
}
}
When I run buildconf and then configure --disable-all --enable-cli --enable-mylib it always shoots me the 'libraries and headers not found' warning from the script.
On Unix systems (config.m4) there appears to be a PHP_ADD_LIBRARY_WITH_PATH macro but I don't see any equivalent for windows. It seems like this is what I need.
I have also tried adding the full path to mylib into the system's path but to no avail. It seems like there might be an environment variable somewhere in the PHP build cosmos that needs to be set to find external dependencies, but I can't find any information about this.
It would also be good to do all this as a Visual Studio project -- easier for Windows users the world over. I have not seen anything on the web that looks like this.
By the way, I have successfully phpized this library using the same swig+phpize procedure on Linux (I followed this guide for the php part:http://www.sitepoint.com/install-php-extensions-source/) and it works beautifully.
In most cases you can use this info to build a PECL extension https://wiki.php.net/internals/windows/stepbystepbuild#building_pecl_extensions . Your config.w32 looks ok, though please note that that when depending on some additional library, usually it should be ARG_WITH(...) for semantics. Your lib stuff can be then put into the deps dir as in the wiki.
Another way could be passing --with-extra-libs and --with-extra-includes to configure. Those have to contain paths to *.lib and *.h dirs respectively. When using a static lib, that should be it, otherwise you'll need to place the corresponding *.dll onto the %path% for the ext to work.
Hope this helps, otherwise you can also gain some attention on the PHP mailing lists or specifically on #winphp-dev at freenode.
Thanks.

How to use composer/composer PHP classes to update individual packages

I want to use the composer/composer PHP classes to update individual plugin packages.
I do not want to use command-line solutions like exec("php composer.phar update");
I am unable to get it to work. I have tried several different options, much alike the following code.
It just returns a blank screen.
use Composer\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
$input = new ArrayInput(array('command' => 'require vendor/packkage dev-master'));
$output = new BufferedOutput();
$application = new Application();
$application->run($input, $output);
dd($output->fetch());
Things i would like to achieve:
Download/Update individual packages
Get result output to verify success
Dump autoload
Remove/require packages
A bit of context details:
I am creating a plugin updater for my PHP application (in admin panel).
Every plugin is a composer package and resides on my own Satis repository.
The plugins get installed into a custom dir using my composer plugin.
I can read composer.lock locally and packages.json on the satis server to figure out
what packages require updates.
update
I've managed to at least get it to work. The no-output issue was due to $application->setAutoExit that needed to be false before running. Next issue that i had was that the required package would download itself into the same directory as the class where i called it from. Solved that by using putenv and chdir. Result:
root/comp.php
putenv('COMPOSER_HOME=' . __DIR__ . '/vendor/bin/composer');
chdir(__DIR__);
root/workbench/sumvend/sumpack/src/PackageManager.php
include(base_path() . '/comp.php');
$input = new ArrayInput(array('command' => 'require', 'packages' => ['vend/pak dev-master']));
$output = new BufferedOutput();
$application = new Application();
$application->setAutoExit(false);
$application->run($input, $output); //, $output);
dd($output->fetch());
This works, but it's far from ideal.
The full solution to this would be pretty long winded, but I will try to get you on the right track.
php composer.phar require composer/composer dev-master
You can load the source of composer into your project vendors. You might have already done this.
The code you are looking for is at: Composer\Command\RequireCommand.
$install = Installer::create($io, $composer);
$install
->setVerbose($input->getOption('verbose'))
->setPreferSource($input->getOption('prefer-source'))
->setPreferDist($input->getOption('prefer-dist'))
->setDevMode($updateDevMode)
->setUpdate(true)
->setUpdateWhitelist(array_keys($requirements))
->setWhitelistDependencies($input->getOption('update-with-dependencies'));
;
$status = $install->run();
Most of the command relates to the reading and writing to of the composer.json file.
However the installer itself is independent of where the configuration actually came from. You could in theory store the configuration in a database.
This is the static create method for the installer:
public static function create(IOInterface $io, Composer $composer)
{
return new static(
$io,
$composer->getConfig(),
$composer->getPackage(),
$composer->getDownloadManager(),
$composer->getRepositoryManager(),
$composer->getLocker(),
$composer->getInstallationManager(),
$composer->getEventDispatcher(),
$composer->getAutoloadGenerator()
);
}
You will need to pay special attention to the Package, And implement your own.
Although your current attempt to run it on the command line will work, I do not recommend it because Composer is primarily a development and deployment utility, not an application utility.
In order to smoothly use it to assist with loading plugins on a production environment, you will need to tightly integrate its internals with your own application, not just use it on the side.
This is something I am interested in as well, and I think this has inspired me to look into it myself. So I'll let you know what I come up with, but this is the best I can advise you for now on what I consider to be the correct approach.

Categories