PHP Event API
Events
I tried a couple of existing plugin solutions most did 95% of what I needed and the other 5% was next to impossible to force.
After wasting days I decided I would write my own, what follows is an overview of Tentacle event API.
Contact me if you have any questions.
Registering an event:
[sourcecode language="php"]
function method_one (){
echo ‘one ‘;
}
function method_two (){
echo ‘two ‘;
}
event::on(‘event_trigger’, ‘method_one’, 2);
event::on(‘event_trigger’, ‘method_two’, 1);
event::trigger(‘event_trigger’);
[/sourcecode]
Turning events off:
[sourcecode language="php"]event::off(‘event_trigger’);
event::off(‘event_trigger’, ‘method_two’);
event::off(null, ‘method_one’);[/sourcecode]
Test if an event exists:
[sourcecode language="php"]var_dump(event::exists(‘event_trigger’));
[/sourcecode]
boolean true
Trigger an event:
[sourcecode language="php"]function method_name ( )
{
echo ‘my method name’;
}
event::on(‘event_name’, ‘method_name’);
event::trigger(‘event_name’);[/sourcecode]
my method name
Trigger an event and pass data to it:
[sourcecode language="php"]function method_data ( $text = ” )
{
echo ‘ 1 my method data is ‘.$text;
}
function method_data_two ( $text = ” )
{
echo ‘ 2 my method data is ‘.$text;
}
event::on(‘event_data’, ‘method_data’, 1);
event::on(‘event_data’, ‘method_data_two’, 2);
event::trigger(‘event_data’, ‘this’);[/sourcecode]
1 my method data is this 2 my method data is this
Triggering a class and passing data to the method
[sourcecode language="php"]class my
{
static function method_name ( $text = ” )
{
echo ‘my class method name is ‘.$text;
}
}
event::on(‘event_class’, ‘my::method_name’);
event::trigger(‘event_class’, ‘Lary’);[/sourcecode]
my class method name is Lary
Event chaining
[sourcecode language="php"]function method_sad ( $text = ” )
{
return str_replace(‘blah’, "sad", $text);
}
function method_happy ( $text = ” )
{
return str_replace(‘sad’, "happy", $text);
}
event::on(‘event_mood’, ‘method_sad’, 1);
event::on(‘event_mood’, ‘method_happy’, 2);
echo event::filter(‘event_mood’, ‘I am blah!’);
[/sourcecode]
I am happy!
A big leap for Tentacle
Previously the Tentacle blog was powered by WordPress, A few months ago I had spent a lot of time working on the ability to import the WordPress WXR files.
For the moment this only includes posts, tags, categories, and media. No comments or pages.
This could change in the future.
This for me was actually a big deal, and to make it happen a number of things had to be considered.
- The category and tag relations had to be kept
- Media needed to be transferred to the new site
- Media links in the post content also needed to be remapped
- Media needed to be reprocessed
The only Issue that I came across was a memory error while resizing a rather large PNG image.
Today tentaclecms.com/blog/ is totally powered by Tentacle its self. It is lacking pagination but that’s in the works!
One problem that I noticed right off the start was how would the URLs be managed.
When you build an application using MVC there are routs, and those routs need to match a pattern. The problem was that I did not want to create a bunch of controllers. So I built a special version of routs that would specifically handle blog related requests, mapping requests for tags, categories, pages, and posts possible.
My next problem was dealing with content and how plugins could interact with it.
I will go into more detail later on about how plugins and events are created, but at the core Tentacle will do a few nice things for you out of the box.
- It will automatically wrap lines with <p> tags if they are not already there.
- It will convert links and email addressed to clickable items.
- Rendering using SmartyPants
- An event call for [shortcode]
A lot of work went into making this happen, and Tentacle is that much closer to a beta release!
Recursive Glob
While building the upgrade script for tentacle I knew that the file I was going to work with would be a zip, and would contain many sub folders.
PHP has a function called glob that finds files pathnames matching a pattern.
It works really well but only goes one level deep, using the recursive_glob() function lets you return a folders file structure.
Paste + Data + No Account concept
Originally inspired by paste.laravel.com, I wanted to create something that could be used to easily share code snippets for Tentacle.
But then I started thinking of other ways this could be useful.
Currently you can add, fork ( edit ), and view raw text. I also added the ability to upload images, soon I would like to add a gallery option.
Late on I am going to add the ability to store files, multiple files will be viewable as a list or downloadable as a zip.
I have been considering ways of managing user data without requiring user accounts.
No password users an email address as a login, you receive an email with a session link.
If you were to login from another location you previous session is closed. This would make it possible to keep sensitive data private.
By tracking your friends email we can go about displaying the content in much the same manner.
Hierarchical Data
I am proud to announce that Tentacle will now support sub pages! That’s right! Nest as many as you want!
One of the biggest challenges I had with building this CMS is the data structure.
On paper it looks easy.
Posts will have an ID, a Parent ID, Date, Title, and so on.
Help me test Tentacles Installation!
I want to make Tentacle a dream to install.
For the time being I would like to get a group of potential users to test the setup process.
The ideal tester should be a little techie, you don’t need to be a hard core nerd but you should understand what the errors mean.
And please send me feedback on the blog, or send a tweet, even an email.
Thanks!
DIY simple staging server.
While working on Tentacle I needed a simple solution for testing code on a remote web server.
In the past I would work locally and syncing with FTP to the server and then tested off of the remote server, I used SVN for my source control. Within the last year I have started working locally and using Git as my main source control.
This left a bit of a gap in my process where I could no longer test on a remote server without updating it manually by S/FTP or opening terminal and manually calling a git pull.
Open terminal and manually git pull it did break up the work flow a bit so using the Dingo framework I created a very simple Git helper and gave it its own URL something like git/pull.
I then used a Github Post-Receive URL Hook found under admin/service hooks that pointed to my staging server git pull URL, every time I do a push to Github, Github will then automatically fire the URL thus triggering the pull helper.
One little note is that if you check out a dev branch on a staging server and also have a live server you would add a second URL with the same code on that liver server. One push would update both sites.
Just make sure the live server is on the right branch.
If you don’t feel like using a hook then no problem at all. Just call the URL in the browser and you will see the Git pull message.
