Mac OS X: Install and set up ctags properly

I was reading an article yesterday by Jon Cairns – Using Vim as a PHP IDE and he mentioned using exuberant-ctags as one of the extension I can use for VIM.

I tried to follow the example given on the website, but it didn’t work. I searched for any hint for Mac OSX, but unfortunately none of them worked for me.

Thanks to Exuberant Ctags on Mac on StackOverflow I knew, that I have to install it by using:

brew install ctags

But even after the install, the ctags was still not working as expected.

Each time I typed:

$ ctags -R -file php.tags --languages=PHP

The only result I was getting was:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ctags: illegal option -- R
usage: ctags [-BFadtuwvx] [-f tagsfile] file ...

Then I found another hint, how to make ctags working on my Mac OS: ctags setup on mac, but as you can guest, that didn’t help me as well.

I was feeling bad, but I’m not looser so I continued my research. At last I thought I must have done a mistake when installing ctags with brew, so I decided to uninstall it and install again. And the solution was just right there.

The whole problem was, that ctags is installed in different directory:

$ brew install ctags
....
/usr/local/Cellar/ctags/5.8_1: 8 files, 345.5K

Taking into the fact the thing I learned from ctags setup on mac, I updated my alias as following:

alias ctags="`brew --prefix`/Cellar/ctags/5.8_1/bin/ctags"

and it solved my problem.

Ctags were finally setup on my Mac OSX and working as expected.

Protected: DibiPHP: Quick start in english

This content is password protected. To view it please enter your password below:

Composer: Install package to custom directory

I had a nice challenge yesterday. I need to find a way, how to tell composer to install one of the package into custom directory – basically outside vendor folder.

It was not easy fight, but the feeling of victory is great.

1. Prepare test package repository

I will include this project into my main project by using composer functionality. You can see this test-package here.

{
  "name" : "ptx/test_package",
  "description" : "Test package repository.",
  "version" : "0.9",
  "authors" : [ 
    {   
      "name" : "Tomas Pavlatka",
      "homepage" : "http://www.pavlatka.cz",
      "email" : "tomas@pavlatka.cz"
    }   
  ],  
  "require" : { 
    "dibi/dibi": "^3.0"
  }
}

2. Create main project + composer.json

Let’s create our main project and create new composer.json file to load what we need for this project. Our main project can be found here.

To use our idea, we need to use mnsami/composer-custom-directory-installer package. Without it, it will not work.

{
  "repositories" : [ 
    {   
      "type" : "git",
      "url" : "https://pavlatka@bitbucket.org/pavlatka/test-package.git"
    }   
  ],  
  "extra" : { 
    "installer-paths" : { 
      "ptx/test/" : ["ptx/test_package"]
    }   
  },  
  "require": {
    "ptx/test_package" : "dev-master",
    "mnsami/composer-custom-directory-installer": "^1.1"
  }   
}

2a. repositories

Let’s specify that we need to load our custom package from our repository. If you do not use GIT repository, there’re another ways how to load your package. Please check composer repository documentation for further assistance.

2b. Extras

We have to tell composer, where to install our package. We want our package to be installed under /ptx/test folder instead of /vendor folder.

3. Run composer install to load and build everything for us.

The last thing we need to do is to run composer install and let composer to do its magic.

Our final structure is:

- README
- composer.json
- composer.lock
- ptx
-- test
--- .gitignore
--- README
--- composer.json
- vendor
-- autoload.php
-- composer
-- dibi
-- mnsami

Big plus for Belgium railways

I was travelling last weekend to Belgium and I have to say big thank you to Belgium Railways. I was really surprised how clean the trains were and also how helpful the personal was.

They answered all our questions – even helped us to find correct connection. Everything with smile on their faces without a look like – why are you bothering us. It might seem a little, but comparing to Berlin situation, it was like day and night.

I’m looking forward to use your service again. Thank you one more time.

