The files directory is:
$ cd src/lib/Rpc/
$ ls
a.php b.php c.php
Then, I write the line in composer.json file:
autoload: {
psr-4: {
Rpc\\: src/lib/Rpc/
}
}
Now I want to load the file which it will be loaded the first when load Rpc lib.
Because the file needs to set global function, the content of file:
<?php
Rpc\Chan::setDefaultVar();
So, how should I write the composer.json file?
Ok, lets do this...
a) Syntax of your composer.json file is invalid.
You can check the syntax of your composer.json file by using the command composer validate composer.json on the CLI. It will give you some details about the missing properties and the syntax in general.
You minimal composer.json file should include a name, description and a license - but validate will tell you.
I've fixed your composer.json syntax and added the missing properties - to get you started.
{
"name": "thinkerou/rpc-project",
"description": "description for my rpc-project",
"license": "MIT",
"autoload": {
"psr-4": {
"Rpc\\": "src/lib/Rpc/"
}
}
}
b) The Autoloading problem
Its a bit hard to understand your question, because of the language barrier, but
i understand it like this: you want to load one file of your library always, because it uses a static function to initialize some values, right?
I would suggest to use the files autoloading mechanism in this case. It will require certain files explicitly on every request (!). (Its often used, when your package includes PHP functions that cannot be autoloaded by PHP, but should work in this case, too.)
Example:
{
"autoload": {
"files": ["autoload_this_file_on_every_request.php"]
}
}
So, in the end the complete example with "PSR-4 autoloading" for the classes of your library and "files autoloading" for one specific file would look like this:
{
"name": "thinkerou/rpc-project",
"description": "description for my rpc-project",
"license": "MIT",
"autoload": {
"psr-4": {
"Rpc\\": "src/lib/Rpc/"
},
"files": ["src/lib/Rpc/autoload_this_file_on_every_request.php"]
}
}
Related
How to use git, when I have large tree of folders in file structure of web-project. Module, which I developing is separated by different folders in this file structure. As it is customary by MVC pattern concern.
In the overall file structure of web-project I have, roughly speaking:
model folder
controller folder
views folder
languages folder
and so on
I making changes in files at this folders and need track changes. These folders are not combined in one folder, that associated with module, that I developing. These folders are scattered in different parts of the file structure.
I could create git repository at the root of file structure and in .gitignore specify, which folders track. But I develop many modules. And I need separate git repositories for them.
Where and how create git repositories to developing many modules in large file structure?
If I init git repository at the root of web-project is it possible to create many repositories at the root of file structure for each module and for each repository specify which folders git should track?
I think simplest solution now days it's to keep your independent modules in separated repositories and then requesting them using composer.
I will show you some theoretical example of it.
I have project, which should use Payment Module. Payment Module is a separated repository with composer.json file provided in root directory.
For example:
{
"name": "company/payment-module",
"description": "Module handling payments from our customers.",
"autoload": {
"psr-4": {
"Company\\PaymentModule\\": "src/"
}
}
}
That gives you possibility of using any of modules created in any application/project you'll build.
So, for example in your project you can require Payment Module as dependency.
{
"name": "company/shop",
"type": "project",
"description": "The main repository of our shop.",
"autoload": {
"psr-4": {
"": "src/"
}
},
"repositories": [
{
"type": "git",
"url": "https://github.com/company/payment-module"
}
],
"require": {
"company/payment-module": "dev-master"
}
}
When you define modules you want in the project and install them using composer, they will be under vendor/ directory and will be autoloaded into the namespaces you define.
In my composer.lock file, I noticed that some packages have a reference value:
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/paragraphs-8.x-1.1.zip",
"reference": "8.x-1.1",
"shasum": "c678e5704a98c6a0549e415412da081cfeb03a00"
},
And some just have null:
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/redirect-8.x-1.0-alpha5.zip",
"reference": null,
"shasum": "927aa4c8d8b40b0cd2442bee86f2f386d25e53ca"
},
What does the value refer to? I thought it refers to a commit, but both of these are zip packages, where 1 has a reference and the other does not.
I have checked the code of both modules and read some articles. After a bit of research and discussing with JS developers, I came to know that 'reference' in the composer file denote the PHP library tag, branch or zip file. So for example, if I say my package reference is from "reference":"master" then this will pull the code from that repository every time I run the composer update command. Defining a reference is a way to omit the requirement of adding a composer file to the library itself. But if your library is already supporting the composer using a composer.json file within its own directory then you won't need to define the package in the composer file.
Now, let's come to both these modules. First, check the source tree of the Paragraph module at http://cgit.drupalcode.org/paragraphs/tree/?h=8.x-1.x, and you will notice that no composer.json file available there so we must need to define the reference parameter in composer file to tell the application to pick the correct source files. But on the other hand, if you see the code source tree of the redirect module at http://cgit.drupalcode.org/redirect/tree/, you will found a composer.json at the root of the file. This file will allow you to omit the reference parameter from your application composer.json file.
Also, I think if we don't define this parameter the latest will be pulled in and based on the above criteria composer.lock file gets updated upon composer install command to run.
Hope this will clear your doubts!
I'm working on a PHP library (as a composer package) that needs a XML file to work. I'm using simplexml_load_file to load the XML file, but I'm wondering where I should put the file and how to get a path that always works. The current file structure looks like this is:
/
src/
Vendor/
Library.php
tests/
LibraryTest.php
composer.json
composer.lock
My composer.json file :
{
"name": "vendor/library",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"Vendor": "src/"
}
}
}
You can't do this directly, but there are some possibilities:
Create a private repository which will contain only your XML file and add it to your composer. You'll then find it in your vendor folder. Here you can find a documentation about how to create custom repos: https://getcomposer.org/doc/articles/handling-private-packages-with-satis.md
Save it in your application, in some data folder and add to a GIT.
Add a simple checking to your application, so if the XML file hasn't been found, display message with information how to get it.
I'd recommend to mix points 2 and 3.
I'm trying to get my head around using PHP's composer. In particular, I'm trying to load this project and its dependencies.
I am installing composer in the necessary folder (root of my application), so it creates a composer.phar.
I make sure I have the correct JSON file for the project in that same directory:
{
"name": "tumblr/tumblr",
"description": "Official Tumblr PHP Client",
"keywords": ["tumblr", "api", "sdk", "gif"],
"homepage": "https://github.com/tumblr/tumblr.php",
"authors": [{
"name": "John Crepezzi",
"email": "john.crepezzi#gmail.com",
"homepage": "https://github.com/seejohnrun",
"role": "developer"
}],
"license": "Apache-2.0",
"type": "library",
"require": {
"eher/oauth": "1.0.*",
"guzzle/guzzle": ">=3.1.0,<4"
},
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"psr-0": {
"Tumblr\\API": "lib"
}
}
}
I then write this in the appropriate directory with the terminal: php composer.phar install.
However, this does not load the tumblr.php. It loads other files, such as symphony, sebastian, guzzle, etc.
Am I doing this incorrectly? Does this JSON not load the tumblr.php, but its dependencies?
Composer generates a file vendor/autoload.php. If you want to use any of the things you installed with Composer in your own code, you just need to require_once 'vendor/autoload.php' and can then simply call whatever code in whatever Composer-installed library you want; the autoloader will take care of locating and including the necessary files without you having to worry about particular directories inside the vendor folder.
The autoload entry in the composer.json file is there so any library can specify particulars of how its files should be autoloaded. You do not typically have to use that yourself in your application for anything. You may use this entry to add autoloading for your own code "for free" if you wish. However, again, you do not need to add this to use any of the installed dependencies, those should all already be configured correctly for autoloading in their respective composer.json files.
If the JSON data you show is in your OWN project, you are doing it wrong. That JSON data is from the original Tumblr project, and you shouldn't copy it into your project, because as you found out, it will not really help you using the Tumblr client.
The correct way of using Composer:
Start your project by having a main directory (probably already with files).
Run composer init to easily create the initial composer.json file.
You will be asked if you want to include dependencies. You may answer yes and add tumblr/tumblr as the dependency of your project.
Alternatively, if you already have a composer.json, you can call composer require tumblr/tumblr:~0.1.
To use the library in your code, you have to include the file vendor/autoload.php and can then create all the classes according to the documentation.
I'm using Composer for module dependency management (loving using autoload.php instead of a ton of includes and requires!).
I want to include a PHP file that is outside of the root Composer directory (for security) with credentials stored in defines().
This isn't working, composer.json:
{
"autoload": {
"classmap": ["../credentials.php"]
}
}
credentials.php:
define('RYAN','BRODIE');
test.php:
require_once __DIR__.'/../vendor/autoload.php';
echo RYAN;
Results in Notice: Use of undefined constant RYAN. If Composer's autoloader is only intended for Class includes then I'd be grateful for any hacks (as it were) to make this work.
That method should work fine, however you'll need to use files instead of classmap for example;
{
"autoload": {
"files": [ "../constants.php" ]
}
}