I'm trying to create a single-file version of PHP framework Nette 2.4. I create a phar file using this code:
<?php
$phar = new Phar('nette.phar');
$phar->buildFromDirectory(__DIR__ . '/../vendor/nette');
$phar->setStub("<?php
require 'phar://' . __FILE__ . '/loader.php';
__HALT_COMPILER();");
$phar->compressFiles(Phar::GZ);
Everything is fine, the file is created. If I try to use this compacted version:
// bootstrap.php
<?php
require __DIR__ . '/nette.phar';
...
Error occurs:
Warning: require(phar://C:\wamp64\www\app\nette.phar/loader.php): failed to open stream: phar error: "loader.php" is not a file in phar "C:/wamp64/www/app/nette.phar" in C:\wamp64\www\app\nette.phar on line 2
So, single-file version is loaded, but there is no boot file loader.php. Does anyone know where is the mistake? Thanks for all the suggestions!
You can't create .phar from Nette downloaded via Composer. It doesn't have some necessary files (e.g. your Loader.php).
If you want to create .phar download official .zip from https://nette.org/en/download and create .phar from folder ./Nette.
__
BTW you don't need to create .phar yourself, you can find it in the .zip file in folder Nette-minified
Related
I try to run Flysystem's basic example code for the Local adapter and get a Class 'League\Flysystem\Adapter\Local' not found error. This is my process:
version check:
php -v
PHP 5.5.9-1ubuntu4.23 (cli) (built: Feb 8 2018 21:59:47)
install Flysystem:
composer require league/flysystem
output shows I'm up-to-date (this is my 2nd time running it):
Using version ^1.0 for league/flysystem
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
now there's a vendor folder in web root. And within ./web/vendor/league/flysystem/src/Adapter$ are these files:
AbstractAdapter.php
AbstractFtpAdapter.php
CanOverwriteFiles.php
Ftpd.php
Ftp.php
Local.php
NullAdapter.php
Polyfill/
SynologyFtp.php
...just showing that it seems to be installed correctly(?) I create one test file and one test directory in my web root:
fly-local.php
myfiles/
Into fly-local.php I paste the text from their docs (https://flysystem.thephpleague.com/docs/adapter/local/):
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
...and change the adapter's root folder to myfiles (is that correct?). Then I run it:
php fly-local.php
It outputs:
PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found in /[PROJECT DIR]/web/fly-local.php on line 6
PHP Stack trace:
PHP 1. {main}() /[PROJECT DIR]/web/fly-local.php:0
What am I doing wrong?
You used composer, then you need to include the composer autoload.php file.
The fly-local.php should be:
<?php
require __DIR__.'/vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
If you use a framework, you can see it includes the autoload php file for you (index.php, in general). If your test/custom file is not included to framework, you need to include the file manually.
I have a problem of building the php appliction.
Target 'build' does not exist in this project
in netbeans php project.
Just send what are all the prosedures to build a phar along with composer in netbeans IDE using ubuntu os
Netbeans has plugins, you can install NBPhar, which will help you to create .phar file.
You can create .phar file by following below steps :
Create a new PHP file named create-phar.php in your myapp root with the following code:
<?php
$srcRoot = "~/myapp/src";
$buildRoot = "~/myapp/build";
$phar = new Phar($buildRoot . "/myapp.phar",
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, "myapp.phar");
$phar["index.php"] = file_get_contents($srcRoot . "/index.php");
$phar["common.php"] = file_get_contents($srcRoot . "/common.php");
$phar->setStub($phar->createDefaultStub("index.php"));
copy($srcRoot . "/config.ini", $buildRoot . "/config.ini");
Then open a terminal window, navigate to the myapp directory and run it:
$ php create-phar.php
Refer this article Packaging Your Apps with Phar for detailed explanation.
I'm new to Composer and I'm really struggling to auto-load my classes with composer. What am I missing in the following process?
I installed the package in my PHP includes folder (which is outside the document root - I'm not sure if that matters) like this:
composer require monolog\monolog
It stated it completed successfully and I confirmed the project was added to my vendor folder.
My entire composer.json file looks like this:
{
"require": {
"monolog/monolog": "^1.22"
}
}
My entire test file looks like this:
<?php
require_once "vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
?>
And I get this error when I load the page:
Fatal error: Uncaught Error: Class 'Monolog\Logger' not found in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php:6 Stack trace: #0 {main} thrown in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php on line 6
It includes the vendor/autoload.php file without any error.
I've tried to run these commands in composer without any change:
composer update
composer dump-autoload -0
I've also tried it with different packages and I get the same error, so I'm pretty sure it has nothing to do with the monolog package.
Is there a step here I'm missing? I don't need to manually define which classes to autoload in a json file if I require them in composer, do I?
Edit 1:
As requested, here's the paths to my different files.
Path to the test page:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php
Path to the composer.json file (outside the document root but in my includes path):
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\composer.json
My vendor folder is here:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\vendor\
And inside my vendor folder I have these folders and file:
bin/
composer/
monolog/
psr/
autoload.php
You need to include autoload in your qbtest.php as following:
require_once "../wwwincludes/vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
i have downloaded phpepub via composer
then started to run the test file to understand how to use the library but it throws an error
Class 'com\grandt\EPub' not found
and then i started to view the test folder and opened the file exampletest1.php which also threw an error saying that
Class 'PHPePub\Core\Logger' not found
i'm thinking a way of working out this error for a while now checked the privileges (which is fine) also the file is also present in the folder
here is the file structure of the library
phpepub/
legacy/
src/
PHPePub/
Core/
structure/
Logger.php
.
.
.
Helpers/
tests/
demo/
EPub.Example1.php
.
.
.
composer.json
vendor/
composer/
grandt/
phpzip/
.
.
.
README.md
test.php
ReadMe.html
.
.
.
.
composer.json
You need to require the vendor/autoload.php file in each file where you're using components installed using composer.
test.php :
<?php
require_once __DIR__.'/vendor/autoload.php';
//...
test/exampletest1.php
<?php
require_once __DIR__.'/../vendor/autoload.php');
//...
See Basic Usage - Autoloading in Composer Documentation.
Usage in your projects
In the root directory of your project, add "grandt/phpepub": ">=4.0.3" to your composer dependencies and run composer install.
Let's say your project directory structure is :
project
vendor
public
index.php
composer.json
When you run composer install, Composer creates a directory vendor/ in the project root and generates an autoload file vendor/autoload.php.
To use the installed libraries in index.php, require the autoload file :
index.php :
<?php
require_once __DIR__."/../vendor/autoload.php";
//...
For a quick detailed explanation, try reading Juan Treminio - Composer Namespaces in 5 minutes
Tried to run the trans.php program from wamp server from the path
C:\wamp\www\sep24\e\trans.php
I have included the AWS folder in
C:\wamp\www\sep24\e\Amazon\
And AWS credential file in wamp/www folder as well user directory for the access
C:\wamp\www\.aws\credentials & C:\Users\username\.aws\credentials
This is my program
<?php
define('ROOT', dirname(__FILE__));
require ROOT . '/vendor/autoload.php';
use Amazon\Aws\ElasticTranscoder\ElasticTranscoderClient;
-------------
------------
// no error here.
?>
When i'm trying to run the program, I get this error
Fatal error: require(): Failed opening required 'C:\wamp\www\sep24\e/src/functions.php' (include_path='.;C:\php\pear') in C:\wamp\www\sep24\e\vendor\composer\autoload_real.php on line 54
I have included all the packages of AWS which I downloaded from the git.
What change should I make ?
There are two main problems are:
1 Composer Autoloading
The AWS dependency needs to be downloaded with Composer,
if you want the Composer Autoloader to work correctly.
Do not move folders around, when working with Composer.
The autoloading expects the files and folders inside the vendor folder.
I have included all the packages of AWS which I downloaded from the git.
You don't need to do this manually.
2 The use statement is wrong.
Change use Amazon\Aws\ElasticTranscoder\ElasticTranscoderClient;
to use \Aws\ElasticTranscoder\ElasticTranscoderClient;
3 Example Application
Because it is your third question and you seem to have problems with the application structure in connection with Composer, i will provide a simple PHP application template to demonstrate how you work with the AWS dependency.
This example provides a basic namespaced PHP application and includes the Client class from the AWS dependency(, which you have to fetched by Composer).
You find the file over here:
https://www.dropbox.com/s/q1b406thgu3146n/php-app-composer-aws.zip?dl=0
Extract the test folder into your www folder.
Then execute a composer install and run index.php.
You will end up with a error from TranscoderClient, because it expects a configuration. Not part of the problem.
Use composer.
Create testaws directory and put composer.json file with content below (you can adjust it to your needs for example PHP version or dev packages)
{
"name": "yourname/sampleapp",
"description": "Sample app",
"require": {
"php": ">=5.5.0",
"aws/aws-sdk-php" : "dev-master"
},
}
run composer install
then in index.php in testaws directory put this line in index.php
require __DIR__ . '/vendor/autoload.php';
After you do this steps it should work. More about composer you will find there
Also you can find sample project here
Delete the vendors folder and run composer install.