How should I include XML data file in a composer package? - php

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.

Related

How to use git with MVC pattern

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.

Laravel: Use github custom function non-laravel

In my Laravel Project, I want to use Github project having just 1 file.
No composer package included.
This file have some formulas to calculate the result and have 7 functions.
My Question:
Where to save this file in my project?
Or
Can I directly copy those functions in my controller/Model?
Please suggest.
Usually when I include code in laravel that is not part of a composer package I create a folder in the App folder with my nickname or companyname:
/project_folder/app/company/helperfunctions.php
You can still use composer to autoload the file if you want. Just include the classmap in your composer.json and add the path to your folder
"autoload": {
"classmap": [
"app/company"
],
},

How to set files load order with composer?

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"]
}
}

Autoloading multiple PHP projects via composer

Assume I have folder layout as
projectA
projectA\src
projectA\vendor\autoload.php
projectB
projectB\src
projectB\vendor\autoload.php
projectC
projectC\src
projectC\vendor\autoload.php
These projects need to be in the same level and they need to coexist with each others, e.g. projectA might use codes from projectB and projectC and vice vera, so they cannot be placed into the vendor folder.
The thing is: the autoload.php in each project is able to autoload their own src and vendor folder, but how to autoload for the others as well?
Assume their neighbor's project will have the folder name as the PHP namespace, is it possible to setup a autoload.php (via composer) such that in the future when I add new project folder, the autoload will magically work?
You can write in each project a composer.json with custom autoload configuration..
examples:
ProjectA:
"autoload": {
"psr-0": {
"ProjectB\\": "path/ProjectB/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
ProjectB:
"autoload": {
"psr-0": {
"ProjectA\\": "path/ProjectA/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
Composer is conceived for manage dependencies of single project.. Load more autoload.php of different projects is not a good idea..
but with this method you can create a complete autoloader for each projects

Using PHP's composer for the first time

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.

Categories