How to do project in php

Creating a Simple PHP Project with PHPUnit and Composer: A Step-by-Step Guide

The folder holds the files responsible for handling the response data of all resources. My goal is to develop a composer package that uses this API and can be utilized in various PHP projects.

How to easily generate a minimal php project with phpunit and composer?

My goal is to apply test-driven development with PHP to solve a kata. To achieve this, I prefer to use a generator or a template that is built on a basic composer project framework, integrated with phpunit.

The minimal project should contain:

  • The PHP dependency, PHPUnit, is included in the composer.json file under the «require-dev» section.
  • The folder belonging to the vendor.
  • A folder containing a sample test file and its corresponding source file.

Curious about the methods employed by other developers in PHP, I stumbled upon the following alternatives thus far:

  • Utilize PHPStorm to generate a fresh composer project using laravel/laravel as a foundation. Then, follow the instructions outlined in both this and this resource, including php artisan key:generate .
  • Follow the instructions outlined in the mentioned article to utilize composer init.
  • Implement the php-pds/skeleton approach, then refer to the guidelines mentioned in composer require phpunit/phpunit —dev .

How do PHP developers usually handle the initial boilerplate in a small PHP project?

As a point of reference, I can provide you with my approach.

Using PHP with composer and PHPUnit, I have a preconfigured skeleton consisting of a class and its respective test. Whenever I want to take on a new kata challenge, I simply duplicate the skeleton into the new kata (https://docs.github.com/en/free-pro-team@latest/github/creating-cloning-and-archiving-repositories/duplicating-a-repository), and modify the readme and class names to match the kata. My starting point for the kata is typically the master branch, and I create a new branch for each attempt.

You can refer to my repository by following the link provided. It can be used as a template for your project.

PHP Composer project standard directory layout, src/ vendor/ .git composer.json composer.lock For compatibility purposes, the Git and Composer files should sit in the root of the project. The source directory should be the root of the source files beginning with the top level namespace of your classes. The vendor directory should contain all third party …

API Consuming library — How to structure a composer package

An API from a basic service can be utilized through a composer package, which should be adaptable to PHP projects. To carry out this task, I researched and discovered the implementation of GuzzleHttp in other libraries. Nonetheless, I am uncertain about the layout of an API consuming library as it is a REST API.

The API provides access to a duo of resources, namely Customers and Products .

The methods belonging to the resource with the Products code are as follows:

  • Retrieve the inventory of products that are currently in stock using the GET method.
  • Include items by utilizing the POST method.
  • Remove item — ERASE
Читайте также:  Unit test python order

All the methods in the resources share the same Customers .

My current approach involves adhering to the psr-4 guideline for structuring, resulting in the following organization.

src/ --MyName/ ----PackageName/ ------Resources/ ------Containers/ ------Exceptions/ ------Client.php 

In a tutorial about creating a composer package, I came across src/MyName/PackageName , which is a structure that utilizes \ \ \ \ \ MyName\PackageName\ \ \ \ as its namespace.

The Client.php file is responsible for loading authorization configuration for Basic Auth and creating a new instance of \ \ \ \ \ GuzzleHttp\Client\ \ \ \ . It offers two methods for constructing requests, allowing users to set the HTTP method, URL, and additional parameters. Additionally, it features the __call() method, which creates a new object from Folder Resources . The method to be called is determined by the first element of the array passed as the second argument.

Inside the directory labeled Resources , there are two documents: Products.php and Customers.php . These files include the methods required to manage the resources mentioned earlier. Each of these classes extends the Client.php class.

The directory named Containers has data files for handling responses from all resources.

The folder labeled Exceptions is where you can find custom exception classes that may be raised during the process.

Do you think this is a viable way to create a library that is simple to maintain, or are there some key ideas that I am overlooking?

Tips for organizing a composer package

In summary, adhere to PSR-4 and establish a folder structure that is reflected in the autoloading section of your composer.json file. Beyond that, choose concise and comprehensible class and method titles for presenting the API.

Your inquiry includes a combination of various topics, with certain aspects intermingling.

When discussing the setup of your project, it’s important to distinguish between the source code’s object-oriented design, the folder layout for autoloading purposes, and the integration of composer with its own autoloading description.

Let’s go through this in order.

a) source-code

I’m uncertain about the design of a library that consumes an API. Is this a viable method for creating a library that is easy to maintain, or am I overlooking certain principles?

Is the code I have written unambiguous and comprehensible to both myself and others, such that it can be utilized in other projects?

The advises to give here are:

  • Picking clear class and method names makes use easier, like \ \ \ \ \ \ \ Company\PhotoApi\Client\.php\ \ \ \ \ \ Namespace and Class could probably expose the vendor, e.g person or company producing the source and then you could include the API name, finally the classname.
  • Follow some basic OO principles
  • How you fetch the data from the API is taste based. Going with Guzzle is ok, while going with a lighter solutions, like file_get_contents or curl , would probably work out, too. It depends.
  • Regarding the aspect of maintainability, there are two key points to consider identified as
  • The lower the number of files and the less complex your code is, the better the maintainability. Do you need an Exceptions folder? How many files are there? Why not simply stick to PHP’s default exceptions? and
  • You might also consider, that your lib has to change if the API changes, right? And, then if your lib changes, all the code in projects using your lib has to change, right? If there is only a small set of endpoints exposed by the API, then it might be better to use just one object and provide accessor methods for them, instead of using multiple objects as accessors, which might be too fine-grained. This means that projects using your API will (possibly) have fewer lines changes when upgrading. Anyway: your users will tell you, if your API lib is too difficult to use. .
