To implement search functionality, a suitable tokenization tool is required. For PHP, there are few native options, so we opted for phpAnalysis.
This plugin is not hosted on GitHub, and although some user have uploaded it, they often add additional layers of abstraction. We will upload the original version to GitHub.
Step 1: Register on GitHub
Create a new repository at GitHub. Ensure the repository is public and avoid using underscores or hyphens in the name to prevent issues with reading the composer.json file.
Example: https://github.com/exampleUser/phpAnalysis
Step 2: Connect Local Project to GitHub Repository
Set up your GitHub username and email:
git config --global user.name "YourUsername"
git config --global user.email "your-email@example.com"
Generate an SSH key:
ssh-keygen -t rsa -C "your-email@example.com"
Follow the prompts, perssing Enter three times. Copy the generated SSH key from the specified location.
Navigate to GitHub settings, select SSH and GPG keys, then add a new SSH key using the copied key.
Create a new repository on GitHub and copy the SSH URL provided.
In your local repository, open Git Bash and run:
git remote add origin "copied-SSH-URL"
git pull origin master
git push -u origin master
Step 3: Submit to Packagist
Log in to Packagist using your GitHub account and submit your package by providing the GitHub URL, e.g., https://github.com/exampleUser/phpAnalysis.git.
To update your package on Packagist, use the update button.
Once submitted, you can install the package using Composer:
composer require exampleUser/phpAnalysis dev-master
Git Basics
Refer to the official Git documentation for more commands.
Creating a Composer Package
Create a composer.json file in your project directory:
{
"name": "exampleUser/phpAnalysis",
"description": "A Chinese text segmentation tool",
"type": "library",
"license": "MIT",
"require": {
"php": ">=5.6.5"
},
"autoload": {
"psr-4": {
"exampleUser\\": "./"
},
"files": []
},
"minimum-stability": "dev"
}
Include an autoloader:
<?php
namespace exampleUser;
class Loader
{
private static $rootPath = '';
public static function setRootPath($path)
{
self::$rootPath = $path;
}
public static function loadClass($className)
{
$filePath = str_replace('\\', '/', $className) . '.php';
if (strpos($className, 'exampleUser\\') === 0) {
$filePath = __DIR__ . substr($filePath, strlen('exampleUser'));
} else {
if (self::$rootPath) {
$filePath = self::$rootPath . '/' . $filePath;
} else {
$filePath = __DIR__ . '/../' . $filePath;
}
}
if (file_exists($filePath)) {
require_once $filePath;
if (class_exists($className, false)) {
return true;
}
}
return false;
}
}
spl_autoload_register([__NAMESPACE__ . '\\Loader', 'loadClass']);