Book: Database Design for mere mortals

database_design_for_mere_mortal

Yesterday I got a new book to read: Michael J. Hernandez / Database Design for mere mortals. I believe it will be a good mate during my travels to and from job. It has about 610 pages, so I have a lot of to read :).

From first glance, it will be much more readable than my previous book, which I haven’t finished and I’m afraid whether I will ever find the courage to at least try to finish it.

I will keep you updated about the content with my notes.

Enable XDebug on MAMP step by step

I had to enable XDebug on my MAMP installation. And as my common practice, I decided to make a step by step visual manual how to do it.

1. Backup your php.ini file

I always recommend to make a backup version of your current php.ini file, so you can revert easily in case something goes wrong.

$ cd /Applications/MAMP/bin/php/php5.6.10/conf
$ cp php.ini php.ini.bck

Please check version of PHP you are using on your MAMP installation. It might be different than mine.

2. Edit php.ini

Edit php.ini file and find section [xdebug]

$ vim php.ini

By default it looks somehow like following

[xdebug]
;zend_extension="/Applications/MAMP/bin/php/php5.5.26/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"

The only thing you have to do is to change it to following

[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.5.26/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_autostart=1

and save it.

3. Restart your apache and enjoy xdebug

$ sudo /Applications/MAMP/bin/stopApache.sh && sudo /Applications/MAMP/bin/startApache.sh 

Mac OS X – Fix broken .bash_profile

Murphy law says:

If things can go wrong, they will surely go :).

I was trying to add PATH to my .bash_profile and I made mistake. This is not something you’d like to do because this will break all your command line commands. Not even vim will be working to fix .bash_profile file.

So what to do next?

Thanks to the help on stackoverflow.com there’s an easy solution.

Open your terminal and run:

export PATH=/bin:/usr/bin:/usr/local/bin

When you restart the terminal, all your commands will be working again as expected.

vim ~/.bash_profile

So now we can keep breaking our PATH as often as we want since we know how to repair it easily ;).

Mac OS X – Enable memcached on MAMP

I had to install memcached on Mac OSX El Captain and MAMP and it was quite tricky. Let’s do it together to make your life more easier.

0. Preparation

The easiest way is to use homebrew to install it. If you do not have brew already, run command in your terminal.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

1. Install memcached

To install memcached, run this command in your terminal.

brew install memcached

Most of the time, everything goes well and that’s the reason why a lot of articles finishes here. I had an issue yesterday with libevent package. But I have removed and install it again and the issue was solved.

brew remove --force libevent
brew install libevent

2. Install memcached as PHP extension

This quite depends on the PHP version you are using with the MAMP. To find all the supported php memcached version, run this command in the terminal:

brew search memcached

I use PHP 7.0 on localhost, so I have to install php70-memcached. But as I said, this depends on the PHP version, you are running on localhost.

brew install homebrew/php/php70-memcached

For some reason I was force to run this command, so maybe you will need to run it as well. But it’s also part of the message / help written in terminal, so it’s good idea to read them 😉

brew install --HEAD homebrew/php/php70-memcached

Btw, this command takes a while to finish, but now we should have memcached installed on Mac OS X.

php -i | grep memcached
...
memcached support => enabled
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => php => php
...

3. Create memcached.so and update php.ini
Now the memcached is installed for our PHP Cli version (command line version of PHP), but it might not be accessible for PHP running in browser. If you didn’t know there’re 2 version of PHP, it’s fine ;). You learned another new thing.

3a. Download memcached.so from Github
I found a lot of articles describing how to build memcached.so from source codes of PHP, but actually it never worked for me. Most probably I was doing some silly mistakes, but at the end, I found Github repository with already done memcached.so files which are ready to be downloaded and used.

https://github.com/majksner/php-memcached-mamp

I would recommend you to do the same – download it and use it. It will save you a lot of time 😉

