Friday, 7 May 2021

PHP composer

How to install, remove and update one library using PHP composer for an existing project

You have a existing PHP project and you have installed lots of libraries using composer. There are composer.json and composer.lock already.

use phpunit library as example

1. To install new one (--dev flag is used to put this library in require-dev section of compose.json)

composer require --dev phpunit/phpunit ^8

2. To remove old one

compose remove phpunit/phpunit

3. To update one

//without depency update
composer require --dev phpunit/phpunit ^9

We can check phpunit version

// go to vendor/bin
./phpunit --version
//with depency update
./composer.phar -W require guzzlehttp/guzzle 7.4.5

4. To install or update for all libraries

Davey Shafik has a good explanation. Composer: It’s All About the Lock File

Version in compose.json

Tilde Version Range (~) - ~1.2.3 is equivalent to >=1.2.3 <1.3.0
Caret Version Range (^) - ^1.2.3 is equivalent to >=1.2.3 <2.0.0

Search packages

Search Packages

Composer detected issues in your platform

Once I got Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 7.3.0". You are running 7.2.24-0ubuntu0.18.04.9.

some libraries have platform requirement. In the vendor composer directory, there is a file called platform_check.php. This file will be updated when you install some libraies.

use this flag --ignore-platform-reqs when do compose install to avoid update that file. Also, composer will not check php version when install. Be sure you know what you are doing.

 composer install --ignore-platform-reqs
./composer.phar require --ignore-platform-reqs -W  google/cloud ^0.257

Another option is set a flag in the compose.json. Then that file will not be generated.

"config": {
        "platform": {
            "php": "8.0.22"
        },
        "platform-check": false
    },

Clear composer cache

After a new libray is installed using composer, system seems not find the library. It may be because of cache. Can clear cache by the command below

composer clearcache

See composer tree

composer show --tree

dump-autoload

If new classess are added to classmap package, need to run the command below to update autoload. It will update vendor/composer/autoload_classmap.php

composer dump-autoload

A use case is with PHP Propel 2. In compose.json, we need to add classmap directory.

autoload": {
    "classmap": ["src/lib/Propel/generated-classes/"]}

After generated class using propel model:build, we need to run composer dump-autoload. Otherwise, may not be able to auto load these generated classes.

Propel class inherit debug for class not found

step one: om
step two: convert-conf

Lock file prevent our project from updating library

I want to update aws sdk for PHP to the latest version. Because there is a lock file, I can not remove the old version of aws sdk for PHP. If I remove the lock file first and do a composer install,I can install the latest version of aws PHP sdk. However, some functions of our project do not work. The following is how I solved this problem.

  • Find root cause is that we need "revenuewire/dynamodb-orm": "2.0.5" to make our project work
  • Currently the compose.json has this: "revenuewire/dynamodb-orm": "^2.0". If install without the lock file, it will install revenuewire/dynamodb-orm": "2.0.6" and will break the project
  • Modify compose.json. Change "revenuewire/dynamodb-orm": "^2.0" to "revenuewire/dynamodb-orm": "2.0.5". That means fix the version for revenuewire/dynamodb-orm
  • remove the compose.lock
  • compose install. Everything are working and aws PHP sdk is upgraded.

Show version of the package installed

composer show aws/aws-sdk-php

Thursday, 5 November 2020

Run Jenkins on Ubuntu

To start:

sudo service jenkins start

To stop:

sudo service jenkins stop

To restart:

sudo service jenkins restart

To check status:

sudo systemctl status jenkins 

To access: localhost:8080

