How to safely commit vendor dir with php Composer

Composer is great tool, but sometimes you will need avoid all external dependencies. Sometimes you will need have all source codes in your git repository. Should I commit the dependencies in my vendor directory, and still have all benefits of using Composer? Here are my requirements: - Everything need to be automatically. - I will work on application and my packages together. - I need a simple way how to commit and publish new version of packages. - Workflow need to be simple (mimimum of commands)

How to commit vendor dir?

Remove vendor dir from your .gitignore and add follow lines to root of composer.json

"scripts": {
 "post-update-cmd": ["echo [WARNING] Delete all .git dirs", "rm -rf vendor/**/**/.git"],
 "post-install-cmd": ["echo [WARNING] Delete all .git dirs", "rm -rf vendor/**/**/.git"]
},

That’s automatically remove the .git directory of every dependency after the installation or update. You can safely commit everything to your git repo and avoid mistakes with fake git “submodules”. Run: composer update and commit everything to your repo. ### How make changes in packages?

When you are using tagged releases (no dev versions) of package then run:

rm -rf vendor/some/package && composer update some/package --prefer-source --no-scripts

For dev versions (dev-master) it’s simpler:

rm -rf vendor/some/package && composer update some/package --no-scripts

Now, when you go to vendor/some/package dir it’s common git repository - you can make changes, commit and publish new versions of package. But don’t forgot run composer update before commit main repo, becouse there is still .git dir in package! ### Disadvantages

Setup email sending from PHP on Mac

First write in terminal: which sendmail wlll return path to sendmail app (in my case /usr/sbin/sendmail). Then open php.ini file and setup:

sendmail_path = /usr/sbin/sendmail -t -i

Create necessary folder and setup postfix permissions:

sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions
sudo /usr/sbin/postfix start

And check if emails will come:

php -r "mail('youremail@domain.com', 'subject', 'message', 'From: <youremail@domain.com>' . PHP_EOL);"

Wordpress - How to get user by metadata

Getting user by field name is easy, #Wordpress have function for that get_user_by. Getting user by his metadata is a little bit complicated. You can found on the Internet some complex procedures how to get user by metadata using WP_Query object. Forget about them! You need prepare correct meta query as array or string and call get_users. Thats all!

/**
 * Get user by his metadata
 * @author Roman Ozana <ozana@omdesign.cz>
 * @param $meta_key
 * @param $meta_value
 * @return mixed
 */
function get_user_by_meta($meta_key, $meta_value) {
 return reset(
  get_users(
   array(
    'meta_key' => $meta_key,
    'meta_value' => $meta_value,
    'number' => 1,
    'count_total' => false
   )
  )
 );
}

How to detect Retina display in CSS?

@media only screen and (-webkit-min-device-pixel-ratio: 2),(min-resolution: 192dpi) {
  /* it's retina yeah! */
}

Toggle Hidden Files in Finder

Run Automator and create new Application. Add task Run Shell script and paste follow code:

STATUS=`defaults read com.apple.finder AppleShowAllFiles`
if [ $STATUS == YES ];
then
    defaults write com.apple.finder AppleShowAllFiles NO
else
    defaults write com.apple.finder AppleShowAllFiles YES
fi
killall Finder

Save application. From now can Tooggle

List all mounted drives on mac

It’s simple: df -h

Wodpress constants for human-readable intervals

Constants for expressing human-readable intervals in new #Wordpress are very useful:

define( 'MINUTE_IN_SECONDS', 60 );
define( 'HOUR_IN_SECONDS',   60 * MINUTE_IN_SECONDS );
define( 'DAY_IN_SECONDS',    24 * HOUR_IN_SECONDS   );
define( 'WEEK_IN_SECONDS',    7 * DAY_IN_SECONDS    );
define( 'YEAR_IN_SECONDS',  365 * DAY_IN_SECONDS    );

Example:

wp_cache_add($key, $data, 'default, 5 * DAY_IN_SECONDS);

Make WordPress globals more accessible

Wordpress using a lot globals variables. These variables are used throughout WordPress code for various reasons. Great example is database connection object wpdb. Here is common example how to use wpdb in some function:

function something() {
  global $wpdb;
  /** @var wpdb $wpdb */
  $wpdb->query('SQL...');
}

It’s highly uncomfortable and long! Therefore, I have prepared a simple object which make all global variables much more accesible from anywhere: https://gist.github.com/OzzyCzech/4737518 (PHP 5.3+ only)

function something() {
  Globals::wpdb()->query('SQL...');
}

Wordpress - register and setup widgets from PHP code

PHP driven registration of the #Widgets in #Wordpress can be quite confusing. There is my way how to handle it:

class Widgets {

 private $widgets = array();
 private $options = array();

 public function __construct() {
  $this->options = wp_get_sidebars_widgets();
 }

 public function unregisterAllWidgets() {
  foreach ($this->options as $name => $values)
   $this->options[$name] = array();
 }

 public function cleanWidgetSettings($type, $multi = null) {
  $this->widgets[$type] = array();
  if ($multi !== null) $this->widgets[$type]['_multiwidget'] = (int)(bool)$multi;
 }

 public function setMultiWidget($type, $val = 1) {
  $this->widgets[$type]['_multiwidget'] = $val;
 }

 public function add($type, array $options, $area = null) {
  $this->widgets[$type][] = $options;
  if ($area !== null) {
   if (array_key_exists($area, $this->options) === false) {
    throw new Exception('Widget area ' . $area . ' not exists.');
   }
   end($this->widgets[$type]);
   $this->options[$area][] = $type . '-' . key($this->widgets[$type]);
  }
 }

 public function __destruct() {
  foreach ($this->widgets as $name => $settings) {
   if (!array_key_exists('_multiwidget', $settings)) $settings['_multiwidget'] = 1;
   update_option('widget_' . $name, $settings);
  }
  wp_set_sidebars_widgets($this->options);
 }

}

Following code is an example how to use class Witgets:

$widgets = new Widgets();
$widgets->unregisterAllWidgets();

$widgets->add(
 'text',
 array(
  'title' => 'About us',
  'text' => 'Simple text about us',
  'filter' => true,
  'classes' => '',
  'ids' => null,
 ),
 'footer-1'
);

// ...

This code need to be call only once. Put your code to function.php to after_switch_theme action:

add_action(
 'after_switch_theme', function () {
  $widgets = new Widgets();
  $widgets->unregisterAllWidgets();
  // ...
 }
);

https://gist.github.com/4663766/

Seznam věcí které Internet zabil

Co všechno Internet zabil nebo zabíjí?

  1. Hudební průmysl
  2. Filmový průmysl
  3. Porno průmysl
  4. Zásilkové služby (Quelle, Neckermann atd.)
  5. Pohledy a dopisy psané rukou
  6. Anonymitu (ta postupně mizí)
  7. DVD a videopůjčovny
  8. Časopisy noviny a poctivou novinařinu
  9. Televizi
  10. Encyklopedie
  11. MMS, SMS
  12. Tištěné mapy