3b. Copy memcached.so to proper extension folder
As I said earlier, I use PHP 7.0 on my localhost, so the folder with extension for me is

/Applications/MAMP/bin/php/php7.0.0/lib/php/extensions/no-debug-non-zts-20151012

So move the file to the proper folder.

3c. Edit php.ini and add info about new extension to be loaded

Edit proper php.ini file

vim /Applications/MAMP/bin/php/php7.0.0/conf/php.ini

And add

extension=memcached.so

Somewhere in this file. It’s good idea to find where extension= are and add it as the last one.

3c. Restart MAMP

The last thing to be done is to restart MAMP and check whether everything is working as expected.

If you run http://localhost:8888/MAMP/index.php?language=English&page=phpinfo in your browser, you should find memcached section on this page. If yes, congratulation – memcached is installed on your MAMP and you can take advantage of it.

4. Congratulation

I do not know better feeling than when the things are finally working as expected. So CONGRATULATION, memcached is up and running on our Mac OS X El Captain and also in MAMP.

Please let me know if you had any other issues while installing mamcached on your Mac OS X using MAMP, so we can solve them together.

MySQL – How to find out hour difference between two dates

My challenge for yesterday was to find out, how many hours a user is registered. So in other words, what’s hour difference between the date/time an user register and now.

And believe or not, it’s pretty easy.

# Created is saved as unixtime, e.g. 1455251945
SELECT 
  FLOOR((now() - u.created) / 3600) hours
FROM
  users u
WHERE 
  uid = 1;

If you save created as datetime, you can still do it.

# Created is saved as datetime, e.g. 2016-02-11 11:00:12
SELECT 
  FLOOR((now() - UNIX_TIMESTAMP(u.created)) / 3600) hours
FROM
  users u
WHERE 
  uid = 1;

So challenge done, job done and smile kept 😉

Mac OS X – Set up autocomplete hotkey in MySQLWorkbench

I use MySQLWorkbench everyday and the autocomplete hotkey is not enabled by default. I can’t figure out why, but luckily, there is a way how to enable it.

1. Edit /Applications/MySQLWorkbench.app/…/main_menu.xml

vim /Applications/MySQLWorkbench.app/Contents/Resources/data/main_menu.xml

2. Find section Auto-complete

You can use /Auto-complete in vim to find the string

3. Remove comments

<value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.edit.code-completion-menu">
  <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.edit</link>
  <value type="string" key="caption">Code Completion</value>
  <value type="string" key="name">cc-menu</value>
  <value type="string" key="itemType">cascade</value>
  <value type="string" key="context">*query</value>

  <value type="list" content-type="object" content-struct-name="app.MenuItem" key="subItems">
    <value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.edit.list-members">
      <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.edit</link>
      <value type="string" key="caption">Auto-complete</value>
      <value type="string" key="name">list-members</value>
      <value type="string" key="command">builtin:list-members</value>
      <value type="string" key="itemType">action</value>
      <value type="string" key="shortcut">Modifier+Space</value>
      <value type="string" key="platform">windows,linux</value>
      <value type="string" key="context">*query</value>
    </value>
    <value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.edit.list-members-mac">
      <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.edit</link>
      <value type="string" key="caption">Auto-complete</value>
      <value type="string" key="name">list-members</value>
      <value type="string" key="command">builtin:list-members</value>
      <value type="string" key="itemType">action</value>
      <value type="string" key="shortcut">Command+Escape</value>
      <value type="string" key="platform">macosx</value>
      <value type="string" key="context">*query</value>
    </value>
  </value>
</value>

4. Change hot-key in case you like another one.

<value type="string" key="shortcut">Command+Escape</value>
<value type="string" key="platform">macosx</value>

5. Restart MySQL Workbench

Restart MySQL Workbench and enjoy your Code completion / Autocomplete feature.

© 2016 Tomas Pavlatka

Theme by Anders NorenUp ↑