use Pipe line to build git branch

  1. create a multiple scan pipe line item in Jenkins (branch sources in local format: file:///home/leo2019/play_jenkins)
  2. global tool config to add git excutable
  3. create a Jenkinsfile in the git repository

Find plugins installed

  1. go to http://localhost:8080/script
  2. past the groovy script below
  3. run the script
Jenkins.instance.pluginManager.plugins.each{
  plugin -> 
    println ("${plugin.getDisplayName()} (${plugin.getShortName()}): ${plugin.getVersion()}")
}

Install Jenkins on Docker

  1. stop jenkin if other jenkin is running
  2. Go to home directory
    mkdir jenkins_home
  3. //let folder own by jenkin. If log into container
     //and issue id command, got:  
     //uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins)
      cd jenkins_home
      sudo chown -R 1000:1000 .
  4. docker run -d -p 49001:8080 -v ~/jenkins_home:/var/jenkins_home --name jenkins jenkins/jenkins
  5. http://localhost:49001
  6. user name: admin password in: /var/lib/jenkins/secrets/initialAdminPassword

To Start container

docker container start jenkins

To Stop container

docker container stop jenkins

Thursday, 12 September 2013

Struct and pointer sample

#include <stdio.h>

typedef struct Table Table;

struct Table {
        int Height;
        int  Width;
};

Table *  create_table(int height, int width)
{
    Table *mytable_ptr;

    Table my  = {height, width};

    mytable_ptr = &my;

    return mytable_ptr;

}



int main()
{
        Table  * my;
        my = create_table(10, 20);

       int height = my->Height;

       printf("h%d\n", height);
}

Thursday, 6 June 2013

Enviorment variables

1. View all environment variables
export -p

2. View individual variables
echo $variable_name

3. Set variable dynamically
export APPLICATION_ENV='local'

4. Set variable permanently
put export statement in .profile

Wednesday, 27 March 2013

More MySql tips

dump db

mysqldump --host='127.0.0.1' --user=root --password='passwd' --column-statistics=0  db_name>dw.sql

redirect result to a csv file

select * from usermanager_users
 into outfile '/tmp/test.csv' fields terminated by ',' enclosed by '"' 
 lines terminated by '\n';

redirect result on command line

mysql -u [username ] -p[password] [db_name] -e " SELECT * FROM [tbl_name] " > file_name

Case sensitive compare

#Will match first name Mary, but not match mary
SELECT first_name FROM  `name` WHERE first_name  REGEXP BINARY 'Mary';

Show stored procedures

SHOW PROCEDURE STATUS WHERE Db = 'db_name'\G

# show details of procedure
SHOW CREATE PROCEDURE procedure_name\G

Create a mysql user

CREATE USER lshen@'10.18.276.170' IDENTIFIED   BY 'eikdkdk123scx';

select * from mysql.user where User="lshen"\G

GRANT SELECT  ON *.* TO lshen@'10.18.276.170';

FLUSH PRIVILEGES;

Query Cache

If swap between different MySQL versions, cache may cause problem. For example, they may use the different end character. Therefore, need to flush cache.

#To check whether the query cache is present in your MySQL server
mysql> SHOW VARIABLES LIKE 'have_query_cache';

#To monitor query cache performance
mysql> SHOW STATUS LIKE 'Qcache%';

#removes all query results from the query cache
mysql> RESET QUERY CACHE ;

Slove authentication method unknown to the client issue

Error message: Unable to open PDO connection [wrapped: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client.

root cause:This error is coming from MySQL 8.x’s new default auth‐plugin (caching_sha2_password) which older PHP/MySQL clients (PDO_MYSQL via mysqlnd) don’t yet understand.

source database server is using Mysql8.4. data warehouse is using php7.3

Solution: upgrade data warehouse to use PHP 8.0

<       apk add bash curl php7-mysqlnd php7-fileinfo php7-soap php7-pdo_mysql php7-xmlwriter mysql-client \
<        php7-dom php7-tokenizer msmtp gnu-libiconv ncurses util-linux && \
---
>        apk add bash curl php8-mysqlnd php8-fileinfo php8-soap php8-pdo_mysql php8-xmlwriter mysql-client mariadb-connector-c \
>        php8-dom php8-tokenizer msmtp gnu-libiconv ncurses util-linux && \

Friday, 14 December 2012

PHP cheating sheet

Cope File to Remote Server Using PHP

1. install php ssh2 extension

in ubuntu

apt-get install libssh2-1-dev libssh2-php

 test if php ssh2 installed

file_exists('ssh2_connect')

2. find finger print of remote server
 connect to the server
 ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub
the second part is fingerprint

3. using ssh2 to scp files

$conn = ssh2_connect('remote-server-name', 22);

$fingerprint = ssh2_fingerprint($connection,
SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);

if ($fingerprint != $knowprint) {
    die("middle man found");
}
ssh2_auth_password($conn, 'username', 'password');

ssh2_scp_send($conn, 'file_name_in_local, 'file_name_in_remote', 0777);

Use call_user_func function

class Hello {
    public static function say_hello()
    {
        echo "Hello World!\n";
    }
}

//There are three ways to use call_user_func

//seperate class name and function
call_user_func(array("Hello", 'say_hello'));

//combine class name and function into one string
call_user_func('Hello::say_hello');

//create a object
call_user_func(array(new Hello(), 'say_hello'));

PHP extension

what extensions have been loaded

php -m 

In Alpine, extension is here /usr/lib/php/modules

Thursday, 6 December 2012

Linux Commands

Limit resource a user can access

edit file /etc/security/limits.conf

//show limits
ulimit -a

See differences between two folders

diff -rq folder1 folder2

Find file in mac or Linux

find ./ -name platform_check.php

Open image

open test.png
open test.jpg

curl

//curl to redirect content to file
curl  --output leo.jpg https://checkout.whahaha-content.com/images/unionpay.png

created nested directories

The -p flag will create nested directories if directories do not exist. If exist, do nothing.

//mkdir: leo: No such file or directory
mkdir leo/test

//this works
mkdir -p leo/test

sed command

replace all occurences of a pattern

//ie flag will replace in place and that file will be modified
sed  -ie 's/unix/linux/g' my.txt

//all leo in my.txt will be replaced by developer. Also will make a backup file called my.txt.bak
sed -i.bak "s/leo/developer/g" my.txt

find which process is using a port number in mac

lsof -nP -iTCP -sTCP:LISTEN | grep <port-number>

In mac count items one level below current directory

find . -mindepth 1 -maxdepth 1 | wc -l

Saturday, 1 September 2012

Configure dual monitor

In Ubuntu 12.04.1 LTS

Click Dash home, and type "display". Launch the display configure program.

In Window 7

Press window command key+p to launch the display configure program.

PhpStorm

Find usage for a function in the project

Select a symbol for which you want to find usages, right-click the symbol, and select Find Usages from its context menu.

Find functions in a file
View | Tool Windows | Structure
Variable type hint
/** @var RouteCollectorProxy $app */
    public static $app;

Tuesday, 14 August 2012

SVN Tip

SVN Patch

Run command in project root

1. Create a patch
svn diff > mypatch.diff

2. apply a patch
download patch

patch -p0 -i fix_path.diff

3. reverse patch
 patch -p0 -R -i fix_path.diff

4.  How to revert svn commit:
The following two commands will do the trick

svn merge -r 101:100 http://svn.example.com/repos/calc/trunk
svn ci



5. SVN Blame

svn blame svn://repositoy/filename
                       

Thursday, 2 August 2012

Set up localhost server

  1. npm init
  2. npm install express
  3. create a index.html and index.js
  4. node index.js
  5. http://localhost:5000
//index.js
const express = require('express');
const app = express();
const port = 5000;

app.get('/', (req, res) => {
    res.sendFile('index.html', {root: __dirname});
});

app.listen(port, () => {
    console.log(`Now listening on port ${port}`);
});

Create a post api using express

const express = require('express');
const app = express();

//middleware to parse incoming request with json payload
app.use(express.json());

//middleware to parse incoming request with url encoded payload
app.use(express.urlencoded({ extended: true }));

const port = 5000;

app.get('/', (req, res) => {
    res.sendFile('index.html', { root: __dirname });
});

app.listen(port, () => {
    console.log(`Now listening on port ${port}`);
});

app.post('/api/create_transaction', (req, res) => {
    const body = req.body;
    console.log("body:", body);
    res.status(201).json([]);
});

Sunday, 29 July 2012

Change wireless password

1. connect a computer with router by cable
2. go to http://192.168.1.254
3. log in as admin
4. go to wireless ->security
5. change password

Wednesday, 21 December 2011

Propel Memo

Set up project

  1. create database scheme xml from an existing database > propel-gen reverse
  2. create model classes > propel-gen om
  3. create config file > propel-gen convert-conf

Update Database

After database scheme XML is modified, we need to update database. Also need to generate model class again and running time conf

  1. ../Propel/generator/bin/propel-gen sql
  2. ../Propel/generator/bin/propel-gen insert-sql
  3. ../Propel/generator/bin/propel-gen om
  4. ../Propel/generator/bin/propel-gen convert-conf

Tuesday, 20 December 2011

MySql tips

Connect

#enter password manually
mysql --host='10.22.999.162' --user=leo --password

#password in command
mysql -h 10.49.41.213 -u leo --password='mysecret'
Show comments

1. Find table structure
> show columns from book;
> desc book;

get more details
> show create table book;

2 . Find table index
> show index from book;

3. Find permission of user
>  show grants;

show grants for 'cs_user'@'10.32.%.%' 
4. Find stored procedures
>  show procedure status

5. Find triggers
>  show triggers

6. Find more shows

More show commands

> help show;

Date time functions

 >  select date_format(convert_tz(from_unixtime(1370132292), "UTC", "America/Vancouver"), "%Y %m %d");

Stored Procedure

1. create procedure
drop procedure if exists countbook;

DELIMITER //

create procedure countbook()
BEGIN
    select count(*)
    from book;
END //

DELIMITER ;


2. call procedure
> call countbook() 

3. more notes
Stored procedure can take parameters. Parameters can be inputs or outputs. Inside procedure, can declare function (see ben forta MySql crash course)


More Store Procedure

 //find procedure
show procedure status where db="myDW"\G

//show procedure details
show create procedure load_dim_date\G

//flip that from security_type=definer to the invoker
ALTER PROCEDURE update_dim_date SQL SECURITY INVOKER;

More show process list

show full processlist
# 1234 is process id coming from show processlist
kill 1234

Find mysql server hostname

show variables where Variable_name = 'hostname';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| hostname      | ip-10-32-210-21.corp.leotest.com |
+---------------+--------------------------------------+
1 row in set (0.09 sec)

Find mysql users

select * from mysql.user \G

Find mysql version

select version();

Update allowed ip for a user

# if version 5.7.8 and up, can use remame statement
RENAME USER 'qrhen'@'10.17.259.170' TO 'qrhen'@'10.31.248.110';

Grant permission to user

GRANT SELECT ON `MyTabble`.* TO `lyhen`@`10.33.264.%`;



FLUSH PRIVILEGES;

SHOW GRANTS FOR `lyhen`@`10.33.264.%`;

#show grants for current logged in user
show grants;

Check which process owns 3306

lsof -i :3306

Saturday, 26 November 2011

jQuery cheat sheet

  • parent()
  • parents()
  • closest() find the parent element nearest to the selected element
  • children()
  • siblings()
  • prev()
  • next()
  • remove() $("p").remove() remove p elements
  • empty() $("p").empty() delete content in ps
  • detache() $elememts = $("p").detache() remove and hold
  • replacewith()
  • before()
  • after()
  • first()
  • eq()
  • last()
  • slice()
  • filter()
  • not()

Thursday, 24 November 2011

Create a SVN repository

Assume SVN server name is apple

1. ssh to  apple
svnadmin create /var/svn/project_name

2. go back to my machine, cd to  project_name/
svn import svn+ssh://apple/var/svn/project_name

3. check out
svn co svn+ssh://apple/var/svn/project_name

Wednesday, 23 November 2011

Regular Expression

Special Symbol
 \d is digit
\s is white space
. any char
\w equal [A-Za-z0-9_]


boundary
^ is start of string
$ is end of string
\b word boundary

how many times (for previous item)
?  0 or 1
* 0 or more
+ 1 or more
{n} n times
{n,} at least n times
{n,m} n<=match times<=m

group
(pattern) group as one item
(x|y) x or y
[abc] either a,b or c
[^abc] any char except a,b,c
[a-d] match a,b.c or d

PHP related function
preg_match("/mypatter/i", $subject) return 1 if match; otherwise, return 0.  i is a flag for case insensitive search.

Example

all combinations of digits chars and spaces  /^([a-zA-Z\d\s]+)+$/  For this regular expression, empty string will fail







Sample bashrc file

export CVS_RSH=ssh
export CVSROOT=:ext:cvs.somedomain.com:/var/cvs/cvsroot/php
export PATH=/homes/lshen/bin/:/usr/local/lib/jre1.6.0_22/bin/:/usr/bin/:/usr/bin/php/bin/:$PATH
export CLASSPATH=/usr/local/lib/jdk1.6.0_22/bin/:.
export JAVA_HOME=/usr/local/lib/jdk1.6.0_22/bin/
export EDITOR=/usr/bin/vim
export LC_CTYPE=en_US.UTF-8

xset b off

alias ls='ls --color=tty'
alias phpcs='phpcs --standard=Zend';
alias vi='vim'
alias lpr='lpr -P Ricoh'

Some useful command (Mysql Vi etc) Memo

My SQL

Grant a user to a database
mysql>grant all on db_name.* to 'db_username'@'localhost' identified by 'db_userpasswd'
dump a database to a sql file
mysqldump -u root -p --opt leo_db>dump.sql

vi

Auto replace string in vi
:%s/pattern_search_for/new_string_to_be_used/gc

Open and create compressed files

Open files
tar zxvf filename.tar.gz
unzip filename.zip
tar -xvf my.tar
bunzip2 studybuzz.bz2
bzip2 -d studybuzz.bz2
tar jxvf studybuzz.tar.bz2
unrar e test.rar

Create a zip file without parent directory

/test/test.txt
/test/test2.txt
cd test
zip -r -j my.zip *
Create files
zip -r filename.zip filedir
tar -cvf my.tar filedir
Print Env Variables
printenv
curl
curl -H "x-api-key:1234" http://myapi.com/user/1

#The -v option activates verbose mode, revealing detailed communication logs between the client and server
curl -v -H "x-api-key:1234" http://myapi.com/user/1

#curl does not follow HTTP redirects by default. Use the -L, --location option to tell it to do that
curl -L -H "x-api-key:1234" http://myapi.com/user/1