Читайте также:  Python input from keyboard

While you have something like:

 $PhotoApiProducts = new Company\PhotoApi\Products; $products = $PhotoApiProducts->get(); 
 $api = new Company\PhotoApi\Client; $products = $api->getProducts(); $consumers = $api->getConsumers(); 

The arrangement of folders.

I’m following psr-4 as suggested

My recommendation for the folder layout is to adhere to the PSR-4 standard, however, you will have to determine the specific folder layout on your own. To explore various folder layouts that comply with PSR-4, you can refer to the examples section of the PSR-4 standard at http://www.php-fig.org/psr/psr-4/.

c) Integration of the composer

Upon completing your project, it is essential to include a composer.json file that describes it. The crucial section is the autoloading section, as this is where you reveal your project’s structure to Composer.

If you’ve chosen to utilize PSR-4 in your project, you can declare it in the autoloading section and specify the mapping from your namespace to the source.

To use your lib in a Composer project, the user must first load the composer autoloader during the bootstrap process. After that, they can simply use a namespaced classname from the lib and the autoloader will take care of the rest.

Package installation using composer, To install these defined dependencies to our project, we must run the composer “install” command from the terminal in our project directory in the as shown in the code snippet below: composer install. This composer install command will trigger Composer create download the specified version of …

How do I use Composer to setup google-api-php-client?

Is there any assistance available for the incomplete guidance provided?

The first step is to have a smooth way to connect to Google APIs through PHP, which should be a straightforward task.

To install the library, simply add it as a dependency to your «composer.json» by including the line «require»: . If you’re not familiar with composer or json, don’t worry, this is all you need to do.

To install Composer, which is a requirement for Step 3, Linux is needed. However, this seems to contradict the purpose of using App Engine, which is to avoid dealing with Linux. To resolve this, Debian was set up on Compute Engine and the instructions were followed by adding «sudo curl -sS https://getcomposer.org/installer | sudo php — —install-dir=/usr/local/bin —filename=composer».

Читайте также:  Javascript массив элемент объекта

In the fourth step, basic usage of Composer is required. The instructions provided on the Google web page suggest adding «require»: to Composer. However, this guidance is not very helpful.

To begin utilizing Composer in your project, you simply require a file named composer.json. This document outlines your project’s dependencies and potentially other metadata.

Is the composer.json file assumed to be common knowledge, as they fail to provide an explanation of its location or purpose?

I am looking to use the gogole-api-php-client, but unfortunately, the autoload.php file is no longer available on their website. Despite my attempts to obtain it manually, I keep encountering errors indicating that GuzzleHttp/Collection is missing. As a result, I am considering using Composer to manage the dependencies, which many claim is an easy solution. However, I am struggling to find clear and concise instructions on how to proceed without dedicating a significant amount of time to learning about json, composer, and linux. I am beginning to have doubts about whether this tool even works, as I have yet to find someone who has successfully implemented it. It would be helpful if these websites could refrain from overusing the terms «easy» and «simple,» as they do not seem to comprehend their true meanings. «Excessively complicated» and «extremely vague» would be more appropriate descriptors.

We apologize for the ambiguity and have made necessary modifications to the documentation to enhance clarity.

Composer is the standard package manager for PHP nowadays, and the aforementioned process is described in detail in the other comment.

To obtain all the necessary dependencies, check the Releases section for any package with the label google-api-php-client-[RELEASE_NAME].zip . Download and extract the file, then add the autoloader located at vendor/autoload.php . This will ensure that you have everything you need.

If you encounter any issues, please inform me.

It appears that the documentation was indicating the unstable branch (known as master) incorrectly. However, if you install the v1-master branch, it should be functional. As an illustration, the HelloAnalytics example should function with the v1-master.

git clone -b v1-master https://github.com/google/google-api-php-client.git 

To directly address the question posed in the title, suppose you desire to execute the unstable branch. In that case, you may install composer (composer.phar) onto your project folder. Afterward, execute the following command:

php composer.phar require "google/apiclient:~2.0@dev" 

Upon successful execution, a folder named «vendor» will be generated. It will comprise of autoload.php that you can easily integrate into your code for seamless operation.

require_once 'vendor/autoload.php'; 

As I am inexperienced in this matter, there may be someone else who can provide a better explanation.

How do I use Composer to setup google-api-php-client?, To answer your the question in the title more directly, if you for example want to run the unstable branch — Install composer (composer.phar) to your project folder and just run the command: php composer.phar require «google/apiclient:~2.0@dev» This should result in a folder called vendor which …

Источник

Оцените статью